blob: 93bd286395955ccc90cb79bd8c8f445489ed6362 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Guenter Roeckaff6e002012-01-19 11:02:27 -08002 * w83781d.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (c) 1998 - 2001 Frodo Looijaard <frodol@dds.nl>,
5 * Philip Edelbrock <phil@netroedge.com>,
6 * and Mark Studebaker <mdsxyz123@yahoo.com>
7 * Copyright (c) 2007 - 2008 Jean Delvare <khali@linux-fr.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/*
Guenter Roeckaff6e002012-01-19 11:02:27 -080025 * Supports following chips:
26 *
Jean Delvare4101ece2012-11-05 21:54:40 +010027 * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
Guenter Roeckaff6e002012-01-19 11:02:27 -080028 * as99127f 7 3 0 3 0x31 0x12c3 yes no
29 * as99127f rev.2 (type_name = as99127f) 0x31 0x5ca3 yes no
30 * w83781d 7 3 0 3 0x10-1 0x5ca3 yes yes
31 * w83782d 9 3 2-4 3 0x30 0x5ca3 yes yes
32 * w83783s 5-6 3 2 1-2 0x40 0x5ca3 yes no
33 *
34 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Joe Perches1ca28212011-01-12 21:55:11 +010036#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/slab.h>
41#include <linux/jiffies.h>
42#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040043#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020044#include <linux/hwmon-vid.h>
Jean Delvare34875332007-05-08 17:22:03 +020045#include <linux/hwmon-sysfs.h>
Jim Cromie311ce2e2006-09-24 21:22:52 +020046#include <linux/sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040047#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010048#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Wolfgang Grandegger443850c2008-10-17 17:51:18 +020050#ifdef CONFIG_ISA
51#include <linux/platform_device.h>
52#include <linux/ioport.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020053#include <linux/io.h>
Wolfgang Grandegger443850c2008-10-17 17:51:18 +020054#endif
55
56#include "lm75.h"
Jean Delvare7666c132007-05-08 17:22:02 +020057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050059static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
60 0x2e, 0x2f, I2C_CLIENT_END };
Jean Delvare3aed1982009-01-07 16:37:32 +010061
Jean Delvaree5e9f442009-12-14 21:17:27 +010062enum chips { w83781d, w83782d, w83783s, as99127f };
63
64/* Insmod parameters */
Jean Delvare3aed1982009-01-07 16:37:32 +010065static unsigned short force_subclients[4];
66module_param_array(force_subclients, short, NULL, 0);
67MODULE_PARM_DESC(force_subclients, "List of subclient addresses: "
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
69
Rusty Russell90ab5ee2012-01-13 09:32:20 +103070static bool reset;
Jean Delvarefabddcd2006-02-05 23:26:51 +010071module_param(reset, bool, 0);
72MODULE_PARM_DESC(reset, "Set to one to reset chip on load");
73
Rusty Russell90ab5ee2012-01-13 09:32:20 +103074static bool init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075module_param(init, bool, 0);
76MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
77
78/* Constants specified below */
79
80/* Length of ISA address segment */
81#define W83781D_EXTENT 8
82
83/* Where are the ISA address/data registers relative to the base address */
84#define W83781D_ADDR_REG_OFFSET 5
85#define W83781D_DATA_REG_OFFSET 6
86
Jean Delvare34875332007-05-08 17:22:03 +020087/* The device registers */
88/* in nr from 0 to 8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
90 (0x554 + (((nr) - 7) * 2)))
91#define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
92 (0x555 + (((nr) - 7) * 2)))
93#define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
94 (0x550 + (nr) - 7))
95
Jean Delvare34875332007-05-08 17:22:03 +020096/* fan nr from 0 to 2 */
97#define W83781D_REG_FAN_MIN(nr) (0x3b + (nr))
98#define W83781D_REG_FAN(nr) (0x28 + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100#define W83781D_REG_BANK 0x4E
101#define W83781D_REG_TEMP2_CONFIG 0x152
102#define W83781D_REG_TEMP3_CONFIG 0x252
Jean Delvare34875332007-05-08 17:22:03 +0200103/* temp nr from 1 to 3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#define W83781D_REG_TEMP(nr) ((nr == 3) ? (0x0250) : \
105 ((nr == 2) ? (0x0150) : \
106 (0x27)))
107#define W83781D_REG_TEMP_HYST(nr) ((nr == 3) ? (0x253) : \
108 ((nr == 2) ? (0x153) : \
109 (0x3A)))
110#define W83781D_REG_TEMP_OVER(nr) ((nr == 3) ? (0x255) : \
111 ((nr == 2) ? (0x155) : \
112 (0x39)))
113
114#define W83781D_REG_CONFIG 0x40
Jean Delvarec7f5d7e2006-02-05 23:13:48 +0100115
116/* Interrupt status (W83781D, AS99127F) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#define W83781D_REG_ALARM1 0x41
118#define W83781D_REG_ALARM2 0x42
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Jean Delvare05663362007-11-30 23:51:24 +0100120/* Real-time status (W83782D, W83783S) */
Jean Delvarec7f5d7e2006-02-05 23:13:48 +0100121#define W83782D_REG_ALARM1 0x459
122#define W83782D_REG_ALARM2 0x45A
123#define W83782D_REG_ALARM3 0x45B
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#define W83781D_REG_BEEP_CONFIG 0x4D
126#define W83781D_REG_BEEP_INTS1 0x56
127#define W83781D_REG_BEEP_INTS2 0x57
128#define W83781D_REG_BEEP_INTS3 0x453 /* not on W83781D */
129
130#define W83781D_REG_VID_FANDIV 0x47
131
132#define W83781D_REG_CHIPID 0x49
133#define W83781D_REG_WCHIPID 0x58
134#define W83781D_REG_CHIPMAN 0x4F
135#define W83781D_REG_PIN 0x4B
136
137/* 782D/783S only */
138#define W83781D_REG_VBAT 0x5D
139
140/* PWM 782D (1-4) and 783S (1-2) only */
Jean Delvare34875332007-05-08 17:22:03 +0200141static const u8 W83781D_REG_PWM[] = { 0x5B, 0x5A, 0x5E, 0x5F };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#define W83781D_REG_PWMCLK12 0x5C
143#define W83781D_REG_PWMCLK34 0x45C
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145#define W83781D_REG_I2C_ADDR 0x48
146#define W83781D_REG_I2C_SUBADDR 0x4A
147
Guenter Roeckaff6e002012-01-19 11:02:27 -0800148/*
149 * The following are undocumented in the data sheets however we
150 * received the information in an email from Winbond tech support
151 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Sensor selection - not on 781d */
153#define W83781D_REG_SCFG1 0x5D
154static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
155
156#define W83781D_REG_SCFG2 0x59
157static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
158
159#define W83781D_DEFAULT_BETA 3435
160
Jean Delvare474d00a2007-05-08 17:22:03 +0200161/* Conversions */
162#define IN_TO_REG(val) SENSORS_LIMIT(((val) + 8) / 16, 0, 255)
163#define IN_FROM_REG(val) ((val) * 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165static inline u8
166FAN_TO_REG(long rpm, int div)
167{
168 if (rpm == 0)
169 return 255;
170 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
171 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
172}
173
Jean Delvare474d00a2007-05-08 17:22:03 +0200174static inline long
175FAN_FROM_REG(u8 val, int div)
176{
177 if (val == 0)
178 return -1;
179 if (val == 255)
180 return 0;
181 return 1350000 / (val * div);
182}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Jean Delvare474d00a2007-05-08 17:22:03 +0200184#define TEMP_TO_REG(val) SENSORS_LIMIT((val) / 1000, -127, 128)
185#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Guenter Roeckc531eb32012-01-15 09:19:16 -0800187#define BEEP_MASK_FROM_REG(val, type) ((type) == as99127f ? \
Jean Delvare2fbbbf12008-10-17 17:51:18 +0200188 (~(val)) & 0x7fff : (val) & 0xff7fff)
Guenter Roeckc531eb32012-01-15 09:19:16 -0800189#define BEEP_MASK_TO_REG(val, type) ((type) == as99127f ? \
Jean Delvare2fbbbf12008-10-17 17:51:18 +0200190 (~(val)) & 0x7fff : (val) & 0xff7fff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192#define DIV_FROM_REG(val) (1 << (val))
193
194static inline u8
195DIV_TO_REG(long val, enum chips type)
196{
197 int i;
198 val = SENSORS_LIMIT(val, 1,
199 ((type == w83781d
200 || type == as99127f) ? 8 : 128)) >> 1;
Grant Coadyabc01922005-05-12 13:41:51 +1000201 for (i = 0; i < 7; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (val == 0)
203 break;
204 val >>= 1;
205 }
Jean Delvare474d00a2007-05-08 17:22:03 +0200206 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209struct w83781d_data {
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200210 struct i2c_client *client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700211 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100212 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 enum chips type;
214
Jean Delvare360782d2008-10-17 17:51:19 +0200215 /* For ISA device only */
216 const char *name;
217 int isa_addr;
218
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100219 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 char valid; /* !=0 if following fields are valid */
221 unsigned long last_updated; /* In jiffies */
222
223 struct i2c_client *lm75[2]; /* for secondary I2C addresses */
224 /* array of 2 pointers to subclients */
225
226 u8 in[9]; /* Register value - 8 & 9 for 782D only */
227 u8 in_max[9]; /* Register value - 8 & 9 for 782D only */
228 u8 in_min[9]; /* Register value - 8 & 9 for 782D only */
229 u8 fan[3]; /* Register value */
230 u8 fan_min[3]; /* Register value */
Jean Delvare474d00a2007-05-08 17:22:03 +0200231 s8 temp; /* Register value */
232 s8 temp_max; /* Register value */
233 s8 temp_max_hyst; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 u16 temp_add[2]; /* Register value */
235 u16 temp_max_add[2]; /* Register value */
236 u16 temp_max_hyst_add[2]; /* Register value */
237 u8 fan_div[3]; /* Register encoding, shifted right */
238 u8 vid; /* Register encoding, combined */
239 u32 alarms; /* Register encoding, combined */
240 u32 beep_mask; /* Register encoding, combined */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 u8 pwm[4]; /* Register value */
Jean Delvare34875332007-05-08 17:22:03 +0200242 u8 pwm2_enable; /* Boolean */
Guenter Roeckaff6e002012-01-19 11:02:27 -0800243 u16 sens[3]; /*
244 * 782D/783S only.
245 * 1 = pentium diode; 2 = 3904 diode;
246 * 4 = thermistor
247 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 u8 vrm;
249};
250
Wolfgang Grandegger443850c2008-10-17 17:51:18 +0200251static struct w83781d_data *w83781d_data_if_isa(void);
252static int w83781d_alias_detect(struct i2c_client *client, u8 chipid);
253
Jean Delvare31b8dc42007-05-08 17:22:03 +0200254static int w83781d_read_value(struct w83781d_data *data, u16 reg);
255static int w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static struct w83781d_data *w83781d_update_device(struct device *dev);
Jean Delvare7666c132007-05-08 17:22:02 +0200257static void w83781d_init_device(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259/* following are the sysfs callback functions */
260#define show_in_reg(reg) \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800261static ssize_t show_##reg(struct device *dev, struct device_attribute *da, \
Jean Delvare34875332007-05-08 17:22:03 +0200262 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{ \
Jean Delvare34875332007-05-08 17:22:03 +0200264 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 struct w83781d_data *data = w83781d_update_device(dev); \
Jean Delvare34875332007-05-08 17:22:03 +0200266 return sprintf(buf, "%ld\n", \
267 (long)IN_FROM_REG(data->reg[attr->index])); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269show_in_reg(in);
270show_in_reg(in_min);
271show_in_reg(in_max);
272
273#define store_in_reg(REG, reg) \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800274static ssize_t store_in_##reg(struct device *dev, struct device_attribute \
Jean Delvare34875332007-05-08 17:22:03 +0200275 *da, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{ \
Jean Delvare34875332007-05-08 17:22:03 +0200277 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
Jean Delvare7666c132007-05-08 17:22:02 +0200278 struct w83781d_data *data = dev_get_drvdata(dev); \
Jean Delvare34875332007-05-08 17:22:03 +0200279 int nr = attr->index; \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800280 unsigned long val; \
281 int err = kstrtoul(buf, 10, &val); \
282 if (err) \
283 return err; \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100284 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 data->in_##reg[nr] = IN_TO_REG(val); \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800286 w83781d_write_value(data, W83781D_REG_IN_##REG(nr), \
287 data->in_##reg[nr]); \
288 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100289 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return count; \
291}
292store_in_reg(MIN, min);
293store_in_reg(MAX, max);
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295#define sysfs_in_offsets(offset) \
Jean Delvare34875332007-05-08 17:22:03 +0200296static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
297 show_in, NULL, offset); \
298static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
299 show_in_min, store_in_min, offset); \
300static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
301 show_in_max, store_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303sysfs_in_offsets(0);
304sysfs_in_offsets(1);
305sysfs_in_offsets(2);
306sysfs_in_offsets(3);
307sysfs_in_offsets(4);
308sysfs_in_offsets(5);
309sysfs_in_offsets(6);
310sysfs_in_offsets(7);
311sysfs_in_offsets(8);
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313#define show_fan_reg(reg) \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800314static ssize_t show_##reg(struct device *dev, struct device_attribute *da, \
Jean Delvare34875332007-05-08 17:22:03 +0200315 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{ \
Jean Delvare34875332007-05-08 17:22:03 +0200317 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 struct w83781d_data *data = w83781d_update_device(dev); \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800319 return sprintf(buf, "%ld\n", \
Jean Delvare34875332007-05-08 17:22:03 +0200320 FAN_FROM_REG(data->reg[attr->index], \
321 DIV_FROM_REG(data->fan_div[attr->index]))); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323show_fan_reg(fan);
324show_fan_reg(fan_min);
325
326static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200327store_fan_min(struct device *dev, struct device_attribute *da,
328 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Jean Delvare34875332007-05-08 17:22:03 +0200330 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Jean Delvare7666c132007-05-08 17:22:02 +0200331 struct w83781d_data *data = dev_get_drvdata(dev);
Jean Delvare34875332007-05-08 17:22:03 +0200332 int nr = attr->index;
Guenter Roeckc531eb32012-01-15 09:19:16 -0800333 unsigned long val;
334 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Guenter Roeckc531eb32012-01-15 09:19:16 -0800336 err = kstrtoul(buf, 10, &val);
337 if (err)
338 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100340 mutex_lock(&data->update_lock);
Jean Delvare34875332007-05-08 17:22:03 +0200341 data->fan_min[nr] =
342 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvare31b8dc42007-05-08 17:22:03 +0200343 w83781d_write_value(data, W83781D_REG_FAN_MIN(nr),
Jean Delvare34875332007-05-08 17:22:03 +0200344 data->fan_min[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100346 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return count;
348}
349
Jean Delvare34875332007-05-08 17:22:03 +0200350static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
351static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
352 show_fan_min, store_fan_min, 0);
353static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
354static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
355 show_fan_min, store_fan_min, 1);
356static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
357static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO | S_IWUSR,
358 show_fan_min, store_fan_min, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360#define show_temp_reg(reg) \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800361static ssize_t show_##reg(struct device *dev, struct device_attribute *da, \
Jean Delvare34875332007-05-08 17:22:03 +0200362 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{ \
Jean Delvare34875332007-05-08 17:22:03 +0200364 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct w83781d_data *data = w83781d_update_device(dev); \
Jean Delvare34875332007-05-08 17:22:03 +0200366 int nr = attr->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800368 return sprintf(buf, "%d\n", \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
370 } else { /* TEMP1 */ \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800371 return sprintf(buf, "%ld\n", (long)TEMP_FROM_REG(data->reg)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 } \
373}
374show_temp_reg(temp);
375show_temp_reg(temp_max);
376show_temp_reg(temp_max_hyst);
377
378#define store_temp_reg(REG, reg) \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800379static ssize_t store_temp_##reg(struct device *dev, \
Jean Delvare34875332007-05-08 17:22:03 +0200380 struct device_attribute *da, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{ \
Jean Delvare34875332007-05-08 17:22:03 +0200382 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
Jean Delvare7666c132007-05-08 17:22:02 +0200383 struct w83781d_data *data = dev_get_drvdata(dev); \
Jean Delvare34875332007-05-08 17:22:03 +0200384 int nr = attr->index; \
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +0200385 long val; \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800386 int err = kstrtol(buf, 10, &val); \
387 if (err) \
388 return err; \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100389 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 \
391 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
392 data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
Jean Delvare31b8dc42007-05-08 17:22:03 +0200393 w83781d_write_value(data, W83781D_REG_TEMP_##REG(nr), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 data->temp_##reg##_add[nr-2]); \
395 } else { /* TEMP1 */ \
396 data->temp_##reg = TEMP_TO_REG(val); \
Jean Delvare31b8dc42007-05-08 17:22:03 +0200397 w83781d_write_value(data, W83781D_REG_TEMP_##REG(nr), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 data->temp_##reg); \
399 } \
400 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100401 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return count; \
403}
404store_temp_reg(OVER, max);
405store_temp_reg(HYST, max_hyst);
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407#define sysfs_temp_offsets(offset) \
Jean Delvare34875332007-05-08 17:22:03 +0200408static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
409 show_temp, NULL, offset); \
410static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
411 show_temp_max, store_temp_max, offset); \
412static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
413 show_temp_max_hyst, store_temp_max_hyst, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415sysfs_temp_offsets(1);
416sysfs_temp_offsets(2);
417sysfs_temp_offsets(3);
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400420show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct w83781d_data *data = w83781d_update_device(dev);
423 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
424}
425
Jim Cromie311ce2e2006-09-24 21:22:52 +0200426static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400429show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Jean Delvare90d66192007-10-08 18:24:35 +0200431 struct w83781d_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return sprintf(buf, "%ld\n", (long) data->vrm);
433}
434
435static ssize_t
Guenter Roeckc531eb32012-01-15 09:19:16 -0800436store_vrm_reg(struct device *dev, struct device_attribute *attr,
437 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Jean Delvare7666c132007-05-08 17:22:02 +0200439 struct w83781d_data *data = dev_get_drvdata(dev);
Guenter Roeckc531eb32012-01-15 09:19:16 -0800440 unsigned long val;
441 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Guenter Roeckc531eb32012-01-15 09:19:16 -0800443 err = kstrtoul(buf, 10, &val);
444 if (err)
445 return err;
446 data->vrm = SENSORS_LIMIT(val, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 return count;
449}
450
Jim Cromie311ce2e2006-09-24 21:22:52 +0200451static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400454show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 struct w83781d_data *data = w83781d_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200457 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Jim Cromie311ce2e2006-09-24 21:22:52 +0200460static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
461
Jean Delvare7d4a1372007-10-08 18:29:43 +0200462static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
463 char *buf)
464{
465 struct w83781d_data *data = w83781d_update_device(dev);
466 int bitnr = to_sensor_dev_attr(attr)->index;
467 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
468}
469
470/* The W83781D has a single alarm bit for temp2 and temp3 */
471static ssize_t show_temp3_alarm(struct device *dev,
472 struct device_attribute *attr, char *buf)
473{
474 struct w83781d_data *data = w83781d_update_device(dev);
475 int bitnr = (data->type == w83781d) ? 5 : 13;
476 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
477}
478
479static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
480static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
481static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
482static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
483static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
484static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
485static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10);
486static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16);
487static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17);
488static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
489static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
490static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
491static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
492static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
493static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_temp3_alarm, NULL, 0);
494
Guenter Roeckc531eb32012-01-15 09:19:16 -0800495static ssize_t show_beep_mask(struct device *dev,
496 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct w83781d_data *data = w83781d_update_device(dev);
499 return sprintf(buf, "%ld\n",
500 (long)BEEP_MASK_FROM_REG(data->beep_mask, data->type));
501}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200504store_beep_mask(struct device *dev, struct device_attribute *attr,
505 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Jean Delvare7666c132007-05-08 17:22:02 +0200507 struct w83781d_data *data = dev_get_drvdata(dev);
Guenter Roeckc531eb32012-01-15 09:19:16 -0800508 unsigned long val;
509 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Guenter Roeckc531eb32012-01-15 09:19:16 -0800511 err = kstrtoul(buf, 10, &val);
512 if (err)
513 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100515 mutex_lock(&data->update_lock);
Jean Delvare2fbbbf12008-10-17 17:51:18 +0200516 data->beep_mask &= 0x8000; /* preserve beep enable */
517 data->beep_mask |= BEEP_MASK_TO_REG(val, data->type);
Jean Delvare34875332007-05-08 17:22:03 +0200518 w83781d_write_value(data, W83781D_REG_BEEP_INTS1,
519 data->beep_mask & 0xff);
Jean Delvare31b8dc42007-05-08 17:22:03 +0200520 w83781d_write_value(data, W83781D_REG_BEEP_INTS2,
Jean Delvare2fbbbf12008-10-17 17:51:18 +0200521 (data->beep_mask >> 8) & 0xff);
Jean Delvare34875332007-05-08 17:22:03 +0200522 if (data->type != w83781d && data->type != as99127f) {
523 w83781d_write_value(data, W83781D_REG_BEEP_INTS3,
524 ((data->beep_mask) >> 16) & 0xff);
525 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100526 mutex_unlock(&data->update_lock);
Jean Delvare34875332007-05-08 17:22:03 +0200527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return count;
529}
530
Jean Delvare34875332007-05-08 17:22:03 +0200531static DEVICE_ATTR(beep_mask, S_IRUGO | S_IWUSR,
532 show_beep_mask, store_beep_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Jean Delvare7d4a1372007-10-08 18:29:43 +0200534static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
535 char *buf)
536{
537 struct w83781d_data *data = w83781d_update_device(dev);
538 int bitnr = to_sensor_dev_attr(attr)->index;
539 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
540}
541
542static ssize_t
543store_beep(struct device *dev, struct device_attribute *attr,
544 const char *buf, size_t count)
545{
546 struct w83781d_data *data = dev_get_drvdata(dev);
547 int bitnr = to_sensor_dev_attr(attr)->index;
Jean Delvare7d4a1372007-10-08 18:29:43 +0200548 u8 reg;
Guenter Roeckc531eb32012-01-15 09:19:16 -0800549 unsigned long bit;
550 int err;
Jean Delvare7d4a1372007-10-08 18:29:43 +0200551
Guenter Roeckc531eb32012-01-15 09:19:16 -0800552 err = kstrtoul(buf, 10, &bit);
553 if (err)
554 return err;
555
Jean Delvare7d4a1372007-10-08 18:29:43 +0200556 if (bit & ~1)
557 return -EINVAL;
558
559 mutex_lock(&data->update_lock);
560 if (bit)
561 data->beep_mask |= (1 << bitnr);
562 else
563 data->beep_mask &= ~(1 << bitnr);
564
565 if (bitnr < 8) {
566 reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS1);
567 if (bit)
568 reg |= (1 << bitnr);
569 else
570 reg &= ~(1 << bitnr);
571 w83781d_write_value(data, W83781D_REG_BEEP_INTS1, reg);
572 } else if (bitnr < 16) {
573 reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS2);
574 if (bit)
575 reg |= (1 << (bitnr - 8));
576 else
577 reg &= ~(1 << (bitnr - 8));
578 w83781d_write_value(data, W83781D_REG_BEEP_INTS2, reg);
579 } else {
580 reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS3);
581 if (bit)
582 reg |= (1 << (bitnr - 16));
583 else
584 reg &= ~(1 << (bitnr - 16));
585 w83781d_write_value(data, W83781D_REG_BEEP_INTS3, reg);
586 }
587 mutex_unlock(&data->update_lock);
588
589 return count;
590}
591
592/* The W83781D has a single beep bit for temp2 and temp3 */
593static ssize_t show_temp3_beep(struct device *dev,
594 struct device_attribute *attr, char *buf)
595{
596 struct w83781d_data *data = w83781d_update_device(dev);
597 int bitnr = (data->type == w83781d) ? 5 : 13;
598 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
599}
600
601static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
602 show_beep, store_beep, 0);
603static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO | S_IWUSR,
604 show_beep, store_beep, 1);
605static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO | S_IWUSR,
606 show_beep, store_beep, 2);
607static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO | S_IWUSR,
608 show_beep, store_beep, 3);
609static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO | S_IWUSR,
610 show_beep, store_beep, 8);
611static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO | S_IWUSR,
612 show_beep, store_beep, 9);
613static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO | S_IWUSR,
614 show_beep, store_beep, 10);
615static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO | S_IWUSR,
616 show_beep, store_beep, 16);
617static SENSOR_DEVICE_ATTR(in8_beep, S_IRUGO | S_IWUSR,
618 show_beep, store_beep, 17);
619static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO | S_IWUSR,
620 show_beep, store_beep, 6);
621static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO | S_IWUSR,
622 show_beep, store_beep, 7);
623static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO | S_IWUSR,
624 show_beep, store_beep, 11);
625static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
626 show_beep, store_beep, 4);
627static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO | S_IWUSR,
628 show_beep, store_beep, 5);
629static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO,
630 show_temp3_beep, store_beep, 13);
Jean Delvare2fbbbf12008-10-17 17:51:18 +0200631static SENSOR_DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR,
632 show_beep, store_beep, 15);
Jean Delvare7d4a1372007-10-08 18:29:43 +0200633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200635show_fan_div(struct device *dev, struct device_attribute *da, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Jean Delvare34875332007-05-08 17:22:03 +0200637 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct w83781d_data *data = w83781d_update_device(dev);
639 return sprintf(buf, "%ld\n",
Jean Delvare34875332007-05-08 17:22:03 +0200640 (long) DIV_FROM_REG(data->fan_div[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Guenter Roeckaff6e002012-01-19 11:02:27 -0800643/*
644 * Note: we save and restore the fan minimum here, because its value is
645 * determined in part by the fan divisor. This follows the principle of
646 * least surprise; the user doesn't expect the fan minimum to change just
647 * because the divisor changed.
648 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200650store_fan_div(struct device *dev, struct device_attribute *da,
651 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Jean Delvare34875332007-05-08 17:22:03 +0200653 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Jean Delvare7666c132007-05-08 17:22:02 +0200654 struct w83781d_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 unsigned long min;
Jean Delvare34875332007-05-08 17:22:03 +0200656 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 u8 reg;
Guenter Roeckc531eb32012-01-15 09:19:16 -0800658 unsigned long val;
659 int err;
660
661 err = kstrtoul(buf, 10, &val);
662 if (err)
663 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100665 mutex_lock(&data->update_lock);
Jean Delvare293c0992007-11-30 23:52:44 +0100666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /* Save fan_min */
668 min = FAN_FROM_REG(data->fan_min[nr],
669 DIV_FROM_REG(data->fan_div[nr]));
670
671 data->fan_div[nr] = DIV_TO_REG(val, data->type);
672
Guenter Roeckc531eb32012-01-15 09:19:16 -0800673 reg = (w83781d_read_value(data, nr == 2 ?
674 W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
675 & (nr == 0 ? 0xcf : 0x3f))
676 | ((data->fan_div[nr] & 0x03) << (nr == 0 ? 4 : 6));
677 w83781d_write_value(data, nr == 2 ?
678 W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 /* w83781d and as99127f don't have extended divisor bits */
681 if (data->type != w83781d && data->type != as99127f) {
Jean Delvare31b8dc42007-05-08 17:22:03 +0200682 reg = (w83781d_read_value(data, W83781D_REG_VBAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 & ~(1 << (5 + nr)))
684 | ((data->fan_div[nr] & 0x04) << (3 + nr));
Jean Delvare31b8dc42007-05-08 17:22:03 +0200685 w83781d_write_value(data, W83781D_REG_VBAT, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687
688 /* Restore fan_min */
689 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvare34875332007-05-08 17:22:03 +0200690 w83781d_write_value(data, W83781D_REG_FAN_MIN(nr), data->fan_min[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100692 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return count;
694}
695
Jean Delvare34875332007-05-08 17:22:03 +0200696static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
697 show_fan_div, store_fan_div, 0);
698static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
699 show_fan_div, store_fan_div, 1);
700static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR,
701 show_fan_div, store_fan_div, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200704show_pwm(struct device *dev, struct device_attribute *da, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Jean Delvare34875332007-05-08 17:22:03 +0200706 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 struct w83781d_data *data = w83781d_update_device(dev);
Jean Delvare34875332007-05-08 17:22:03 +0200708 return sprintf(buf, "%d\n", (int)data->pwm[attr->index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
711static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200712show_pwm2_enable(struct device *dev, struct device_attribute *da, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct w83781d_data *data = w83781d_update_device(dev);
Jean Delvare34875332007-05-08 17:22:03 +0200715 return sprintf(buf, "%d\n", (int)data->pwm2_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200719store_pwm(struct device *dev, struct device_attribute *da, const char *buf,
720 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Jean Delvare34875332007-05-08 17:22:03 +0200722 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Jean Delvare7666c132007-05-08 17:22:02 +0200723 struct w83781d_data *data = dev_get_drvdata(dev);
Jean Delvare34875332007-05-08 17:22:03 +0200724 int nr = attr->index;
Guenter Roeckc531eb32012-01-15 09:19:16 -0800725 unsigned long val;
726 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Guenter Roeckc531eb32012-01-15 09:19:16 -0800728 err = kstrtoul(buf, 10, &val);
729 if (err)
730 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100732 mutex_lock(&data->update_lock);
Jean Delvare34875332007-05-08 17:22:03 +0200733 data->pwm[nr] = SENSORS_LIMIT(val, 0, 255);
734 w83781d_write_value(data, W83781D_REG_PWM[nr], data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100735 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return count;
737}
738
739static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200740store_pwm2_enable(struct device *dev, struct device_attribute *da,
741 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Jean Delvare7666c132007-05-08 17:22:02 +0200743 struct w83781d_data *data = dev_get_drvdata(dev);
Guenter Roeckc531eb32012-01-15 09:19:16 -0800744 unsigned long val;
745 u32 reg;
746 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Guenter Roeckc531eb32012-01-15 09:19:16 -0800748 err = kstrtoul(buf, 10, &val);
749 if (err)
750 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100752 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 switch (val) {
755 case 0:
756 case 1:
Jean Delvare31b8dc42007-05-08 17:22:03 +0200757 reg = w83781d_read_value(data, W83781D_REG_PWMCLK12);
758 w83781d_write_value(data, W83781D_REG_PWMCLK12,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 (reg & 0xf7) | (val << 3));
760
Jean Delvare31b8dc42007-05-08 17:22:03 +0200761 reg = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG);
762 w83781d_write_value(data, W83781D_REG_BEEP_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 (reg & 0xef) | (!val << 4));
764
Jean Delvare34875332007-05-08 17:22:03 +0200765 data->pwm2_enable = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 break;
767
768 default:
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100769 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return -EINVAL;
771 }
772
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100773 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return count;
775}
776
Jean Delvare34875332007-05-08 17:22:03 +0200777static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 0);
778static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 1);
779static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 2);
780static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 3);
781/* only PWM2 can be enabled/disabled */
782static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
783 show_pwm2_enable, store_pwm2_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200786show_sensor(struct device *dev, struct device_attribute *da, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Jean Delvare34875332007-05-08 17:22:03 +0200788 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 struct w83781d_data *data = w83781d_update_device(dev);
Jean Delvare34875332007-05-08 17:22:03 +0200790 return sprintf(buf, "%d\n", (int)data->sens[attr->index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
793static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200794store_sensor(struct device *dev, struct device_attribute *da,
795 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Jean Delvare34875332007-05-08 17:22:03 +0200797 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Jean Delvare7666c132007-05-08 17:22:02 +0200798 struct w83781d_data *data = dev_get_drvdata(dev);
Jean Delvare34875332007-05-08 17:22:03 +0200799 int nr = attr->index;
Guenter Roeckc531eb32012-01-15 09:19:16 -0800800 unsigned long val;
801 u32 tmp;
802 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Guenter Roeckc531eb32012-01-15 09:19:16 -0800804 err = kstrtoul(buf, 10, &val);
805 if (err)
806 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100808 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 switch (val) {
811 case 1: /* PII/Celeron diode */
Jean Delvare31b8dc42007-05-08 17:22:03 +0200812 tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
813 w83781d_write_value(data, W83781D_REG_SCFG1,
Jean Delvare34875332007-05-08 17:22:03 +0200814 tmp | BIT_SCFG1[nr]);
Jean Delvare31b8dc42007-05-08 17:22:03 +0200815 tmp = w83781d_read_value(data, W83781D_REG_SCFG2);
816 w83781d_write_value(data, W83781D_REG_SCFG2,
Jean Delvare34875332007-05-08 17:22:03 +0200817 tmp | BIT_SCFG2[nr]);
818 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 break;
820 case 2: /* 3904 */
Jean Delvare31b8dc42007-05-08 17:22:03 +0200821 tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
822 w83781d_write_value(data, W83781D_REG_SCFG1,
Jean Delvare34875332007-05-08 17:22:03 +0200823 tmp | BIT_SCFG1[nr]);
Jean Delvare31b8dc42007-05-08 17:22:03 +0200824 tmp = w83781d_read_value(data, W83781D_REG_SCFG2);
825 w83781d_write_value(data, W83781D_REG_SCFG2,
Jean Delvare34875332007-05-08 17:22:03 +0200826 tmp & ~BIT_SCFG2[nr]);
827 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 break;
Jean Delvareb26f9332007-08-16 14:30:01 +0200829 case W83781D_DEFAULT_BETA:
830 dev_warn(dev, "Sensor type %d is deprecated, please use 4 "
831 "instead\n", W83781D_DEFAULT_BETA);
832 /* fall through */
833 case 4: /* thermistor */
Jean Delvare31b8dc42007-05-08 17:22:03 +0200834 tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
835 w83781d_write_value(data, W83781D_REG_SCFG1,
Jean Delvare34875332007-05-08 17:22:03 +0200836 tmp & ~BIT_SCFG1[nr]);
837 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 break;
839 default:
Jean Delvareb26f9332007-08-16 14:30:01 +0200840 dev_err(dev, "Invalid sensor type %ld; must be 1, 2, or 4\n",
841 (long) val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 break;
843 }
844
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100845 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return count;
847}
848
Jean Delvare34875332007-05-08 17:22:03 +0200849static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR,
850 show_sensor, store_sensor, 0);
851static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR,
Mark M. Hoffman393cdad2007-08-09 08:12:46 -0400852 show_sensor, store_sensor, 1);
Jean Delvare34875332007-05-08 17:22:03 +0200853static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR,
Mark M. Hoffman393cdad2007-08-09 08:12:46 -0400854 show_sensor, store_sensor, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Guenter Roeckaff6e002012-01-19 11:02:27 -0800856/*
857 * Assumes that adapter is of I2C, not ISA variety.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 * OTHERWISE DON'T CALL THIS
859 */
860static int
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200861w83781d_detect_subclients(struct i2c_client *new_client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 int i, val1 = 0, id;
864 int err;
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200865 int address = new_client->addr;
866 unsigned short sc_addr[2];
867 struct i2c_adapter *adapter = new_client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 struct w83781d_data *data = i2c_get_clientdata(new_client);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200869 enum chips kind = data->type;
Guenter Roeckbbc8a562012-06-12 08:27:49 -0700870 int num_sc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 id = i2c_adapter_id(adapter);
873
874 if (force_subclients[0] == id && force_subclients[1] == address) {
875 for (i = 2; i <= 3; i++) {
876 if (force_subclients[i] < 0x48 ||
877 force_subclients[i] > 0x4f) {
878 dev_err(&new_client->dev, "Invalid subclient "
879 "address %d; must be 0x48-0x4f\n",
880 force_subclients[i]);
881 err = -EINVAL;
882 goto ERROR_SC_1;
883 }
884 }
Jean Delvare31b8dc42007-05-08 17:22:03 +0200885 w83781d_write_value(data, W83781D_REG_I2C_SUBADDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 (force_subclients[2] & 0x07) |
887 ((force_subclients[3] & 0x07) << 4));
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200888 sc_addr[0] = force_subclients[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 } else {
Jean Delvare31b8dc42007-05-08 17:22:03 +0200890 val1 = w83781d_read_value(data, W83781D_REG_I2C_SUBADDR);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200891 sc_addr[0] = 0x48 + (val1 & 0x07);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893
894 if (kind != w83783s) {
Guenter Roeckbbc8a562012-06-12 08:27:49 -0700895 num_sc = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (force_subclients[0] == id &&
897 force_subclients[1] == address) {
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200898 sc_addr[1] = force_subclients[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 } else {
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200900 sc_addr[1] = 0x48 + ((val1 >> 4) & 0x07);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200902 if (sc_addr[0] == sc_addr[1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 dev_err(&new_client->dev,
904 "Duplicate addresses 0x%x for subclients.\n",
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200905 sc_addr[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 err = -EBUSY;
907 goto ERROR_SC_2;
908 }
909 }
910
Guenter Roeckbbc8a562012-06-12 08:27:49 -0700911 for (i = 0; i < num_sc; i++) {
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200912 data->lm75[i] = i2c_new_dummy(adapter, sc_addr[i]);
913 if (!data->lm75[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 dev_err(&new_client->dev, "Subclient %d "
915 "registration at address 0x%x "
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200916 "failed.\n", i, sc_addr[i]);
917 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (i == 1)
919 goto ERROR_SC_3;
920 goto ERROR_SC_2;
921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923
924 return 0;
925
926/* Undo inits in case of errors */
927ERROR_SC_3:
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200928 i2c_unregister_device(data->lm75[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929ERROR_SC_2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930ERROR_SC_1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return err;
932}
933
Jean Delvare34875332007-05-08 17:22:03 +0200934#define IN_UNIT_ATTRS(X) \
935 &sensor_dev_attr_in##X##_input.dev_attr.attr, \
936 &sensor_dev_attr_in##X##_min.dev_attr.attr, \
Jean Delvare293c0992007-11-30 23:52:44 +0100937 &sensor_dev_attr_in##X##_max.dev_attr.attr, \
Jean Delvare7d4a1372007-10-08 18:29:43 +0200938 &sensor_dev_attr_in##X##_alarm.dev_attr.attr, \
939 &sensor_dev_attr_in##X##_beep.dev_attr.attr
Jim Cromie311ce2e2006-09-24 21:22:52 +0200940
Jean Delvare34875332007-05-08 17:22:03 +0200941#define FAN_UNIT_ATTRS(X) \
942 &sensor_dev_attr_fan##X##_input.dev_attr.attr, \
943 &sensor_dev_attr_fan##X##_min.dev_attr.attr, \
Jean Delvare7d4a1372007-10-08 18:29:43 +0200944 &sensor_dev_attr_fan##X##_div.dev_attr.attr, \
945 &sensor_dev_attr_fan##X##_alarm.dev_attr.attr, \
946 &sensor_dev_attr_fan##X##_beep.dev_attr.attr
Jim Cromie311ce2e2006-09-24 21:22:52 +0200947
Jean Delvare34875332007-05-08 17:22:03 +0200948#define TEMP_UNIT_ATTRS(X) \
949 &sensor_dev_attr_temp##X##_input.dev_attr.attr, \
950 &sensor_dev_attr_temp##X##_max.dev_attr.attr, \
Jean Delvare7d4a1372007-10-08 18:29:43 +0200951 &sensor_dev_attr_temp##X##_max_hyst.dev_attr.attr, \
952 &sensor_dev_attr_temp##X##_alarm.dev_attr.attr, \
953 &sensor_dev_attr_temp##X##_beep.dev_attr.attr
Jim Cromie311ce2e2006-09-24 21:22:52 +0200954
Guenter Roeckc531eb32012-01-15 09:19:16 -0800955static struct attribute *w83781d_attributes[] = {
Jim Cromie311ce2e2006-09-24 21:22:52 +0200956 IN_UNIT_ATTRS(0),
957 IN_UNIT_ATTRS(2),
958 IN_UNIT_ATTRS(3),
959 IN_UNIT_ATTRS(4),
960 IN_UNIT_ATTRS(5),
961 IN_UNIT_ATTRS(6),
962 FAN_UNIT_ATTRS(1),
963 FAN_UNIT_ATTRS(2),
964 FAN_UNIT_ATTRS(3),
965 TEMP_UNIT_ATTRS(1),
966 TEMP_UNIT_ATTRS(2),
967 &dev_attr_cpu0_vid.attr,
968 &dev_attr_vrm.attr,
969 &dev_attr_alarms.attr,
970 &dev_attr_beep_mask.attr,
Jean Delvare2fbbbf12008-10-17 17:51:18 +0200971 &sensor_dev_attr_beep_enable.dev_attr.attr,
Jim Cromie311ce2e2006-09-24 21:22:52 +0200972 NULL
973};
974static const struct attribute_group w83781d_group = {
975 .attrs = w83781d_attributes,
976};
977
Guenter Roeck79501332012-01-16 20:45:16 -0800978static struct attribute *w83781d_attributes_in1[] = {
Jim Cromie311ce2e2006-09-24 21:22:52 +0200979 IN_UNIT_ATTRS(1),
Guenter Roeck79501332012-01-16 20:45:16 -0800980 NULL
981};
982static const struct attribute_group w83781d_group_in1 = {
983 .attrs = w83781d_attributes_in1,
984};
985
986static struct attribute *w83781d_attributes_in78[] = {
Jim Cromie311ce2e2006-09-24 21:22:52 +0200987 IN_UNIT_ATTRS(7),
988 IN_UNIT_ATTRS(8),
Guenter Roeck79501332012-01-16 20:45:16 -0800989 NULL
990};
991static const struct attribute_group w83781d_group_in78 = {
992 .attrs = w83781d_attributes_in78,
993};
994
995static struct attribute *w83781d_attributes_temp3[] = {
Jim Cromie311ce2e2006-09-24 21:22:52 +0200996 TEMP_UNIT_ATTRS(3),
Guenter Roeck79501332012-01-16 20:45:16 -0800997 NULL
998};
999static const struct attribute_group w83781d_group_temp3 = {
1000 .attrs = w83781d_attributes_temp3,
1001};
1002
1003static struct attribute *w83781d_attributes_pwm12[] = {
Jean Delvare34875332007-05-08 17:22:03 +02001004 &sensor_dev_attr_pwm1.dev_attr.attr,
1005 &sensor_dev_attr_pwm2.dev_attr.attr,
Guenter Roeck79501332012-01-16 20:45:16 -08001006 &dev_attr_pwm2_enable.attr,
1007 NULL
1008};
1009static const struct attribute_group w83781d_group_pwm12 = {
1010 .attrs = w83781d_attributes_pwm12,
1011};
1012
1013static struct attribute *w83781d_attributes_pwm34[] = {
Jean Delvare34875332007-05-08 17:22:03 +02001014 &sensor_dev_attr_pwm3.dev_attr.attr,
1015 &sensor_dev_attr_pwm4.dev_attr.attr,
Guenter Roeck79501332012-01-16 20:45:16 -08001016 NULL
1017};
1018static const struct attribute_group w83781d_group_pwm34 = {
1019 .attrs = w83781d_attributes_pwm34,
1020};
1021
1022static struct attribute *w83781d_attributes_other[] = {
Jean Delvare34875332007-05-08 17:22:03 +02001023 &sensor_dev_attr_temp1_type.dev_attr.attr,
1024 &sensor_dev_attr_temp2_type.dev_attr.attr,
1025 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jim Cromie311ce2e2006-09-24 21:22:52 +02001026 NULL
1027};
Guenter Roeck79501332012-01-16 20:45:16 -08001028static const struct attribute_group w83781d_group_other = {
1029 .attrs = w83781d_attributes_other,
Jim Cromie311ce2e2006-09-24 21:22:52 +02001030};
1031
Jean Delvare7666c132007-05-08 17:22:02 +02001032/* No clean up is done on error, it's up to the caller */
1033static int
1034w83781d_create_files(struct device *dev, int kind, int is_isa)
1035{
1036 int err;
1037
Guenter Roeckc531eb32012-01-15 09:19:16 -08001038 err = sysfs_create_group(&dev->kobj, &w83781d_group);
1039 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001040 return err;
1041
1042 if (kind != w83783s) {
Guenter Roeck79501332012-01-16 20:45:16 -08001043 err = sysfs_create_group(&dev->kobj, &w83781d_group_in1);
1044 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001045 return err;
1046 }
1047 if (kind != as99127f && kind != w83781d && kind != w83783s) {
Guenter Roeck79501332012-01-16 20:45:16 -08001048 err = sysfs_create_group(&dev->kobj, &w83781d_group_in78);
1049 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001050 return err;
1051 }
1052 if (kind != w83783s) {
Guenter Roeck79501332012-01-16 20:45:16 -08001053 err = sysfs_create_group(&dev->kobj, &w83781d_group_temp3);
1054 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001055 return err;
Jean Delvare7d4a1372007-10-08 18:29:43 +02001056
Jean Delvare7768aa72007-10-25 13:11:01 +02001057 if (kind != w83781d) {
Jean Delvare7d4a1372007-10-08 18:29:43 +02001058 err = sysfs_chmod_file(&dev->kobj,
1059 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1060 S_IRUGO | S_IWUSR);
1061 if (err)
1062 return err;
Jean Delvare7768aa72007-10-25 13:11:01 +02001063 }
Jean Delvare7666c132007-05-08 17:22:02 +02001064 }
1065
1066 if (kind != w83781d && kind != as99127f) {
Guenter Roeck79501332012-01-16 20:45:16 -08001067 err = sysfs_create_group(&dev->kobj, &w83781d_group_pwm12);
1068 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001069 return err;
1070 }
1071 if (kind == w83782d && !is_isa) {
Guenter Roeck79501332012-01-16 20:45:16 -08001072 err = sysfs_create_group(&dev->kobj, &w83781d_group_pwm34);
1073 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001074 return err;
1075 }
1076
1077 if (kind != as99127f && kind != w83781d) {
Guenter Roeck79501332012-01-16 20:45:16 -08001078 err = device_create_file(dev,
1079 &sensor_dev_attr_temp1_type.dev_attr);
1080 if (err)
1081 return err;
1082 err = device_create_file(dev,
1083 &sensor_dev_attr_temp2_type.dev_attr);
1084 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001085 return err;
1086 if (kind != w83783s) {
Guenter Roeckc531eb32012-01-15 09:19:16 -08001087 err = device_create_file(dev,
Guenter Roeck79501332012-01-16 20:45:16 -08001088 &sensor_dev_attr_temp3_type.dev_attr);
Guenter Roeckc531eb32012-01-15 09:19:16 -08001089 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001090 return err;
1091 }
1092 }
1093
Jean Delvare7666c132007-05-08 17:22:02 +02001094 return 0;
1095}
1096
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001097/* Return 0 if detection is successful, -ENODEV otherwise */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098static int
Jean Delvare310ec792009-12-14 21:17:23 +01001099w83781d_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
Jean Delvarebab2bf42009-12-09 20:35:54 +01001101 int val1, val2;
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001102 struct w83781d_data *isa = w83781d_data_if_isa();
1103 struct i2c_adapter *adapter = client->adapter;
1104 int address = client->addr;
Jean Delvarebab2bf42009-12-09 20:35:54 +01001105 const char *client_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 enum vendor { winbond, asus } vendid;
1107
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001108 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1109 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Guenter Roeckaff6e002012-01-19 11:02:27 -08001111 /*
1112 * We block updates of the ISA device to minimize the risk of
1113 * concurrent access to the same W83781D chip through different
1114 * interfaces.
1115 */
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001116 if (isa)
1117 mutex_lock(&isa->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Jean Delvarebab2bf42009-12-09 20:35:54 +01001119 if (i2c_smbus_read_byte_data(client, W83781D_REG_CONFIG) & 0x80) {
1120 dev_dbg(&adapter->dev,
1121 "Detection of w83781d chip failed at step 3\n");
1122 goto err_nodev;
1123 }
1124
1125 val1 = i2c_smbus_read_byte_data(client, W83781D_REG_BANK);
1126 val2 = i2c_smbus_read_byte_data(client, W83781D_REG_CHIPMAN);
1127 /* Check for Winbond or Asus ID if in bank 0 */
1128 if (!(val1 & 0x07) &&
1129 ((!(val1 & 0x80) && val2 != 0xa3 && val2 != 0xc3) ||
Guenter Roeckc531eb32012-01-15 09:19:16 -08001130 ((val1 & 0x80) && val2 != 0x5c && val2 != 0x12))) {
Jean Delvarebab2bf42009-12-09 20:35:54 +01001131 dev_dbg(&adapter->dev,
1132 "Detection of w83781d chip failed at step 4\n");
1133 goto err_nodev;
1134 }
Guenter Roeckaff6e002012-01-19 11:02:27 -08001135 /*
1136 * If Winbond SMBus, check address at 0x48.
1137 * Asus doesn't support, except for as99127f rev.2
1138 */
Jean Delvarebab2bf42009-12-09 20:35:54 +01001139 if ((!(val1 & 0x80) && val2 == 0xa3) ||
Guenter Roeckc531eb32012-01-15 09:19:16 -08001140 ((val1 & 0x80) && val2 == 0x5c)) {
Jean Delvarebab2bf42009-12-09 20:35:54 +01001141 if (i2c_smbus_read_byte_data(client, W83781D_REG_I2C_ADDR)
1142 != address) {
1143 dev_dbg(&adapter->dev,
1144 "Detection of w83781d chip failed at step 5\n");
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001145 goto err_nodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148
Jean Delvarebab2bf42009-12-09 20:35:54 +01001149 /* Put it now into bank 0 and Vendor ID High Byte */
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001150 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1151 (i2c_smbus_read_byte_data(client, W83781D_REG_BANK)
1152 & 0x78) | 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Jean Delvarebab2bf42009-12-09 20:35:54 +01001154 /* Get the vendor ID */
1155 val2 = i2c_smbus_read_byte_data(client, W83781D_REG_CHIPMAN);
1156 if (val2 == 0x5c)
1157 vendid = winbond;
1158 else if (val2 == 0x12)
1159 vendid = asus;
1160 else {
1161 dev_dbg(&adapter->dev,
1162 "w83781d chip vendor is neither Winbond nor Asus\n");
1163 goto err_nodev;
1164 }
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 /* Determine the chip type. */
Jean Delvarebab2bf42009-12-09 20:35:54 +01001167 val1 = i2c_smbus_read_byte_data(client, W83781D_REG_WCHIPID);
1168 if ((val1 == 0x10 || val1 == 0x11) && vendid == winbond)
1169 client_name = "w83781d";
1170 else if (val1 == 0x30 && vendid == winbond)
1171 client_name = "w83782d";
1172 else if (val1 == 0x40 && vendid == winbond && address == 0x2d)
1173 client_name = "w83783s";
1174 else if (val1 == 0x31)
1175 client_name = "as99127f";
1176 else
1177 goto err_nodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Jean Delvarebab2bf42009-12-09 20:35:54 +01001179 if (val1 <= 0x30 && w83781d_alias_detect(client, val1)) {
1180 dev_dbg(&adapter->dev, "Device at 0x%02x appears to "
1181 "be the same as ISA device\n", address);
1182 goto err_nodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001185 if (isa)
1186 mutex_unlock(&isa->update_lock);
1187
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001188 strlcpy(info->type, client_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001190 return 0;
1191
1192 err_nodev:
1193 if (isa)
1194 mutex_unlock(&isa->update_lock);
1195 return -ENODEV;
1196}
1197
Guenter Roeck79501332012-01-16 20:45:16 -08001198static void w83781d_remove_files(struct device *dev)
1199{
1200 sysfs_remove_group(&dev->kobj, &w83781d_group);
1201 sysfs_remove_group(&dev->kobj, &w83781d_group_in1);
1202 sysfs_remove_group(&dev->kobj, &w83781d_group_in78);
1203 sysfs_remove_group(&dev->kobj, &w83781d_group_temp3);
1204 sysfs_remove_group(&dev->kobj, &w83781d_group_pwm12);
1205 sysfs_remove_group(&dev->kobj, &w83781d_group_pwm34);
1206 sysfs_remove_group(&dev->kobj, &w83781d_group_other);
1207}
1208
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001209static int
1210w83781d_probe(struct i2c_client *client, const struct i2c_device_id *id)
1211{
1212 struct device *dev = &client->dev;
1213 struct w83781d_data *data;
1214 int err;
1215
Guenter Roeck144d2b92012-06-02 11:48:00 -07001216 data = devm_kzalloc(dev, sizeof(struct w83781d_data), GFP_KERNEL);
1217 if (!data)
1218 return -ENOMEM;
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001219
1220 i2c_set_clientdata(client, data);
1221 mutex_init(&data->lock);
1222 mutex_init(&data->update_lock);
1223
1224 data->type = id->driver_data;
1225 data->client = client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 /* attach secondary i2c lm75-like clients */
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001228 err = w83781d_detect_subclients(client);
1229 if (err)
Guenter Roeck144d2b92012-06-02 11:48:00 -07001230 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 /* Initialize the chip */
Jean Delvare7666c132007-05-08 17:22:02 +02001233 w83781d_init_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 /* Register sysfs hooks */
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001236 err = w83781d_create_files(dev, data->type, 0);
Jean Delvare7666c132007-05-08 17:22:02 +02001237 if (err)
Guenter Roeck144d2b92012-06-02 11:48:00 -07001238 goto exit_remove_files;
Jim Cromie311ce2e2006-09-24 21:22:52 +02001239
Tony Jones1beeffe2007-08-20 13:46:20 -07001240 data->hwmon_dev = hwmon_device_register(dev);
1241 if (IS_ERR(data->hwmon_dev)) {
1242 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck144d2b92012-06-02 11:48:00 -07001243 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001244 }
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return 0;
1247
Guenter Roeck144d2b92012-06-02 11:48:00 -07001248 exit_remove_files:
Guenter Roeck79501332012-01-16 20:45:16 -08001249 w83781d_remove_files(dev);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001250 if (data->lm75[0])
1251 i2c_unregister_device(data->lm75[0]);
1252 if (data->lm75[1])
1253 i2c_unregister_device(data->lm75[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return err;
1255}
1256
1257static int
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001258w83781d_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001260 struct w83781d_data *data = i2c_get_clientdata(client);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001261 struct device *dev = &client->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001263 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeck79501332012-01-16 20:45:16 -08001264 w83781d_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001266 if (data->lm75[0])
1267 i2c_unregister_device(data->lm75[0]);
1268 if (data->lm75[1])
1269 i2c_unregister_device(data->lm75[1]);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return 0;
1272}
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274static int
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001275w83781d_read_value_i2c(struct w83781d_data *data, u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001277 struct i2c_client *client = data->client;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001278 int res, bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 struct i2c_client *cl;
1280
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001281 bank = (reg >> 8) & 0x0f;
1282 if (bank > 2)
1283 /* switch banks */
1284 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1285 bank);
1286 if (bank == 0 || bank > 2) {
1287 res = i2c_smbus_read_byte_data(client, reg & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 } else {
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001289 /* switch to subclient */
1290 cl = data->lm75[bank - 1];
1291 /* convert from ISA to LM75 I2C addresses */
1292 switch (reg & 0xff) {
1293 case 0x50: /* TEMP */
Jean Delvare90f41022011-11-04 12:00:47 +01001294 res = i2c_smbus_read_word_swapped(cl, 0);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001295 break;
1296 case 0x52: /* CONFIG */
1297 res = i2c_smbus_read_byte_data(cl, 1);
1298 break;
1299 case 0x53: /* HYST */
Jean Delvare90f41022011-11-04 12:00:47 +01001300 res = i2c_smbus_read_word_swapped(cl, 2);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001301 break;
1302 case 0x55: /* OVER */
1303 default:
Jean Delvare90f41022011-11-04 12:00:47 +01001304 res = i2c_smbus_read_word_swapped(cl, 3);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001305 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001308 if (bank > 2)
1309 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return res;
1312}
1313
1314static int
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001315w83781d_write_value_i2c(struct w83781d_data *data, u16 reg, u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001317 struct i2c_client *client = data->client;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001318 int bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 struct i2c_client *cl;
1320
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001321 bank = (reg >> 8) & 0x0f;
1322 if (bank > 2)
1323 /* switch banks */
1324 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1325 bank);
1326 if (bank == 0 || bank > 2) {
1327 i2c_smbus_write_byte_data(client, reg & 0xff,
1328 value & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 } else {
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001330 /* switch to subclient */
1331 cl = data->lm75[bank - 1];
1332 /* convert from ISA to LM75 I2C addresses */
1333 switch (reg & 0xff) {
1334 case 0x52: /* CONFIG */
1335 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
1336 break;
1337 case 0x53: /* HYST */
Jean Delvare90f41022011-11-04 12:00:47 +01001338 i2c_smbus_write_word_swapped(cl, 2, value);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001339 break;
1340 case 0x55: /* OVER */
Jean Delvare90f41022011-11-04 12:00:47 +01001341 i2c_smbus_write_word_swapped(cl, 3, value);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001342 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001345 if (bank > 2)
1346 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return 0;
1349}
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351static void
Jean Delvare7666c132007-05-08 17:22:02 +02001352w83781d_init_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Jean Delvare7666c132007-05-08 17:22:02 +02001354 struct w83781d_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 int i, p;
1356 int type = data->type;
1357 u8 tmp;
1358
Guenter Roeckaff6e002012-01-19 11:02:27 -08001359 if (reset && type != as99127f) { /*
1360 * this resets registers we don't have
1361 * documentation for on the as99127f
1362 */
1363 /*
1364 * Resetting the chip has been the default for a long time,
1365 * but it causes the BIOS initializations (fan clock dividers,
1366 * thermal sensor types...) to be lost, so it is now optional.
1367 * It might even go away if nobody reports it as being useful,
1368 * as I see very little reason why this would be needed at
1369 * all.
1370 */
Jean Delvare7666c132007-05-08 17:22:02 +02001371 dev_info(dev, "If reset=1 solved a problem you were "
Jean Delvarefabddcd2006-02-05 23:26:51 +01001372 "having, please report!\n");
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /* save these registers */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001375 i = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG);
1376 p = w83781d_read_value(data, W83781D_REG_PWMCLK12);
Guenter Roeckaff6e002012-01-19 11:02:27 -08001377 /*
1378 * Reset all except Watchdog values and last conversion values
1379 * This sets fan-divs to 2, among others
1380 */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001381 w83781d_write_value(data, W83781D_REG_CONFIG, 0x80);
Guenter Roeckaff6e002012-01-19 11:02:27 -08001382 /*
1383 * Restore the registers and disable power-on abnormal beep.
1384 * This saves FAN 1/2/3 input/output values set by BIOS.
1385 */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001386 w83781d_write_value(data, W83781D_REG_BEEP_CONFIG, i | 0x80);
1387 w83781d_write_value(data, W83781D_REG_PWMCLK12, p);
Guenter Roeckc531eb32012-01-15 09:19:16 -08001388 /*
1389 * Disable master beep-enable (reset turns it on).
1390 * Individual beep_mask should be reset to off but for some
1391 * reason disabling this bit helps some people not get beeped
1392 */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001393 w83781d_write_value(data, W83781D_REG_BEEP_INTS2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
1395
Guenter Roeckaff6e002012-01-19 11:02:27 -08001396 /*
1397 * Disable power-on abnormal beep, as advised by the datasheet.
1398 * Already done if reset=1.
1399 */
Jean Delvarefabddcd2006-02-05 23:26:51 +01001400 if (init && !reset && type != as99127f) {
Jean Delvare31b8dc42007-05-08 17:22:03 +02001401 i = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG);
1402 w83781d_write_value(data, W83781D_REG_BEEP_CONFIG, i | 0x80);
Jean Delvarefabddcd2006-02-05 23:26:51 +01001403 }
1404
Jean Delvare303760b2005-07-31 21:52:01 +02001405 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 if ((type != w83781d) && (type != as99127f)) {
Jean Delvare31b8dc42007-05-08 17:22:03 +02001408 tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 for (i = 1; i <= 3; i++) {
1410 if (!(tmp & BIT_SCFG1[i - 1])) {
Jean Delvareb26f9332007-08-16 14:30:01 +02001411 data->sens[i - 1] = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 } else {
1413 if (w83781d_read_value
Jean Delvare31b8dc42007-05-08 17:22:03 +02001414 (data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1416 data->sens[i - 1] = 1;
1417 else
1418 data->sens[i - 1] = 2;
1419 }
Jean Delvare7c7a5302005-06-16 19:24:14 +02001420 if (type == w83783s && i == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 break;
1422 }
1423 }
1424
1425 if (init && type != as99127f) {
1426 /* Enable temp2 */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001427 tmp = w83781d_read_value(data, W83781D_REG_TEMP2_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (tmp & 0x01) {
Jean Delvare7666c132007-05-08 17:22:02 +02001429 dev_warn(dev, "Enabling temp2, readings "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 "might not make sense\n");
Jean Delvare31b8dc42007-05-08 17:22:03 +02001431 w83781d_write_value(data, W83781D_REG_TEMP2_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 tmp & 0xfe);
1433 }
1434
1435 /* Enable temp3 */
Jean Delvare7c7a5302005-06-16 19:24:14 +02001436 if (type != w83783s) {
Jean Delvare31b8dc42007-05-08 17:22:03 +02001437 tmp = w83781d_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 W83781D_REG_TEMP3_CONFIG);
1439 if (tmp & 0x01) {
Jean Delvare7666c132007-05-08 17:22:02 +02001440 dev_warn(dev, "Enabling temp3, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 "readings might not make sense\n");
Jean Delvare31b8dc42007-05-08 17:22:03 +02001442 w83781d_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 W83781D_REG_TEMP3_CONFIG, tmp & 0xfe);
1444 }
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447
1448 /* Start monitoring */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001449 w83781d_write_value(data, W83781D_REG_CONFIG,
1450 (w83781d_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 W83781D_REG_CONFIG) & 0xf7)
1452 | 0x01);
Jean Delvare7666c132007-05-08 17:22:02 +02001453
1454 /* A few vars need to be filled upon startup */
Jean Delvare34875332007-05-08 17:22:03 +02001455 for (i = 0; i < 3; i++) {
1456 data->fan_min[i] = w83781d_read_value(data,
Jean Delvare7666c132007-05-08 17:22:02 +02001457 W83781D_REG_FAN_MIN(i));
1458 }
Jean Delvare7666c132007-05-08 17:22:02 +02001459
1460 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
1463static struct w83781d_data *w83781d_update_device(struct device *dev)
1464{
Jean Delvare7666c132007-05-08 17:22:02 +02001465 struct w83781d_data *data = dev_get_drvdata(dev);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001466 struct i2c_client *client = data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 int i;
1468
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001469 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1472 || !data->valid) {
1473 dev_dbg(dev, "Starting device update\n");
1474
1475 for (i = 0; i <= 8; i++) {
Jean Delvare7c7a5302005-06-16 19:24:14 +02001476 if (data->type == w83783s && i == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 continue; /* 783S has no in1 */
1478 data->in[i] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001479 w83781d_read_value(data, W83781D_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 data->in_min[i] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001481 w83781d_read_value(data, W83781D_REG_IN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 data->in_max[i] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001483 w83781d_read_value(data, W83781D_REG_IN_MAX(i));
Jean Delvare05663362007-11-30 23:51:24 +01001484 if ((data->type != w83782d) && (i == 6))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 break;
1486 }
Jean Delvare34875332007-05-08 17:22:03 +02001487 for (i = 0; i < 3; i++) {
1488 data->fan[i] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001489 w83781d_read_value(data, W83781D_REG_FAN(i));
Jean Delvare34875332007-05-08 17:22:03 +02001490 data->fan_min[i] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001491 w83781d_read_value(data, W83781D_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
1493 if (data->type != w83781d && data->type != as99127f) {
Jean Delvare34875332007-05-08 17:22:03 +02001494 for (i = 0; i < 4; i++) {
1495 data->pwm[i] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001496 w83781d_read_value(data,
Jean Delvare34875332007-05-08 17:22:03 +02001497 W83781D_REG_PWM[i]);
Jean Delvare848ddf12009-05-08 20:27:28 +02001498 /* Only W83782D on SMBus has PWM3 and PWM4 */
1499 if ((data->type != w83782d || !client)
Jean Delvare34875332007-05-08 17:22:03 +02001500 && i == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 break;
1502 }
1503 /* Only PWM2 can be disabled */
Jean Delvare34875332007-05-08 17:22:03 +02001504 data->pwm2_enable = (w83781d_read_value(data,
Guenter Roeckc531eb32012-01-15 09:19:16 -08001505 W83781D_REG_PWMCLK12) & 0x08) >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
1507
Jean Delvare31b8dc42007-05-08 17:22:03 +02001508 data->temp = w83781d_read_value(data, W83781D_REG_TEMP(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 data->temp_max =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001510 w83781d_read_value(data, W83781D_REG_TEMP_OVER(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 data->temp_max_hyst =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001512 w83781d_read_value(data, W83781D_REG_TEMP_HYST(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 data->temp_add[0] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001514 w83781d_read_value(data, W83781D_REG_TEMP(2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 data->temp_max_add[0] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001516 w83781d_read_value(data, W83781D_REG_TEMP_OVER(2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 data->temp_max_hyst_add[0] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001518 w83781d_read_value(data, W83781D_REG_TEMP_HYST(2));
Jean Delvare7c7a5302005-06-16 19:24:14 +02001519 if (data->type != w83783s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 data->temp_add[1] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001521 w83781d_read_value(data, W83781D_REG_TEMP(3));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 data->temp_max_add[1] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001523 w83781d_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 W83781D_REG_TEMP_OVER(3));
1525 data->temp_max_hyst_add[1] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001526 w83781d_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 W83781D_REG_TEMP_HYST(3));
1528 }
Jean Delvare31b8dc42007-05-08 17:22:03 +02001529 i = w83781d_read_value(data, W83781D_REG_VID_FANDIV);
Jean Delvare7c7a5302005-06-16 19:24:14 +02001530 data->vid = i & 0x0f;
Jean Delvare31b8dc42007-05-08 17:22:03 +02001531 data->vid |= (w83781d_read_value(data,
Jean Delvare7c7a5302005-06-16 19:24:14 +02001532 W83781D_REG_CHIPID) & 0x01) << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 data->fan_div[0] = (i >> 4) & 0x03;
1534 data->fan_div[1] = (i >> 6) & 0x03;
Jean Delvare31b8dc42007-05-08 17:22:03 +02001535 data->fan_div[2] = (w83781d_read_value(data,
Jean Delvare7c7a5302005-06-16 19:24:14 +02001536 W83781D_REG_PIN) >> 6) & 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if ((data->type != w83781d) && (data->type != as99127f)) {
Jean Delvare31b8dc42007-05-08 17:22:03 +02001538 i = w83781d_read_value(data, W83781D_REG_VBAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 data->fan_div[0] |= (i >> 3) & 0x04;
1540 data->fan_div[1] |= (i >> 4) & 0x04;
Jean Delvare7c7a5302005-06-16 19:24:14 +02001541 data->fan_div[2] |= (i >> 5) & 0x04;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
Jean Delvare05663362007-11-30 23:51:24 +01001543 if (data->type == w83782d) {
Jean Delvare31b8dc42007-05-08 17:22:03 +02001544 data->alarms = w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001545 W83782D_REG_ALARM1)
Jean Delvare31b8dc42007-05-08 17:22:03 +02001546 | (w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001547 W83782D_REG_ALARM2) << 8)
Jean Delvare31b8dc42007-05-08 17:22:03 +02001548 | (w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001549 W83782D_REG_ALARM3) << 16);
1550 } else if (data->type == w83783s) {
Jean Delvare31b8dc42007-05-08 17:22:03 +02001551 data->alarms = w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001552 W83782D_REG_ALARM1)
Jean Delvare31b8dc42007-05-08 17:22:03 +02001553 | (w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001554 W83782D_REG_ALARM2) << 8);
1555 } else {
Guenter Roeckaff6e002012-01-19 11:02:27 -08001556 /*
1557 * No real-time status registers, fall back to
1558 * interrupt status registers
1559 */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001560 data->alarms = w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001561 W83781D_REG_ALARM1)
Jean Delvare31b8dc42007-05-08 17:22:03 +02001562 | (w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001563 W83781D_REG_ALARM2) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
Jean Delvare31b8dc42007-05-08 17:22:03 +02001565 i = w83781d_read_value(data, W83781D_REG_BEEP_INTS2);
Jean Delvare2fbbbf12008-10-17 17:51:18 +02001566 data->beep_mask = (i << 8) +
Jean Delvare31b8dc42007-05-08 17:22:03 +02001567 w83781d_read_value(data, W83781D_REG_BEEP_INTS1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if ((data->type != w83781d) && (data->type != as99127f)) {
1569 data->beep_mask |=
Jean Delvare31b8dc42007-05-08 17:22:03 +02001570 w83781d_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 W83781D_REG_BEEP_INTS3) << 16;
1572 }
1573 data->last_updated = jiffies;
1574 data->valid = 1;
1575 }
1576
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001577 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579 return data;
1580}
1581
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001582static const struct i2c_device_id w83781d_ids[] = {
1583 { "w83781d", w83781d, },
1584 { "w83782d", w83782d, },
1585 { "w83783s", w83783s, },
1586 { "as99127f", as99127f },
1587 { /* LIST END */ }
1588};
1589MODULE_DEVICE_TABLE(i2c, w83781d_ids);
1590
1591static struct i2c_driver w83781d_driver = {
1592 .class = I2C_CLASS_HWMON,
1593 .driver = {
1594 .name = "w83781d",
1595 },
1596 .probe = w83781d_probe,
1597 .remove = w83781d_remove,
1598 .id_table = w83781d_ids,
1599 .detect = w83781d_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +01001600 .address_list = normal_i2c,
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001601};
1602
1603/*
1604 * ISA related code
1605 */
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001606#ifdef CONFIG_ISA
1607
1608/* ISA device, if found */
1609static struct platform_device *pdev;
1610
1611static unsigned short isa_address = 0x290;
1612
Guenter Roeckaff6e002012-01-19 11:02:27 -08001613/*
1614 * I2C devices get this name attribute automatically, but for ISA devices
1615 * we must create it by ourselves.
1616 */
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001617static ssize_t
1618show_name(struct device *dev, struct device_attribute *devattr, char *buf)
1619{
1620 struct w83781d_data *data = dev_get_drvdata(dev);
Jean Delvare360782d2008-10-17 17:51:19 +02001621 return sprintf(buf, "%s\n", data->name);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001622}
1623static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1624
1625static struct w83781d_data *w83781d_data_if_isa(void)
1626{
1627 return pdev ? platform_get_drvdata(pdev) : NULL;
1628}
1629
1630/* Returns 1 if the I2C chip appears to be an alias of the ISA chip */
1631static int w83781d_alias_detect(struct i2c_client *client, u8 chipid)
1632{
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001633 struct w83781d_data *isa;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001634 int i;
1635
1636 if (!pdev) /* No ISA chip */
1637 return 0;
1638
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001639 isa = platform_get_drvdata(pdev);
1640
1641 if (w83781d_read_value(isa, W83781D_REG_I2C_ADDR) != client->addr)
1642 return 0; /* Address doesn't match */
1643 if (w83781d_read_value(isa, W83781D_REG_WCHIPID) != chipid)
1644 return 0; /* Chip type doesn't match */
1645
Guenter Roeckaff6e002012-01-19 11:02:27 -08001646 /*
1647 * We compare all the limit registers, the config register and the
1648 * interrupt mask registers
1649 */
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001650 for (i = 0x2b; i <= 0x3d; i++) {
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001651 if (w83781d_read_value(isa, i) !=
1652 i2c_smbus_read_byte_data(client, i))
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001653 return 0;
1654 }
1655 if (w83781d_read_value(isa, W83781D_REG_CONFIG) !=
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001656 i2c_smbus_read_byte_data(client, W83781D_REG_CONFIG))
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001657 return 0;
1658 for (i = 0x43; i <= 0x46; i++) {
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001659 if (w83781d_read_value(isa, i) !=
1660 i2c_smbus_read_byte_data(client, i))
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001661 return 0;
1662 }
1663
1664 return 1;
1665}
1666
1667static int
1668w83781d_read_value_isa(struct w83781d_data *data, u16 reg)
1669{
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001670 int word_sized, res;
1671
1672 word_sized = (((reg & 0xff00) == 0x100)
1673 || ((reg & 0xff00) == 0x200))
1674 && (((reg & 0x00ff) == 0x50)
1675 || ((reg & 0x00ff) == 0x53)
1676 || ((reg & 0x00ff) == 0x55));
1677 if (reg & 0xff00) {
1678 outb_p(W83781D_REG_BANK,
Jean Delvare360782d2008-10-17 17:51:19 +02001679 data->isa_addr + W83781D_ADDR_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001680 outb_p(reg >> 8,
Jean Delvare360782d2008-10-17 17:51:19 +02001681 data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001682 }
Jean Delvare360782d2008-10-17 17:51:19 +02001683 outb_p(reg & 0xff, data->isa_addr + W83781D_ADDR_REG_OFFSET);
1684 res = inb_p(data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001685 if (word_sized) {
1686 outb_p((reg & 0xff) + 1,
Jean Delvare360782d2008-10-17 17:51:19 +02001687 data->isa_addr + W83781D_ADDR_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001688 res =
Jean Delvare360782d2008-10-17 17:51:19 +02001689 (res << 8) + inb_p(data->isa_addr +
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001690 W83781D_DATA_REG_OFFSET);
1691 }
1692 if (reg & 0xff00) {
1693 outb_p(W83781D_REG_BANK,
Jean Delvare360782d2008-10-17 17:51:19 +02001694 data->isa_addr + W83781D_ADDR_REG_OFFSET);
1695 outb_p(0, data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001696 }
1697 return res;
1698}
1699
1700static void
1701w83781d_write_value_isa(struct w83781d_data *data, u16 reg, u16 value)
1702{
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001703 int word_sized;
1704
1705 word_sized = (((reg & 0xff00) == 0x100)
1706 || ((reg & 0xff00) == 0x200))
1707 && (((reg & 0x00ff) == 0x53)
1708 || ((reg & 0x00ff) == 0x55));
1709 if (reg & 0xff00) {
1710 outb_p(W83781D_REG_BANK,
Jean Delvare360782d2008-10-17 17:51:19 +02001711 data->isa_addr + W83781D_ADDR_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001712 outb_p(reg >> 8,
Jean Delvare360782d2008-10-17 17:51:19 +02001713 data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001714 }
Jean Delvare360782d2008-10-17 17:51:19 +02001715 outb_p(reg & 0xff, data->isa_addr + W83781D_ADDR_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001716 if (word_sized) {
1717 outb_p(value >> 8,
Jean Delvare360782d2008-10-17 17:51:19 +02001718 data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001719 outb_p((reg & 0xff) + 1,
Jean Delvare360782d2008-10-17 17:51:19 +02001720 data->isa_addr + W83781D_ADDR_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001721 }
Jean Delvare360782d2008-10-17 17:51:19 +02001722 outb_p(value & 0xff, data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001723 if (reg & 0xff00) {
1724 outb_p(W83781D_REG_BANK,
Jean Delvare360782d2008-10-17 17:51:19 +02001725 data->isa_addr + W83781D_ADDR_REG_OFFSET);
1726 outb_p(0, data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001727 }
1728}
1729
Guenter Roeckaff6e002012-01-19 11:02:27 -08001730/*
1731 * The SMBus locks itself, usually, but nothing may access the Winbond between
1732 * bank switches. ISA access must always be locked explicitly!
1733 * We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
1734 * would slow down the W83781D access and should not be necessary.
1735 * There are some ugly typecasts here, but the good news is - they should
1736 * nowhere else be necessary!
1737 */
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001738static int
1739w83781d_read_value(struct w83781d_data *data, u16 reg)
1740{
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001741 struct i2c_client *client = data->client;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001742 int res;
1743
1744 mutex_lock(&data->lock);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001745 if (client)
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001746 res = w83781d_read_value_i2c(data, reg);
1747 else
1748 res = w83781d_read_value_isa(data, reg);
1749 mutex_unlock(&data->lock);
1750 return res;
1751}
1752
1753static int
1754w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value)
1755{
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001756 struct i2c_client *client = data->client;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001757
1758 mutex_lock(&data->lock);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001759 if (client)
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001760 w83781d_write_value_i2c(data, reg, value);
1761 else
1762 w83781d_write_value_isa(data, reg, value);
1763 mutex_unlock(&data->lock);
1764 return 0;
1765}
1766
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001767static int
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001768w83781d_isa_probe(struct platform_device *pdev)
1769{
1770 int err, reg;
1771 struct w83781d_data *data;
1772 struct resource *res;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001773
1774 /* Reserve the ISA region */
1775 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck144d2b92012-06-02 11:48:00 -07001776 if (!devm_request_region(&pdev->dev,
1777 res->start + W83781D_ADDR_REG_OFFSET, 2,
1778 "w83781d"))
1779 return -EBUSY;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001780
Guenter Roeck144d2b92012-06-02 11:48:00 -07001781 data = devm_kzalloc(&pdev->dev, sizeof(struct w83781d_data),
1782 GFP_KERNEL);
1783 if (!data)
1784 return -ENOMEM;
1785
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001786 mutex_init(&data->lock);
Jean Delvare360782d2008-10-17 17:51:19 +02001787 data->isa_addr = res->start;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001788 platform_set_drvdata(pdev, data);
1789
1790 reg = w83781d_read_value(data, W83781D_REG_WCHIPID);
1791 switch (reg) {
1792 case 0x30:
1793 data->type = w83782d;
Jean Delvare360782d2008-10-17 17:51:19 +02001794 data->name = "w83782d";
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001795 break;
1796 default:
1797 data->type = w83781d;
Jean Delvare360782d2008-10-17 17:51:19 +02001798 data->name = "w83781d";
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001799 }
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001800
1801 /* Initialize the W83781D chip */
1802 w83781d_init_device(&pdev->dev);
1803
1804 /* Register sysfs hooks */
1805 err = w83781d_create_files(&pdev->dev, data->type, 1);
1806 if (err)
1807 goto exit_remove_files;
1808
1809 err = device_create_file(&pdev->dev, &dev_attr_name);
1810 if (err)
1811 goto exit_remove_files;
1812
1813 data->hwmon_dev = hwmon_device_register(&pdev->dev);
1814 if (IS_ERR(data->hwmon_dev)) {
1815 err = PTR_ERR(data->hwmon_dev);
1816 goto exit_remove_files;
1817 }
1818
1819 return 0;
1820
1821 exit_remove_files:
Guenter Roeck79501332012-01-16 20:45:16 -08001822 w83781d_remove_files(&pdev->dev);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001823 device_remove_file(&pdev->dev, &dev_attr_name);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001824 return err;
1825}
1826
Bill Pemberton281dfd02012-11-19 13:25:51 -05001827static int
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001828w83781d_isa_remove(struct platform_device *pdev)
1829{
1830 struct w83781d_data *data = platform_get_drvdata(pdev);
1831
1832 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeck79501332012-01-16 20:45:16 -08001833 w83781d_remove_files(&pdev->dev);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001834 device_remove_file(&pdev->dev, &dev_attr_name);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001835
1836 return 0;
1837}
1838
1839static struct platform_driver w83781d_isa_driver = {
1840 .driver = {
1841 .owner = THIS_MODULE,
1842 .name = "w83781d",
1843 },
1844 .probe = w83781d_isa_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -05001845 .remove = w83781d_isa_remove,
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001846};
1847
Jean Delvare7666c132007-05-08 17:22:02 +02001848/* return 1 if a supported chip is found, 0 otherwise */
1849static int __init
1850w83781d_isa_found(unsigned short address)
1851{
1852 int val, save, found = 0;
Jean Delvareb0bcdd32010-02-05 19:58:36 +01001853 int port;
Jean Delvare7666c132007-05-08 17:22:02 +02001854
Guenter Roeckaff6e002012-01-19 11:02:27 -08001855 /*
1856 * Some boards declare base+0 to base+7 as a PNP device, some base+4
Jean Delvareb0bcdd32010-02-05 19:58:36 +01001857 * to base+7 and some base+5 to base+6. So we better request each port
Guenter Roeckaff6e002012-01-19 11:02:27 -08001858 * individually for the probing phase.
1859 */
Jean Delvareb0bcdd32010-02-05 19:58:36 +01001860 for (port = address; port < address + W83781D_EXTENT; port++) {
1861 if (!request_region(port, 1, "w83781d")) {
Joe Perches1ca28212011-01-12 21:55:11 +01001862 pr_debug("Failed to request port 0x%x\n", port);
Jean Delvareb0bcdd32010-02-05 19:58:36 +01001863 goto release;
1864 }
Jean Delvare2961cb22008-03-09 13:34:28 +01001865 }
Jean Delvare7666c132007-05-08 17:22:02 +02001866
1867#define REALLY_SLOW_IO
Guenter Roeckaff6e002012-01-19 11:02:27 -08001868 /*
1869 * We need the timeouts for at least some W83781D-like
1870 * chips. But only if we read 'undefined' registers.
1871 */
Jean Delvare7666c132007-05-08 17:22:02 +02001872 val = inb_p(address + 1);
1873 if (inb_p(address + 2) != val
1874 || inb_p(address + 3) != val
1875 || inb_p(address + 7) != val) {
Joe Perches1ca28212011-01-12 21:55:11 +01001876 pr_debug("Detection failed at step %d\n", 1);
Jean Delvare7666c132007-05-08 17:22:02 +02001877 goto release;
1878 }
1879#undef REALLY_SLOW_IO
1880
Guenter Roeckaff6e002012-01-19 11:02:27 -08001881 /*
1882 * We should be able to change the 7 LSB of the address port. The
1883 * MSB (busy flag) should be clear initially, set after the write.
1884 */
Jean Delvare7666c132007-05-08 17:22:02 +02001885 save = inb_p(address + W83781D_ADDR_REG_OFFSET);
1886 if (save & 0x80) {
Joe Perches1ca28212011-01-12 21:55:11 +01001887 pr_debug("Detection failed at step %d\n", 2);
Jean Delvare7666c132007-05-08 17:22:02 +02001888 goto release;
1889 }
1890 val = ~save & 0x7f;
1891 outb_p(val, address + W83781D_ADDR_REG_OFFSET);
1892 if (inb_p(address + W83781D_ADDR_REG_OFFSET) != (val | 0x80)) {
1893 outb_p(save, address + W83781D_ADDR_REG_OFFSET);
Joe Perches1ca28212011-01-12 21:55:11 +01001894 pr_debug("Detection failed at step %d\n", 3);
Jean Delvare7666c132007-05-08 17:22:02 +02001895 goto release;
1896 }
1897
1898 /* We found a device, now see if it could be a W83781D */
1899 outb_p(W83781D_REG_CONFIG, address + W83781D_ADDR_REG_OFFSET);
1900 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1901 if (val & 0x80) {
Joe Perches1ca28212011-01-12 21:55:11 +01001902 pr_debug("Detection failed at step %d\n", 4);
Jean Delvare7666c132007-05-08 17:22:02 +02001903 goto release;
1904 }
1905 outb_p(W83781D_REG_BANK, address + W83781D_ADDR_REG_OFFSET);
1906 save = inb_p(address + W83781D_DATA_REG_OFFSET);
1907 outb_p(W83781D_REG_CHIPMAN, address + W83781D_ADDR_REG_OFFSET);
1908 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1909 if ((!(save & 0x80) && (val != 0xa3))
1910 || ((save & 0x80) && (val != 0x5c))) {
Joe Perches1ca28212011-01-12 21:55:11 +01001911 pr_debug("Detection failed at step %d\n", 5);
Jean Delvare7666c132007-05-08 17:22:02 +02001912 goto release;
1913 }
1914 outb_p(W83781D_REG_I2C_ADDR, address + W83781D_ADDR_REG_OFFSET);
1915 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1916 if (val < 0x03 || val > 0x77) { /* Not a valid I2C address */
Joe Perches1ca28212011-01-12 21:55:11 +01001917 pr_debug("Detection failed at step %d\n", 6);
Jean Delvare7666c132007-05-08 17:22:02 +02001918 goto release;
1919 }
1920
1921 /* The busy flag should be clear again */
1922 if (inb_p(address + W83781D_ADDR_REG_OFFSET) & 0x80) {
Joe Perches1ca28212011-01-12 21:55:11 +01001923 pr_debug("Detection failed at step %d\n", 7);
Jean Delvare7666c132007-05-08 17:22:02 +02001924 goto release;
1925 }
1926
1927 /* Determine the chip type */
1928 outb_p(W83781D_REG_BANK, address + W83781D_ADDR_REG_OFFSET);
1929 save = inb_p(address + W83781D_DATA_REG_OFFSET);
1930 outb_p(save & 0xf8, address + W83781D_DATA_REG_OFFSET);
1931 outb_p(W83781D_REG_WCHIPID, address + W83781D_ADDR_REG_OFFSET);
1932 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1933 if ((val & 0xfe) == 0x10 /* W83781D */
Jean Delvare05663362007-11-30 23:51:24 +01001934 || val == 0x30) /* W83782D */
Jean Delvare7666c132007-05-08 17:22:02 +02001935 found = 1;
1936
1937 if (found)
Joe Perches1ca28212011-01-12 21:55:11 +01001938 pr_info("Found a %s chip at %#x\n",
Jean Delvare7666c132007-05-08 17:22:02 +02001939 val == 0x30 ? "W83782D" : "W83781D", (int)address);
1940
1941 release:
Jean Delvareb0bcdd32010-02-05 19:58:36 +01001942 for (port--; port >= address; port--)
1943 release_region(port, 1);
Jean Delvare7666c132007-05-08 17:22:02 +02001944 return found;
1945}
1946
1947static int __init
1948w83781d_isa_device_add(unsigned short address)
1949{
1950 struct resource res = {
1951 .start = address,
Jean Delvare15bde2f2007-08-29 10:39:57 +02001952 .end = address + W83781D_EXTENT - 1,
Jean Delvare7666c132007-05-08 17:22:02 +02001953 .name = "w83781d",
1954 .flags = IORESOURCE_IO,
1955 };
1956 int err;
1957
1958 pdev = platform_device_alloc("w83781d", address);
1959 if (!pdev) {
1960 err = -ENOMEM;
Joe Perches1ca28212011-01-12 21:55:11 +01001961 pr_err("Device allocation failed\n");
Jean Delvare7666c132007-05-08 17:22:02 +02001962 goto exit;
1963 }
1964
1965 err = platform_device_add_resources(pdev, &res, 1);
1966 if (err) {
Joe Perches1ca28212011-01-12 21:55:11 +01001967 pr_err("Device resource addition failed (%d)\n", err);
Jean Delvare7666c132007-05-08 17:22:02 +02001968 goto exit_device_put;
1969 }
1970
1971 err = platform_device_add(pdev);
1972 if (err) {
Joe Perches1ca28212011-01-12 21:55:11 +01001973 pr_err("Device addition failed (%d)\n", err);
Jean Delvare7666c132007-05-08 17:22:02 +02001974 goto exit_device_put;
1975 }
1976
1977 return 0;
1978
1979 exit_device_put:
1980 platform_device_put(pdev);
1981 exit:
1982 pdev = NULL;
1983 return err;
1984}
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986static int __init
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001987w83781d_isa_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Jean Delvarefde09502005-07-19 23:51:07 +02001989 int res;
1990
Jean Delvare7666c132007-05-08 17:22:02 +02001991 if (w83781d_isa_found(isa_address)) {
1992 res = platform_driver_register(&w83781d_isa_driver);
1993 if (res)
Jean Delvarec6566202008-10-17 17:51:18 +02001994 goto exit;
Jean Delvare7666c132007-05-08 17:22:02 +02001995
1996 /* Sets global pdev as a side effect */
1997 res = w83781d_isa_device_add(isa_address);
1998 if (res)
1999 goto exit_unreg_isa_driver;
2000 }
Jean Delvarefde09502005-07-19 23:51:07 +02002001
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02002002 return 0;
2003
2004exit_unreg_isa_driver:
2005 platform_driver_unregister(&w83781d_isa_driver);
2006exit:
2007 return res;
2008}
2009
Geert Uytterhoevendd56b632008-10-26 17:04:38 +01002010static void
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02002011w83781d_isa_unregister(void)
2012{
2013 if (pdev) {
2014 platform_device_unregister(pdev);
2015 platform_driver_unregister(&w83781d_isa_driver);
2016 }
2017}
2018#else /* !CONFIG_ISA */
2019
2020static struct w83781d_data *w83781d_data_if_isa(void)
2021{
2022 return NULL;
2023}
2024
2025static int
2026w83781d_alias_detect(struct i2c_client *client, u8 chipid)
2027{
2028 return 0;
2029}
2030
2031static int
2032w83781d_read_value(struct w83781d_data *data, u16 reg)
2033{
2034 int res;
2035
2036 mutex_lock(&data->lock);
2037 res = w83781d_read_value_i2c(data, reg);
2038 mutex_unlock(&data->lock);
2039
2040 return res;
2041}
2042
2043static int
2044w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value)
2045{
2046 mutex_lock(&data->lock);
2047 w83781d_write_value_i2c(data, reg, value);
2048 mutex_unlock(&data->lock);
2049
2050 return 0;
2051}
2052
2053static int __init
2054w83781d_isa_register(void)
2055{
2056 return 0;
2057}
2058
Geert Uytterhoevendd56b632008-10-26 17:04:38 +01002059static void
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02002060w83781d_isa_unregister(void)
2061{
2062}
2063#endif /* CONFIG_ISA */
2064
2065static int __init
2066sensors_w83781d_init(void)
2067{
2068 int res;
2069
Guenter Roeckaff6e002012-01-19 11:02:27 -08002070 /*
2071 * We register the ISA device first, so that we can skip the
2072 * registration of an I2C interface to the same device.
2073 */
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02002074 res = w83781d_isa_register();
2075 if (res)
2076 goto exit;
2077
Jean Delvarec6566202008-10-17 17:51:18 +02002078 res = i2c_add_driver(&w83781d_driver);
2079 if (res)
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02002080 goto exit_unreg_isa;
Jean Delvarec6566202008-10-17 17:51:18 +02002081
Jean Delvarefde09502005-07-19 23:51:07 +02002082 return 0;
Jean Delvare7666c132007-05-08 17:22:02 +02002083
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02002084 exit_unreg_isa:
2085 w83781d_isa_unregister();
Jean Delvare7666c132007-05-08 17:22:02 +02002086 exit:
2087 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
2089
2090static void __exit
2091sensors_w83781d_exit(void)
2092{
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02002093 w83781d_isa_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 i2c_del_driver(&w83781d_driver);
2095}
2096
2097MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
2098 "Philip Edelbrock <phil@netroedge.com>, "
2099 "and Mark Studebaker <mdsxyz123@yahoo.com>");
2100MODULE_DESCRIPTION("W83781D driver");
2101MODULE_LICENSE("GPL");
2102
2103module_init(sensors_w83781d_init);
2104module_exit(sensors_w83781d_exit);