aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/hwmon')
-rw-r--r--drivers/hwmon/adm1021.c402
-rw-r--r--drivers/hwmon/adm1025.c577
-rw-r--r--drivers/hwmon/adm1026.c1714
-rw-r--r--drivers/hwmon/adm1031.c977
-rw-r--r--drivers/hwmon/adm9240.c791
-rw-r--r--drivers/hwmon/asb100.c1065
-rw-r--r--drivers/hwmon/atxp1.c361
-rw-r--r--drivers/hwmon/ds1621.c341
-rw-r--r--drivers/hwmon/fscher.c691
-rw-r--r--drivers/hwmon/fscpos.c641
-rw-r--r--drivers/hwmon/gl518sm.c604
-rw-r--r--drivers/hwmon/gl520sm.c769
-rw-r--r--drivers/hwmon/it87.c1184
-rw-r--r--drivers/hwmon/lm63.c597
-rw-r--r--drivers/hwmon/lm75.c296
-rw-r--r--drivers/hwmon/lm75.h49
-rw-r--r--drivers/hwmon/lm77.c420
-rw-r--r--drivers/hwmon/lm78.c795
-rw-r--r--drivers/hwmon/lm80.c601
-rw-r--r--drivers/hwmon/lm83.c408
-rw-r--r--drivers/hwmon/lm85.c1575
-rw-r--r--drivers/hwmon/lm87.c828
-rw-r--r--drivers/hwmon/lm90.c655
-rw-r--r--drivers/hwmon/lm92.c429
-rw-r--r--drivers/hwmon/max1619.c372
-rw-r--r--drivers/hwmon/pc87360.c1348
-rw-r--r--drivers/hwmon/sis5595.c817
-rw-r--r--drivers/hwmon/smsc47b397.c352
-rw-r--r--drivers/hwmon/smsc47m1.c593
-rw-r--r--drivers/hwmon/via686a.c875
-rw-r--r--drivers/hwmon/w83627ehf.c846
-rw-r--r--drivers/hwmon/w83627hf.c1511
-rw-r--r--drivers/hwmon/w83781d.c1632
-rw-r--r--drivers/hwmon/w83l785ts.c328
34 files changed, 25444 insertions, 0 deletions
diff --git a/drivers/hwmon/adm1021.c b/drivers/hwmon/adm1021.c
new file mode 100644
index 000000000000..d2c774c32f45
--- /dev/null
+++ b/drivers/hwmon/adm1021.c
@@ -0,0 +1,402 @@
1/*
2 adm1021.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
5 Philip Edelbrock <phil@netroedge.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*/
21
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/jiffies.h>
26#include <linux/i2c.h>
27#include <linux/i2c-sensor.h>
28
29
30/* Addresses to scan */
31static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
32 0x29, 0x2a, 0x2b,
33 0x4c, 0x4d, 0x4e,
34 I2C_CLIENT_END };
35static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
36
37/* Insmod parameters */
38SENSORS_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, mc1066);
39
40/* adm1021 constants specified below */
41
42/* The adm1021 registers */
43/* Read-only */
44#define ADM1021_REG_TEMP 0x00
45#define ADM1021_REG_REMOTE_TEMP 0x01
46#define ADM1021_REG_STATUS 0x02
47#define ADM1021_REG_MAN_ID 0x0FE /* 0x41 = AMD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi*/
48#define ADM1021_REG_DEV_ID 0x0FF /* ADM1021 = 0x0X, ADM1023 = 0x3X */
49#define ADM1021_REG_DIE_CODE 0x0FF /* MAX1617A */
50/* These use different addresses for reading/writing */
51#define ADM1021_REG_CONFIG_R 0x03
52#define ADM1021_REG_CONFIG_W 0x09
53#define ADM1021_REG_CONV_RATE_R 0x04
54#define ADM1021_REG_CONV_RATE_W 0x0A
55/* These are for the ADM1023's additional precision on the remote temp sensor */
56#define ADM1021_REG_REM_TEMP_PREC 0x010
57#define ADM1021_REG_REM_OFFSET 0x011
58#define ADM1021_REG_REM_OFFSET_PREC 0x012
59#define ADM1021_REG_REM_TOS_PREC 0x013
60#define ADM1021_REG_REM_THYST_PREC 0x014
61/* limits */
62#define ADM1021_REG_TOS_R 0x05
63#define ADM1021_REG_TOS_W 0x0B
64#define ADM1021_REG_REMOTE_TOS_R 0x07
65#define ADM1021_REG_REMOTE_TOS_W 0x0D
66#define ADM1021_REG_THYST_R 0x06
67#define ADM1021_REG_THYST_W 0x0C
68#define ADM1021_REG_REMOTE_THYST_R 0x08
69#define ADM1021_REG_REMOTE_THYST_W 0x0E
70/* write-only */
71#define ADM1021_REG_ONESHOT 0x0F
72
73
74/* Conversions. Rounding and limit checking is only done on the TO_REG
75 variants. Note that you should be a bit careful with which arguments
76 these macros are called: arguments may be evaluated more than once.
77 Fixing this is just not worth it. */
78/* Conversions note: 1021 uses normal integer signed-byte format*/
79#define TEMP_FROM_REG(val) (val > 127 ? (val-256)*1000 : val*1000)
80#define TEMP_TO_REG(val) (SENSORS_LIMIT((val < 0 ? (val/1000)+256 : val/1000),0,255))
81
82/* Initial values */
83
84/* Note: Even though I left the low and high limits named os and hyst,
85they don't quite work like a thermostat the way the LM75 does. I.e.,
86a lower temp than THYST actually triggers an alarm instead of
87clearing it. Weird, ey? --Phil */
88
89/* Each client has this additional data */
90struct adm1021_data {
91 struct i2c_client client;
92 enum chips type;
93
94 struct semaphore update_lock;
95 char valid; /* !=0 if following fields are valid */
96 unsigned long last_updated; /* In jiffies */
97
98 u8 temp_max; /* Register values */
99 u8 temp_hyst;
100 u8 temp_input;
101 u8 remote_temp_max;
102 u8 remote_temp_hyst;
103 u8 remote_temp_input;
104 u8 alarms;
105 /* Special values for ADM1023 only */
106 u8 remote_temp_prec;
107 u8 remote_temp_os_prec;
108 u8 remote_temp_hyst_prec;
109 u8 remote_temp_offset;
110 u8 remote_temp_offset_prec;
111};
112
113static int adm1021_attach_adapter(struct i2c_adapter *adapter);
114static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind);
115static void adm1021_init_client(struct i2c_client *client);
116static int adm1021_detach_client(struct i2c_client *client);
117static int adm1021_read_value(struct i2c_client *client, u8 reg);
118static int adm1021_write_value(struct i2c_client *client, u8 reg,
119 u16 value);
120static struct adm1021_data *adm1021_update_device(struct device *dev);
121
122/* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
123static int read_only = 0;
124
125
126/* This is the driver that will be inserted */
127static struct i2c_driver adm1021_driver = {
128 .owner = THIS_MODULE,
129 .name = "adm1021",
130 .id = I2C_DRIVERID_ADM1021,
131 .flags = I2C_DF_NOTIFY,
132 .attach_adapter = adm1021_attach_adapter,
133 .detach_client = adm1021_detach_client,
134};
135
136#define show(value) \
137static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
138{ \
139 struct adm1021_data *data = adm1021_update_device(dev); \
140 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \
141}
142show(temp_max);
143show(temp_hyst);
144show(temp_input);
145show(remote_temp_max);
146show(remote_temp_hyst);
147show(remote_temp_input);
148
149#define show2(value) \
150static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
151{ \
152 struct adm1021_data *data = adm1021_update_device(dev); \
153 return sprintf(buf, "%d\n", data->value); \
154}
155show2(alarms);
156
157#define set(value, reg) \
158static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
159{ \
160 struct i2c_client *client = to_i2c_client(dev); \
161 struct adm1021_data *data = i2c_get_clientdata(client); \
162 int temp = simple_strtoul(buf, NULL, 10); \
163 \
164 down(&data->update_lock); \
165 data->value = TEMP_TO_REG(temp); \
166 adm1021_write_value(client, reg, data->value); \
167 up(&data->update_lock); \
168 return count; \
169}
170set(temp_max, ADM1021_REG_TOS_W);
171set(temp_hyst, ADM1021_REG_THYST_W);
172set(remote_temp_max, ADM1021_REG_REMOTE_TOS_W);
173set(remote_temp_hyst, ADM1021_REG_REMOTE_THYST_W);
174
175static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
176static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst);
177static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL);
178static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_remote_temp_max, set_remote_temp_max);
179static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_remote_temp_hyst, set_remote_temp_hyst);
180static DEVICE_ATTR(temp2_input, S_IRUGO, show_remote_temp_input, NULL);
181static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
182
183
184static int adm1021_attach_adapter(struct i2c_adapter *adapter)
185{
186 if (!(adapter->class & I2C_CLASS_HWMON))
187 return 0;
188 return i2c_detect(adapter, &addr_data, adm1021_detect);
189}
190
191static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind)
192{
193 int i;
194 struct i2c_client *new_client;
195 struct adm1021_data *data;
196 int err = 0;
197 const char *type_name = "";
198
199 /* Make sure we aren't probing the ISA bus!! This is just a safety check
200 at this moment; i2c_detect really won't call us. */
201#ifdef DEBUG
202 if (i2c_is_isa_adapter(adapter)) {
203 dev_dbg(&adapter->dev, "adm1021_detect called for an ISA bus adapter?!?\n");
204 return 0;
205 }
206#endif
207
208 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
209 goto error0;
210
211 /* OK. For now, we presume we have a valid client. We now create the
212 client structure, even though we cannot fill it completely yet.
213 But it allows us to access adm1021_{read,write}_value. */
214
215 if (!(data = kmalloc(sizeof(struct adm1021_data), GFP_KERNEL))) {
216 err = -ENOMEM;
217 goto error0;
218 }
219 memset(data, 0, sizeof(struct adm1021_data));
220
221 new_client = &data->client;
222 i2c_set_clientdata(new_client, data);
223 new_client->addr = address;
224 new_client->adapter = adapter;
225 new_client->driver = &adm1021_driver;
226 new_client->flags = 0;
227
228 /* Now, we do the remaining detection. */
229 if (kind < 0) {
230 if ((adm1021_read_value(new_client, ADM1021_REG_STATUS) & 0x03) != 0x00
231 || (adm1021_read_value(new_client, ADM1021_REG_CONFIG_R) & 0x3F) != 0x00
232 || (adm1021_read_value(new_client, ADM1021_REG_CONV_RATE_R) & 0xF8) != 0x00) {
233 err = -ENODEV;
234 goto error1;
235 }
236 }
237
238 /* Determine the chip type. */
239 if (kind <= 0) {
240 i = adm1021_read_value(new_client, ADM1021_REG_MAN_ID);
241 if (i == 0x41)
242 if ((adm1021_read_value(new_client, ADM1021_REG_DEV_ID) & 0x0F0) == 0x030)
243 kind = adm1023;
244 else
245 kind = adm1021;
246 else if (i == 0x49)
247 kind = thmc10;
248 else if (i == 0x23)
249 kind = gl523sm;
250 else if ((i == 0x4d) &&
251 (adm1021_read_value(new_client, ADM1021_REG_DEV_ID) == 0x01))
252 kind = max1617a;
253 else if (i == 0x54)
254 kind = mc1066;
255 /* LM84 Mfr ID in a different place, and it has more unused bits */
256 else if (adm1021_read_value(new_client, ADM1021_REG_CONV_RATE_R) == 0x00
257 && (kind == 0 /* skip extra detection */
258 || ((adm1021_read_value(new_client, ADM1021_REG_CONFIG_R) & 0x7F) == 0x00
259 && (adm1021_read_value(new_client, ADM1021_REG_STATUS) & 0xAB) == 0x00)))
260 kind = lm84;
261 else
262 kind = max1617;
263 }
264
265 if (kind == max1617) {
266 type_name = "max1617";
267 } else if (kind == max1617a) {
268 type_name = "max1617a";
269 } else if (kind == adm1021) {
270 type_name = "adm1021";
271 } else if (kind == adm1023) {
272 type_name = "adm1023";
273 } else if (kind == thmc10) {
274 type_name = "thmc10";
275 } else if (kind == lm84) {
276 type_name = "lm84";
277 } else if (kind == gl523sm) {
278 type_name = "gl523sm";
279 } else if (kind == mc1066) {
280 type_name = "mc1066";
281 }
282
283 /* Fill in the remaining client fields and put it into the global list */
284 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
285 data->type = kind;
286 data->valid = 0;
287 init_MUTEX(&data->update_lock);
288
289 /* Tell the I2C layer a new client has arrived */
290 if ((err = i2c_attach_client(new_client)))
291 goto error1;
292
293 /* Initialize the ADM1021 chip */
294 if (kind != lm84)
295 adm1021_init_client(new_client);
296
297 /* Register sysfs hooks */
298 device_create_file(&new_client->dev, &dev_attr_temp1_max);
299 device_create_file(&new_client->dev, &dev_attr_temp1_min);
300 device_create_file(&new_client->dev, &dev_attr_temp1_input);
301 device_create_file(&new_client->dev, &dev_attr_temp2_max);
302 device_create_file(&new_client->dev, &dev_attr_temp2_min);
303 device_create_file(&new_client->dev, &dev_attr_temp2_input);
304 device_create_file(&new_client->dev, &dev_attr_alarms);
305
306 return 0;
307
308error1:
309 kfree(data);
310error0:
311 return err;
312}
313
314static void adm1021_init_client(struct i2c_client *client)
315{
316 /* Enable ADC and disable suspend mode */
317 adm1021_write_value(client, ADM1021_REG_CONFIG_W,
318 adm1021_read_value(client, ADM1021_REG_CONFIG_R) & 0xBF);
319 /* Set Conversion rate to 1/sec (this can be tinkered with) */
320 adm1021_write_value(client, ADM1021_REG_CONV_RATE_W, 0x04);
321}
322
323static int adm1021_detach_client(struct i2c_client *client)
324{
325 int err;
326
327 if ((err = i2c_detach_client(client))) {
328 dev_err(&client->dev, "Client deregistration failed, client not detached.\n");
329 return err;
330 }
331
332 kfree(i2c_get_clientdata(client));
333 return 0;
334}
335
336/* All registers are byte-sized */
337static int adm1021_read_value(struct i2c_client *client, u8 reg)
338{
339 return i2c_smbus_read_byte_data(client, reg);
340}
341
342static int adm1021_write_value(struct i2c_client *client, u8 reg, u16 value)
343{
344 if (!read_only)
345 return i2c_smbus_write_byte_data(client, reg, value);
346 return 0;
347}
348
349static struct adm1021_data *adm1021_update_device(struct device *dev)
350{
351 struct i2c_client *client = to_i2c_client(dev);
352 struct adm1021_data *data = i2c_get_clientdata(client);
353
354 down(&data->update_lock);
355
356 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
357 || !data->valid) {
358 dev_dbg(&client->dev, "Starting adm1021 update\n");
359
360 data->temp_input = adm1021_read_value(client, ADM1021_REG_TEMP);
361 data->temp_max = adm1021_read_value(client, ADM1021_REG_TOS_R);
362 data->temp_hyst = adm1021_read_value(client, ADM1021_REG_THYST_R);
363 data->remote_temp_input = adm1021_read_value(client, ADM1021_REG_REMOTE_TEMP);
364 data->remote_temp_max = adm1021_read_value(client, ADM1021_REG_REMOTE_TOS_R);
365 data->remote_temp_hyst = adm1021_read_value(client, ADM1021_REG_REMOTE_THYST_R);
366 data->alarms = adm1021_read_value(client, ADM1021_REG_STATUS) & 0x7c;
367 if (data->type == adm1023) {
368 data->remote_temp_prec = adm1021_read_value(client, ADM1021_REG_REM_TEMP_PREC);
369 data->remote_temp_os_prec = adm1021_read_value(client, ADM1021_REG_REM_TOS_PREC);
370 data->remote_temp_hyst_prec = adm1021_read_value(client, ADM1021_REG_REM_THYST_PREC);
371 data->remote_temp_offset = adm1021_read_value(client, ADM1021_REG_REM_OFFSET);
372 data->remote_temp_offset_prec = adm1021_read_value(client, ADM1021_REG_REM_OFFSET_PREC);
373 }
374 data->last_updated = jiffies;
375 data->valid = 1;
376 }
377
378 up(&data->update_lock);
379
380 return data;
381}
382
383static int __init sensors_adm1021_init(void)
384{
385 return i2c_add_driver(&adm1021_driver);
386}
387
388static void __exit sensors_adm1021_exit(void)
389{
390 i2c_del_driver(&adm1021_driver);
391}
392
393MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
394 "Philip Edelbrock <phil@netroedge.com>");
395MODULE_DESCRIPTION("adm1021 driver");
396MODULE_LICENSE("GPL");
397
398module_param(read_only, bool, 0);
399MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
400
401module_init(sensors_adm1021_init)
402module_exit(sensors_adm1021_exit)
diff --git a/drivers/hwmon/adm1025.c b/drivers/hwmon/adm1025.c
new file mode 100644
index 000000000000..e452d0daf906
--- /dev/null
+++ b/drivers/hwmon/adm1025.c
@@ -0,0 +1,577 @@
1/*
2 * adm1025.c
3 *
4 * Copyright (C) 2000 Chen-Yuan Wu <gwu@esoft.com>
5 * Copyright (C) 2003-2004 Jean Delvare <khali@linux-fr.org>
6 *
7 * The ADM1025 is a sensor chip made by Analog Devices. It reports up to 6
8 * voltages (including its own power source) and up to two temperatures
9 * (its own plus up to one external one). Voltages are scaled internally
10 * (which is not the common way) with ratios such that the nominal value
11 * of each voltage correspond to a register value of 192 (which means a
12 * resolution of about 0.5% of the nominal value). Temperature values are
13 * reported with a 1 deg resolution and a 3 deg accuracy. Complete
14 * datasheet can be obtained from Analog's website at:
15 * http://www.analog.com/Analog_Root/productPage/productHome/0,2121,ADM1025,00.html
16 *
17 * This driver also supports the ADM1025A, which differs from the ADM1025
18 * only in that it has "open-drain VID inputs while the ADM1025 has
19 * on-chip 100k pull-ups on the VID inputs". It doesn't make any
20 * difference for us.
21 *
22 * This driver also supports the NE1619, a sensor chip made by Philips.
23 * That chip is similar to the ADM1025A, with a few differences. The only
24 * difference that matters to us is that the NE1619 has only two possible
25 * addresses while the ADM1025A has a third one. Complete datasheet can be
26 * obtained from Philips's website at:
27 * http://www.semiconductors.philips.com/pip/NE1619DS.html
28 *
29 * Since the ADM1025 was the first chipset supported by this driver, most
30 * comments will refer to this chipset, but are actually general and
31 * concern all supported chipsets, unless mentioned otherwise.
32 *
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
37 *
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License
44 * along with this program; if not, write to the Free Software
45 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
46 */
47
48#include <linux/module.h>
49#include <linux/init.h>
50#include <linux/slab.h>
51#include <linux/jiffies.h>
52#include <linux/i2c.h>
53#include <linux/i2c-sensor.h>
54#include <linux/i2c-vid.h>
55
56/*
57 * Addresses to scan
58 * ADM1025 and ADM1025A have three possible addresses: 0x2c, 0x2d and 0x2e.
59 * NE1619 has two possible addresses: 0x2c and 0x2d.
60 */
61
62static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
63static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
64
65/*
66 * Insmod parameters
67 */
68
69SENSORS_INSMOD_2(adm1025, ne1619);
70
71/*
72 * The ADM1025 registers
73 */
74
75#define ADM1025_REG_MAN_ID 0x3E
76#define ADM1025_REG_CHIP_ID 0x3F
77#define ADM1025_REG_CONFIG 0x40
78#define ADM1025_REG_STATUS1 0x41
79#define ADM1025_REG_STATUS2 0x42
80#define ADM1025_REG_IN(nr) (0x20 + (nr))
81#define ADM1025_REG_IN_MAX(nr) (0x2B + (nr) * 2)
82#define ADM1025_REG_IN_MIN(nr) (0x2C + (nr) * 2)
83#define ADM1025_REG_TEMP(nr) (0x26 + (nr))
84#define ADM1025_REG_TEMP_HIGH(nr) (0x37 + (nr) * 2)
85#define ADM1025_REG_TEMP_LOW(nr) (0x38 + (nr) * 2)
86#define ADM1025_REG_VID 0x47
87#define ADM1025_REG_VID4 0x49
88
89/*
90 * Conversions and various macros
91 * The ADM1025 uses signed 8-bit values for temperatures.
92 */
93
94static int in_scale[6] = { 2500, 2250, 3300, 5000, 12000, 3300 };
95
96#define IN_FROM_REG(reg,scale) (((reg) * (scale) + 96) / 192)
97#define IN_TO_REG(val,scale) ((val) <= 0 ? 0 : \
98 (val) * 192 >= (scale) * 255 ? 255 : \
99 ((val) * 192 + (scale)/2) / (scale))
100
101#define TEMP_FROM_REG(reg) ((reg) * 1000)
102#define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \
103 (val) >= 126500 ? 127 : \
104 (((val) < 0 ? (val)-500 : (val)+500) / 1000))
105
106/*
107 * Functions declaration
108 */
109
110static int adm1025_attach_adapter(struct i2c_adapter *adapter);
111static int adm1025_detect(struct i2c_adapter *adapter, int address, int kind);
112static void adm1025_init_client(struct i2c_client *client);
113static int adm1025_detach_client(struct i2c_client *client);
114static struct adm1025_data *adm1025_update_device(struct device *dev);
115
116/*
117 * Driver data (common to all clients)
118 */
119
120static struct i2c_driver adm1025_driver = {
121 .owner = THIS_MODULE,
122 .name = "adm1025",
123 .id = I2C_DRIVERID_ADM1025,
124 .flags = I2C_DF_NOTIFY,
125 .attach_adapter = adm1025_attach_adapter,
126 .detach_client = adm1025_detach_client,
127};
128
129/*
130 * Client data (each client gets its own)
131 */
132
133struct adm1025_data {
134 struct i2c_client client;
135 struct semaphore update_lock;
136 char valid; /* zero until following fields are valid */
137 unsigned long last_updated; /* in jiffies */
138
139 u8 in[6]; /* register value */
140 u8 in_max[6]; /* register value */
141 u8 in_min[6]; /* register value */
142 s8 temp[2]; /* register value */
143 s8 temp_min[2]; /* register value */
144 s8 temp_max[2]; /* register value */
145 u16 alarms; /* register values, combined */
146 u8 vid; /* register values, combined */
147 u8 vrm;
148};
149
150/*
151 * Sysfs stuff
152 */
153
154#define show_in(offset) \
155static ssize_t show_in##offset(struct device *dev, struct device_attribute *attr, char *buf) \
156{ \
157 struct adm1025_data *data = adm1025_update_device(dev); \
158 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
159 in_scale[offset])); \
160} \
161static ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
162{ \
163 struct adm1025_data *data = adm1025_update_device(dev); \
164 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
165 in_scale[offset])); \
166} \
167static ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
168{ \
169 struct adm1025_data *data = adm1025_update_device(dev); \
170 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
171 in_scale[offset])); \
172} \
173static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL);
174show_in(0);
175show_in(1);
176show_in(2);
177show_in(3);
178show_in(4);
179show_in(5);
180
181#define show_temp(offset) \
182static ssize_t show_temp##offset(struct device *dev, struct device_attribute *attr, char *buf) \
183{ \
184 struct adm1025_data *data = adm1025_update_device(dev); \
185 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
186} \
187static ssize_t show_temp##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
188{ \
189 struct adm1025_data *data = adm1025_update_device(dev); \
190 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[offset-1])); \
191} \
192static ssize_t show_temp##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
193{ \
194 struct adm1025_data *data = adm1025_update_device(dev); \
195 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[offset-1])); \
196}\
197static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp##offset, NULL);
198show_temp(1);
199show_temp(2);
200
201#define set_in(offset) \
202static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
203 size_t count) \
204{ \
205 struct i2c_client *client = to_i2c_client(dev); \
206 struct adm1025_data *data = i2c_get_clientdata(client); \
207 long val = simple_strtol(buf, NULL, 10); \
208 \
209 down(&data->update_lock); \
210 data->in_min[offset] = IN_TO_REG(val, in_scale[offset]); \
211 i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MIN(offset), \
212 data->in_min[offset]); \
213 up(&data->update_lock); \
214 return count; \
215} \
216static ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
217 size_t count) \
218{ \
219 struct i2c_client *client = to_i2c_client(dev); \
220 struct adm1025_data *data = i2c_get_clientdata(client); \
221 long val = simple_strtol(buf, NULL, 10); \
222 \
223 down(&data->update_lock); \
224 data->in_max[offset] = IN_TO_REG(val, in_scale[offset]); \
225 i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MAX(offset), \
226 data->in_max[offset]); \
227 up(&data->update_lock); \
228 return count; \
229} \
230static DEVICE_ATTR(in##offset##_min, S_IWUSR | S_IRUGO, \
231 show_in##offset##_min, set_in##offset##_min); \
232static DEVICE_ATTR(in##offset##_max, S_IWUSR | S_IRUGO, \
233 show_in##offset##_max, set_in##offset##_max);
234set_in(0);
235set_in(1);
236set_in(2);
237set_in(3);
238set_in(4);
239set_in(5);
240
241#define set_temp(offset) \
242static ssize_t set_temp##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
243 size_t count) \
244{ \
245 struct i2c_client *client = to_i2c_client(dev); \
246 struct adm1025_data *data = i2c_get_clientdata(client); \
247 long val = simple_strtol(buf, NULL, 10); \
248 \
249 down(&data->update_lock); \
250 data->temp_min[offset-1] = TEMP_TO_REG(val); \
251 i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_LOW(offset-1), \
252 data->temp_min[offset-1]); \
253 up(&data->update_lock); \
254 return count; \
255} \
256static ssize_t set_temp##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
257 size_t count) \
258{ \
259 struct i2c_client *client = to_i2c_client(dev); \
260 struct adm1025_data *data = i2c_get_clientdata(client); \
261 long val = simple_strtol(buf, NULL, 10); \
262 \
263 down(&data->update_lock); \
264 data->temp_max[offset-1] = TEMP_TO_REG(val); \
265 i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_HIGH(offset-1), \
266 data->temp_max[offset-1]); \
267 up(&data->update_lock); \
268 return count; \
269} \
270static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
271 show_temp##offset##_min, set_temp##offset##_min); \
272static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
273 show_temp##offset##_max, set_temp##offset##_max);
274set_temp(1);
275set_temp(2);
276
277static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
278{
279 struct adm1025_data *data = adm1025_update_device(dev);
280 return sprintf(buf, "%u\n", data->alarms);
281}
282static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
283
284static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
285{
286 struct adm1025_data *data = adm1025_update_device(dev);
287 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
288}
289/* in1_ref is deprecated in favour of cpu0_vid, remove after 2005-11-11 */
290static DEVICE_ATTR(in1_ref, S_IRUGO, show_vid, NULL);
291static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
292
293static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
294{
295 struct adm1025_data *data = adm1025_update_device(dev);
296 return sprintf(buf, "%u\n", data->vrm);
297}
298static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
299{
300 struct i2c_client *client = to_i2c_client(dev);
301 struct adm1025_data *data = i2c_get_clientdata(client);
302 data->vrm = simple_strtoul(buf, NULL, 10);
303 return count;
304}
305static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
306
307/*
308 * Real code
309 */
310
311static int adm1025_attach_adapter(struct i2c_adapter *adapter)
312{
313 if (!(adapter->class & I2C_CLASS_HWMON))
314 return 0;
315 return i2c_detect(adapter, &addr_data, adm1025_detect);
316}
317
318/*
319 * The following function does more than just detection. If detection
320 * succeeds, it also registers the new chip.
321 */
322static int adm1025_detect(struct i2c_adapter *adapter, int address, int kind)
323{
324 struct i2c_client *new_client;
325 struct adm1025_data *data;
326 int err = 0;
327 const char *name = "";
328 u8 config;
329
330 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
331 goto exit;
332
333 if (!(data = kmalloc(sizeof(struct adm1025_data), GFP_KERNEL))) {
334 err = -ENOMEM;
335 goto exit;
336 }
337 memset(data, 0, sizeof(struct adm1025_data));
338
339 /* The common I2C client data is placed right before the
340 ADM1025-specific data. */
341 new_client = &data->client;
342 i2c_set_clientdata(new_client, data);
343 new_client->addr = address;
344 new_client->adapter = adapter;
345 new_client->driver = &adm1025_driver;
346 new_client->flags = 0;
347
348 /*
349 * Now we do the remaining detection. A negative kind means that
350 * the driver was loaded with no force parameter (default), so we
351 * must both detect and identify the chip. A zero kind means that
352 * the driver was loaded with the force parameter, the detection
353 * step shall be skipped. A positive kind means that the driver
354 * was loaded with the force parameter and a given kind of chip is
355 * requested, so both the detection and the identification steps
356 * are skipped.
357 */
358 config = i2c_smbus_read_byte_data(new_client, ADM1025_REG_CONFIG);
359 if (kind < 0) { /* detection */
360 if ((config & 0x80) != 0x00
361 || (i2c_smbus_read_byte_data(new_client,
362 ADM1025_REG_STATUS1) & 0xC0) != 0x00
363 || (i2c_smbus_read_byte_data(new_client,
364 ADM1025_REG_STATUS2) & 0xBC) != 0x00) {
365 dev_dbg(&adapter->dev,
366 "ADM1025 detection failed at 0x%02x.\n",
367 address);
368 goto exit_free;
369 }
370 }
371
372 if (kind <= 0) { /* identification */
373 u8 man_id, chip_id;
374
375 man_id = i2c_smbus_read_byte_data(new_client,
376 ADM1025_REG_MAN_ID);
377 chip_id = i2c_smbus_read_byte_data(new_client,
378 ADM1025_REG_CHIP_ID);
379
380 if (man_id == 0x41) { /* Analog Devices */
381 if ((chip_id & 0xF0) == 0x20) { /* ADM1025/ADM1025A */
382 kind = adm1025;
383 }
384 } else
385 if (man_id == 0xA1) { /* Philips */
386 if (address != 0x2E
387 && (chip_id & 0xF0) == 0x20) { /* NE1619 */
388 kind = ne1619;
389 }
390 }
391
392 if (kind <= 0) { /* identification failed */
393 dev_info(&adapter->dev,
394 "Unsupported chip (man_id=0x%02X, "
395 "chip_id=0x%02X).\n", man_id, chip_id);
396 goto exit_free;
397 }
398 }
399
400 if (kind == adm1025) {
401 name = "adm1025";
402 } else if (kind == ne1619) {
403 name = "ne1619";
404 }
405
406 /* We can fill in the remaining client fields */
407 strlcpy(new_client->name, name, I2C_NAME_SIZE);
408 data->valid = 0;
409 init_MUTEX(&data->update_lock);
410
411 /* Tell the I2C layer a new client has arrived */
412 if ((err = i2c_attach_client(new_client)))
413 goto exit_free;
414
415 /* Initialize the ADM1025 chip */
416 adm1025_init_client(new_client);
417
418 /* Register sysfs hooks */
419 device_create_file(&new_client->dev, &dev_attr_in0_input);
420 device_create_file(&new_client->dev, &dev_attr_in1_input);
421 device_create_file(&new_client->dev, &dev_attr_in2_input);
422 device_create_file(&new_client->dev, &dev_attr_in3_input);
423 device_create_file(&new_client->dev, &dev_attr_in5_input);
424 device_create_file(&new_client->dev, &dev_attr_in0_min);
425 device_create_file(&new_client->dev, &dev_attr_in1_min);
426 device_create_file(&new_client->dev, &dev_attr_in2_min);
427 device_create_file(&new_client->dev, &dev_attr_in3_min);
428 device_create_file(&new_client->dev, &dev_attr_in5_min);
429 device_create_file(&new_client->dev, &dev_attr_in0_max);
430 device_create_file(&new_client->dev, &dev_attr_in1_max);
431 device_create_file(&new_client->dev, &dev_attr_in2_max);
432 device_create_file(&new_client->dev, &dev_attr_in3_max);
433 device_create_file(&new_client->dev, &dev_attr_in5_max);
434 device_create_file(&new_client->dev, &dev_attr_temp1_input);
435 device_create_file(&new_client->dev, &dev_attr_temp2_input);
436 device_create_file(&new_client->dev, &dev_attr_temp1_min);
437 device_create_file(&new_client->dev, &dev_attr_temp2_min);
438 device_create_file(&new_client->dev, &dev_attr_temp1_max);
439 device_create_file(&new_client->dev, &dev_attr_temp2_max);
440 device_create_file(&new_client->dev, &dev_attr_alarms);
441 /* in1_ref is deprecated, remove after 2005-11-11 */
442 device_create_file(&new_client->dev, &dev_attr_in1_ref);
443 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
444 device_create_file(&new_client->dev, &dev_attr_vrm);
445
446 /* Pin 11 is either in4 (+12V) or VID4 */
447 if (!(config & 0x20)) {
448 device_create_file(&new_client->dev, &dev_attr_in4_input);
449 device_create_file(&new_client->dev, &dev_attr_in4_min);
450 device_create_file(&new_client->dev, &dev_attr_in4_max);
451 }
452
453 return 0;
454
455exit_free:
456 kfree(data);
457exit:
458 return err;
459}
460
461static void adm1025_init_client(struct i2c_client *client)
462{
463 u8 reg;
464 struct adm1025_data *data = i2c_get_clientdata(client);
465 int i;
466
467 data->vrm = i2c_which_vrm();
468
469 /*
470 * Set high limits
471 * Usually we avoid setting limits on driver init, but it happens
472 * that the ADM1025 comes with stupid default limits (all registers
473 * set to 0). In case the chip has not gone through any limit
474 * setting yet, we better set the high limits to the max so that
475 * no alarm triggers.
476 */
477 for (i=0; i<6; i++) {
478 reg = i2c_smbus_read_byte_data(client,
479 ADM1025_REG_IN_MAX(i));
480 if (reg == 0)
481 i2c_smbus_write_byte_data(client,
482 ADM1025_REG_IN_MAX(i),
483 0xFF);
484 }
485 for (i=0; i<2; i++) {
486 reg = i2c_smbus_read_byte_data(client,
487 ADM1025_REG_TEMP_HIGH(i));
488 if (reg == 0)
489 i2c_smbus_write_byte_data(client,
490 ADM1025_REG_TEMP_HIGH(i),
491 0x7F);
492 }
493
494 /*
495 * Start the conversions
496 */
497 reg = i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG);
498 if (!(reg & 0x01))
499 i2c_smbus_write_byte_data(client, ADM1025_REG_CONFIG,
500 (reg&0x7E)|0x01);
501}
502
503static int adm1025_detach_client(struct i2c_client *client)
504{
505 int err;
506
507 if ((err = i2c_detach_client(client))) {
508 dev_err(&client->dev, "Client deregistration failed, "
509 "client not detached.\n");
510 return err;
511 }
512
513 kfree(i2c_get_clientdata(client));
514 return 0;
515}
516
517static struct adm1025_data *adm1025_update_device(struct device *dev)
518{
519 struct i2c_client *client = to_i2c_client(dev);
520 struct adm1025_data *data = i2c_get_clientdata(client);
521
522 down(&data->update_lock);
523
524 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
525 int i;
526
527 dev_dbg(&client->dev, "Updating data.\n");
528 for (i=0; i<6; i++) {
529 data->in[i] = i2c_smbus_read_byte_data(client,
530 ADM1025_REG_IN(i));
531 data->in_min[i] = i2c_smbus_read_byte_data(client,
532 ADM1025_REG_IN_MIN(i));
533 data->in_max[i] = i2c_smbus_read_byte_data(client,
534 ADM1025_REG_IN_MAX(i));
535 }
536 for (i=0; i<2; i++) {
537 data->temp[i] = i2c_smbus_read_byte_data(client,
538 ADM1025_REG_TEMP(i));
539 data->temp_min[i] = i2c_smbus_read_byte_data(client,
540 ADM1025_REG_TEMP_LOW(i));
541 data->temp_max[i] = i2c_smbus_read_byte_data(client,
542 ADM1025_REG_TEMP_HIGH(i));
543 }
544 data->alarms = i2c_smbus_read_byte_data(client,
545 ADM1025_REG_STATUS1)
546 | (i2c_smbus_read_byte_data(client,
547 ADM1025_REG_STATUS2) << 8);
548 data->vid = (i2c_smbus_read_byte_data(client,
549 ADM1025_REG_VID) & 0x0f)
550 | ((i2c_smbus_read_byte_data(client,
551 ADM1025_REG_VID4) & 0x01) << 4);
552
553 data->last_updated = jiffies;
554 data->valid = 1;
555 }
556
557 up(&data->update_lock);
558
559 return data;
560}
561
562static int __init sensors_adm1025_init(void)
563{
564 return i2c_add_driver(&adm1025_driver);
565}
566
567static void __exit sensors_adm1025_exit(void)
568{
569 i2c_del_driver(&adm1025_driver);
570}
571
572MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
573MODULE_DESCRIPTION("ADM1025 driver");
574MODULE_LICENSE("GPL");
575
576module_init(sensors_adm1025_init);
577module_exit(sensors_adm1025_exit);
diff --git a/drivers/hwmon/adm1026.c b/drivers/hwmon/adm1026.c
new file mode 100644
index 000000000000..3c85fe150cd7
--- /dev/null
+++ b/drivers/hwmon/adm1026.c
@@ -0,0 +1,1714 @@
1/*
2 adm1026.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (C) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com>
5 Copyright (C) 2004 Justin Thiessen <jthiessen@penguincomputing.com>
6
7 Chip details at:
8
9 <http://www.analog.com/UploadedFiles/Data_Sheets/779263102ADM1026_a.pdf>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24*/
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/slab.h>
29#include <linux/jiffies.h>
30#include <linux/i2c.h>
31#include <linux/i2c-sensor.h>
32#include <linux/i2c-vid.h>
33#include <linux/hwmon-sysfs.h>
34
35/* Addresses to scan */
36static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
37static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
38
39/* Insmod parameters */
40SENSORS_INSMOD_1(adm1026);
41
42static int gpio_input[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
43 -1, -1, -1, -1, -1, -1, -1, -1 };
44static int gpio_output[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
45 -1, -1, -1, -1, -1, -1, -1, -1 };
46static int gpio_inverted[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
47 -1, -1, -1, -1, -1, -1, -1, -1 };
48static int gpio_normal[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
49 -1, -1, -1, -1, -1, -1, -1, -1 };
50static int gpio_fan[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
51module_param_array(gpio_input,int,NULL,0);
52MODULE_PARM_DESC(gpio_input,"List of GPIO pins (0-16) to program as inputs");
53module_param_array(gpio_output,int,NULL,0);
54MODULE_PARM_DESC(gpio_output,"List of GPIO pins (0-16) to program as "
55 "outputs");
56module_param_array(gpio_inverted,int,NULL,0);
57MODULE_PARM_DESC(gpio_inverted,"List of GPIO pins (0-16) to program as "
58 "inverted");
59module_param_array(gpio_normal,int,NULL,0);
60MODULE_PARM_DESC(gpio_normal,"List of GPIO pins (0-16) to program as "
61 "normal/non-inverted");
62module_param_array(gpio_fan,int,NULL,0);
63MODULE_PARM_DESC(gpio_fan,"List of GPIO pins (0-7) to program as fan tachs");
64
65/* Many ADM1026 constants specified below */
66
67/* The ADM1026 registers */
68#define ADM1026_REG_CONFIG1 0x00
69#define CFG1_MONITOR 0x01
70#define CFG1_INT_ENABLE 0x02
71#define CFG1_INT_CLEAR 0x04
72#define CFG1_AIN8_9 0x08
73#define CFG1_THERM_HOT 0x10
74#define CFG1_DAC_AFC 0x20
75#define CFG1_PWM_AFC 0x40
76#define CFG1_RESET 0x80
77#define ADM1026_REG_CONFIG2 0x01
78/* CONFIG2 controls FAN0/GPIO0 through FAN7/GPIO7 */
79#define ADM1026_REG_CONFIG3 0x07
80#define CFG3_GPIO16_ENABLE 0x01
81#define CFG3_CI_CLEAR 0x02
82#define CFG3_VREF_250 0x04
83#define CFG3_GPIO16_DIR 0x40
84#define CFG3_GPIO16_POL 0x80
85#define ADM1026_REG_E2CONFIG 0x13
86#define E2CFG_READ 0x01
87#define E2CFG_WRITE 0x02
88#define E2CFG_ERASE 0x04
89#define E2CFG_ROM 0x08
90#define E2CFG_CLK_EXT 0x80
91
92/* There are 10 general analog inputs and 7 dedicated inputs
93 * They are:
94 * 0 - 9 = AIN0 - AIN9
95 * 10 = Vbat
96 * 11 = 3.3V Standby
97 * 12 = 3.3V Main
98 * 13 = +5V
99 * 14 = Vccp (CPU core voltage)
100 * 15 = +12V
101 * 16 = -12V
102 */
103static u16 ADM1026_REG_IN[] = {
104 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
105 0x36, 0x37, 0x27, 0x29, 0x26, 0x2a,
106 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
107 };
108static u16 ADM1026_REG_IN_MIN[] = {
109 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d,
110 0x5e, 0x5f, 0x6d, 0x49, 0x6b, 0x4a,
111 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
112 };
113static u16 ADM1026_REG_IN_MAX[] = {
114 0x50, 0x51, 0x52, 0x53, 0x54, 0x55,
115 0x56, 0x57, 0x6c, 0x41, 0x6a, 0x42,
116 0x43, 0x44, 0x45, 0x46, 0x47
117 };
118
119/* Temperatures are:
120 * 0 - Internal
121 * 1 - External 1
122 * 2 - External 2
123 */
124static u16 ADM1026_REG_TEMP[] = { 0x1f, 0x28, 0x29 };
125static u16 ADM1026_REG_TEMP_MIN[] = { 0x69, 0x48, 0x49 };
126static u16 ADM1026_REG_TEMP_MAX[] = { 0x68, 0x40, 0x41 };
127static u16 ADM1026_REG_TEMP_TMIN[] = { 0x10, 0x11, 0x12 };
128static u16 ADM1026_REG_TEMP_THERM[] = { 0x0d, 0x0e, 0x0f };
129static u16 ADM1026_REG_TEMP_OFFSET[] = { 0x1e, 0x6e, 0x6f };
130
131#define ADM1026_REG_FAN(nr) (0x38 + (nr))
132#define ADM1026_REG_FAN_MIN(nr) (0x60 + (nr))
133#define ADM1026_REG_FAN_DIV_0_3 0x02
134#define ADM1026_REG_FAN_DIV_4_7 0x03
135
136#define ADM1026_REG_DAC 0x04
137#define ADM1026_REG_PWM 0x05
138
139#define ADM1026_REG_GPIO_CFG_0_3 0x08
140#define ADM1026_REG_GPIO_CFG_4_7 0x09
141#define ADM1026_REG_GPIO_CFG_8_11 0x0a
142#define ADM1026_REG_GPIO_CFG_12_15 0x0b
143/* CFG_16 in REG_CFG3 */
144#define ADM1026_REG_GPIO_STATUS_0_7 0x24
145#define ADM1026_REG_GPIO_STATUS_8_15 0x25
146/* STATUS_16 in REG_STATUS4 */
147#define ADM1026_REG_GPIO_MASK_0_7 0x1c
148#define ADM1026_REG_GPIO_MASK_8_15 0x1d
149/* MASK_16 in REG_MASK4 */
150
151#define ADM1026_REG_COMPANY 0x16
152#define ADM1026_REG_VERSTEP 0x17
153/* These are the recognized values for the above regs */
154#define ADM1026_COMPANY_ANALOG_DEV 0x41
155#define ADM1026_VERSTEP_GENERIC 0x40
156#define ADM1026_VERSTEP_ADM1026 0x44
157
158#define ADM1026_REG_MASK1 0x18
159#define ADM1026_REG_MASK2 0x19
160#define ADM1026_REG_MASK3 0x1a
161#define ADM1026_REG_MASK4 0x1b
162
163#define ADM1026_REG_STATUS1 0x20
164#define ADM1026_REG_STATUS2 0x21
165#define ADM1026_REG_STATUS3 0x22
166#define ADM1026_REG_STATUS4 0x23
167
168#define ADM1026_FAN_ACTIVATION_TEMP_HYST -6
169#define ADM1026_FAN_CONTROL_TEMP_RANGE 20
170#define ADM1026_PWM_MAX 255
171
172/* Conversions. Rounding and limit checking is only done on the TO_REG
173 * variants. Note that you should be a bit careful with which arguments
174 * these macros are called: arguments may be evaluated more than once.
175 */
176
177/* IN are scaled acording to built-in resistors. These are the
178 * voltages corresponding to 3/4 of full scale (192 or 0xc0)
179 * NOTE: The -12V input needs an additional factor to account
180 * for the Vref pullup resistor.
181 * NEG12_OFFSET = SCALE * Vref / V-192 - Vref
182 * = 13875 * 2.50 / 1.875 - 2500
183 * = 16000
184 *
185 * The values in this table are based on Table II, page 15 of the
186 * datasheet.
187 */
188static int adm1026_scaling[] = { /* .001 Volts */
189 2250, 2250, 2250, 2250, 2250, 2250,
190 1875, 1875, 1875, 1875, 3000, 3330,
191 3330, 4995, 2250, 12000, 13875
192 };
193#define NEG12_OFFSET 16000
194#define SCALE(val,from,to) (((val)*(to) + ((from)/2))/(from))
195#define INS_TO_REG(n,val) (SENSORS_LIMIT(SCALE(val,adm1026_scaling[n],192),\
196 0,255))
197#define INS_FROM_REG(n,val) (SCALE(val,192,adm1026_scaling[n]))
198
199/* FAN speed is measured using 22.5kHz clock and counts for 2 pulses
200 * and we assume a 2 pulse-per-rev fan tach signal
201 * 22500 kHz * 60 (sec/min) * 2 (pulse) / 2 (pulse/rev) == 1350000
202 */
203#define FAN_TO_REG(val,div) ((val)<=0 ? 0xff : SENSORS_LIMIT(1350000/((val)*\
204 (div)),1,254))
205#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==0xff ? 0 : 1350000/((val)*\
206 (div)))
207#define DIV_FROM_REG(val) (1<<(val))
208#define DIV_TO_REG(val) ((val)>=8 ? 3 : (val)>=4 ? 2 : (val)>=2 ? 1 : 0)
209
210/* Temperature is reported in 1 degC increments */
211#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)+((val)<0 ? -500 : 500))/1000,\
212 -127,127))
213#define TEMP_FROM_REG(val) ((val) * 1000)
214#define OFFSET_TO_REG(val) (SENSORS_LIMIT(((val)+((val)<0 ? -500 : 500))/1000,\
215 -127,127))
216#define OFFSET_FROM_REG(val) ((val) * 1000)
217
218#define PWM_TO_REG(val) (SENSORS_LIMIT(val,0,255))
219#define PWM_FROM_REG(val) (val)
220
221#define PWM_MIN_TO_REG(val) ((val) & 0xf0)
222#define PWM_MIN_FROM_REG(val) (((val) & 0xf0) + ((val) >> 4))
223
224/* Analog output is a voltage, and scaled to millivolts. The datasheet
225 * indicates that the DAC could be used to drive the fans, but in our
226 * example board (Arima HDAMA) it isn't connected to the fans at all.
227 */
228#define DAC_TO_REG(val) (SENSORS_LIMIT(((((val)*255)+500)/2500),0,255))
229#define DAC_FROM_REG(val) (((val)*2500)/255)
230
231/* Typically used with systems using a v9.1 VRM spec ? */
232#define ADM1026_INIT_VRM 91
233
234/* Chip sampling rates
235 *
236 * Some sensors are not updated more frequently than once per second
237 * so it doesn't make sense to read them more often than that.
238 * We cache the results and return the saved data if the driver
239 * is called again before a second has elapsed.
240 *
241 * Also, there is significant configuration data for this chip
242 * So, we keep the config data up to date in the cache
243 * when it is written and only sample it once every 5 *minutes*
244 */
245#define ADM1026_DATA_INTERVAL (1 * HZ)
246#define ADM1026_CONFIG_INTERVAL (5 * 60 * HZ)
247
248/* We allow for multiple chips in a single system.
249 *
250 * For each registered ADM1026, we need to keep state information
251 * at client->data. The adm1026_data structure is dynamically
252 * allocated, when a new client structure is allocated. */
253
254struct pwm_data {
255 u8 pwm;
256 u8 enable;
257 u8 auto_pwm_min;
258};
259
260struct adm1026_data {
261 struct i2c_client client;
262 struct semaphore lock;
263 enum chips type;
264
265 struct semaphore update_lock;
266 int valid; /* !=0 if following fields are valid */
267 unsigned long last_reading; /* In jiffies */
268 unsigned long last_config; /* In jiffies */
269
270 u8 in[17]; /* Register value */
271 u8 in_max[17]; /* Register value */
272 u8 in_min[17]; /* Register value */
273 s8 temp[3]; /* Register value */
274 s8 temp_min[3]; /* Register value */
275 s8 temp_max[3]; /* Register value */
276 s8 temp_tmin[3]; /* Register value */
277 s8 temp_crit[3]; /* Register value */
278 s8 temp_offset[3]; /* Register value */
279 u8 fan[8]; /* Register value */
280 u8 fan_min[8]; /* Register value */
281 u8 fan_div[8]; /* Decoded value */
282 struct pwm_data pwm1; /* Pwm control values */
283 int vid; /* Decoded value */
284 u8 vrm; /* VRM version */
285 u8 analog_out; /* Register value (DAC) */
286 long alarms; /* Register encoding, combined */
287 long alarm_mask; /* Register encoding, combined */
288 long gpio; /* Register encoding, combined */
289 long gpio_mask; /* Register encoding, combined */
290 u8 gpio_config[17]; /* Decoded value */
291 u8 config1; /* Register value */
292 u8 config2; /* Register value */
293 u8 config3; /* Register value */
294};
295
296static int adm1026_attach_adapter(struct i2c_adapter *adapter);
297static int adm1026_detect(struct i2c_adapter *adapter, int address,
298 int kind);
299static int adm1026_detach_client(struct i2c_client *client);
300static int adm1026_read_value(struct i2c_client *client, u8 register);
301static int adm1026_write_value(struct i2c_client *client, u8 register,
302 int value);
303static void adm1026_print_gpio(struct i2c_client *client);
304static void adm1026_fixup_gpio(struct i2c_client *client);
305static struct adm1026_data *adm1026_update_device(struct device *dev);
306static void adm1026_init_client(struct i2c_client *client);
307
308
309static struct i2c_driver adm1026_driver = {
310 .owner = THIS_MODULE,
311 .name = "adm1026",
312 .flags = I2C_DF_NOTIFY,
313 .attach_adapter = adm1026_attach_adapter,
314 .detach_client = adm1026_detach_client,
315};
316
317int adm1026_attach_adapter(struct i2c_adapter *adapter)
318{
319 if (!(adapter->class & I2C_CLASS_HWMON)) {
320 return 0;
321 }
322 return i2c_detect(adapter, &addr_data, adm1026_detect);
323}
324
325int adm1026_detach_client(struct i2c_client *client)
326{
327 i2c_detach_client(client);
328 kfree(client);
329 return 0;
330}
331
332int adm1026_read_value(struct i2c_client *client, u8 reg)
333{
334 int res;
335
336 if (reg < 0x80) {
337 /* "RAM" locations */
338 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
339 } else {
340 /* EEPROM, do nothing */
341 res = 0;
342 }
343 return res;
344}
345
346int adm1026_write_value(struct i2c_client *client, u8 reg, int value)
347{
348 int res;
349
350 if (reg < 0x80) {
351 /* "RAM" locations */
352 res = i2c_smbus_write_byte_data(client, reg, value);
353 } else {
354 /* EEPROM, do nothing */
355 res = 0;
356 }
357 return res;
358}
359
360void adm1026_init_client(struct i2c_client *client)
361{
362 int value, i;
363 struct adm1026_data *data = i2c_get_clientdata(client);
364
365 dev_dbg(&client->dev, "Initializing device\n");
366 /* Read chip config */
367 data->config1 = adm1026_read_value(client, ADM1026_REG_CONFIG1);
368 data->config2 = adm1026_read_value(client, ADM1026_REG_CONFIG2);
369 data->config3 = adm1026_read_value(client, ADM1026_REG_CONFIG3);
370
371 /* Inform user of chip config */
372 dev_dbg(&client->dev, "ADM1026_REG_CONFIG1 is: 0x%02x\n",
373 data->config1);
374 if ((data->config1 & CFG1_MONITOR) == 0) {
375 dev_dbg(&client->dev, "Monitoring not currently "
376 "enabled.\n");
377 }
378 if (data->config1 & CFG1_INT_ENABLE) {
379 dev_dbg(&client->dev, "SMBALERT interrupts are "
380 "enabled.\n");
381 }
382 if (data->config1 & CFG1_AIN8_9) {
383 dev_dbg(&client->dev, "in8 and in9 enabled. "
384 "temp3 disabled.\n");
385 } else {
386 dev_dbg(&client->dev, "temp3 enabled. in8 and "
387 "in9 disabled.\n");
388 }
389 if (data->config1 & CFG1_THERM_HOT) {
390 dev_dbg(&client->dev, "Automatic THERM, PWM, "
391 "and temp limits enabled.\n");
392 }
393
394 value = data->config3;
395 if (data->config3 & CFG3_GPIO16_ENABLE) {
396 dev_dbg(&client->dev, "GPIO16 enabled. THERM"
397 "pin disabled.\n");
398 } else {
399 dev_dbg(&client->dev, "THERM pin enabled. "
400 "GPIO16 disabled.\n");
401 }
402 if (data->config3 & CFG3_VREF_250) {
403 dev_dbg(&client->dev, "Vref is 2.50 Volts.\n");
404 } else {
405 dev_dbg(&client->dev, "Vref is 1.82 Volts.\n");
406 }
407 /* Read and pick apart the existing GPIO configuration */
408 value = 0;
409 for (i = 0;i <= 15;++i) {
410 if ((i & 0x03) == 0) {
411 value = adm1026_read_value(client,
412 ADM1026_REG_GPIO_CFG_0_3 + i/4);
413 }
414 data->gpio_config[i] = value & 0x03;
415 value >>= 2;
416 }
417 data->gpio_config[16] = (data->config3 >> 6) & 0x03;
418
419 /* ... and then print it */
420 adm1026_print_gpio(client);
421
422 /* If the user asks us to reprogram the GPIO config, then
423 * do it now.
424 */
425 if (gpio_input[0] != -1 || gpio_output[0] != -1
426 || gpio_inverted[0] != -1 || gpio_normal[0] != -1
427 || gpio_fan[0] != -1) {
428 adm1026_fixup_gpio(client);
429 }
430
431 /* WE INTENTIONALLY make no changes to the limits,
432 * offsets, pwms, fans and zones. If they were
433 * configured, we don't want to mess with them.
434 * If they weren't, the default is 100% PWM, no
435 * control and will suffice until 'sensors -s'
436 * can be run by the user. We DO set the default
437 * value for pwm1.auto_pwm_min to its maximum
438 * so that enabling automatic pwm fan control
439 * without first setting a value for pwm1.auto_pwm_min
440 * will not result in potentially dangerous fan speed decrease.
441 */
442 data->pwm1.auto_pwm_min=255;
443 /* Start monitoring */
444 value = adm1026_read_value(client, ADM1026_REG_CONFIG1);
445 /* Set MONITOR, clear interrupt acknowledge and s/w reset */
446 value = (value | CFG1_MONITOR) & (~CFG1_INT_CLEAR & ~CFG1_RESET);
447 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
448 data->config1 = value;
449 adm1026_write_value(client, ADM1026_REG_CONFIG1, value);
450
451 /* initialize fan_div[] to hardware defaults */
452 value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3) |
453 (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) << 8);
454 for (i = 0;i <= 7;++i) {
455 data->fan_div[i] = DIV_FROM_REG(value & 0x03);
456 value >>= 2;
457 }
458}
459
460void adm1026_print_gpio(struct i2c_client *client)
461{
462 struct adm1026_data *data = i2c_get_clientdata(client);
463 int i;
464
465 dev_dbg(&client->dev, "GPIO config is:");
466 for (i = 0;i <= 7;++i) {
467 if (data->config2 & (1 << i)) {
468 dev_dbg(&client->dev, "\t%sGP%s%d\n",
469 data->gpio_config[i] & 0x02 ? "" : "!",
470 data->gpio_config[i] & 0x01 ? "OUT" : "IN",
471 i);
472 } else {
473 dev_dbg(&client->dev, "\tFAN%d\n", i);
474 }
475 }
476 for (i = 8;i <= 15;++i) {
477 dev_dbg(&client->dev, "\t%sGP%s%d\n",
478 data->gpio_config[i] & 0x02 ? "" : "!",
479 data->gpio_config[i] & 0x01 ? "OUT" : "IN",
480 i);
481 }
482 if (data->config3 & CFG3_GPIO16_ENABLE) {
483 dev_dbg(&client->dev, "\t%sGP%s16\n",
484 data->gpio_config[16] & 0x02 ? "" : "!",
485 data->gpio_config[16] & 0x01 ? "OUT" : "IN");
486 } else {
487 /* GPIO16 is THERM */
488 dev_dbg(&client->dev, "\tTHERM\n");
489 }
490}
491
492void adm1026_fixup_gpio(struct i2c_client *client)
493{
494 struct adm1026_data *data = i2c_get_clientdata(client);
495 int i;
496 int value;
497
498 /* Make the changes requested. */
499 /* We may need to unlock/stop monitoring or soft-reset the
500 * chip before we can make changes. This hasn't been
501 * tested much. FIXME
502 */
503
504 /* Make outputs */
505 for (i = 0;i <= 16;++i) {
506 if (gpio_output[i] >= 0 && gpio_output[i] <= 16) {
507 data->gpio_config[gpio_output[i]] |= 0x01;
508 }
509 /* if GPIO0-7 is output, it isn't a FAN tach */
510 if (gpio_output[i] >= 0 && gpio_output[i] <= 7) {
511 data->config2 |= 1 << gpio_output[i];
512 }
513 }
514
515 /* Input overrides output */
516 for (i = 0;i <= 16;++i) {
517 if (gpio_input[i] >= 0 && gpio_input[i] <= 16) {
518 data->gpio_config[gpio_input[i]] &= ~ 0x01;
519 }
520 /* if GPIO0-7 is input, it isn't a FAN tach */
521 if (gpio_input[i] >= 0 && gpio_input[i] <= 7) {
522 data->config2 |= 1 << gpio_input[i];
523 }
524 }
525
526 /* Inverted */
527 for (i = 0;i <= 16;++i) {
528 if (gpio_inverted[i] >= 0 && gpio_inverted[i] <= 16) {
529 data->gpio_config[gpio_inverted[i]] &= ~ 0x02;
530 }
531 }
532
533 /* Normal overrides inverted */
534 for (i = 0;i <= 16;++i) {
535 if (gpio_normal[i] >= 0 && gpio_normal[i] <= 16) {
536 data->gpio_config[gpio_normal[i]] |= 0x02;
537 }
538 }
539
540 /* Fan overrides input and output */
541 for (i = 0;i <= 7;++i) {
542 if (gpio_fan[i] >= 0 && gpio_fan[i] <= 7) {
543 data->config2 &= ~(1 << gpio_fan[i]);
544 }
545 }
546
547 /* Write new configs to registers */
548 adm1026_write_value(client, ADM1026_REG_CONFIG2, data->config2);
549 data->config3 = (data->config3 & 0x3f)
550 | ((data->gpio_config[16] & 0x03) << 6);
551 adm1026_write_value(client, ADM1026_REG_CONFIG3, data->config3);
552 for (i = 15, value = 0;i >= 0;--i) {
553 value <<= 2;
554 value |= data->gpio_config[i] & 0x03;
555 if ((i & 0x03) == 0) {
556 adm1026_write_value(client,
557 ADM1026_REG_GPIO_CFG_0_3 + i/4,
558 value);
559 value = 0;
560 }
561 }
562
563 /* Print the new config */
564 adm1026_print_gpio(client);
565}
566
567
568static struct adm1026_data *adm1026_update_device(struct device *dev)
569{
570 struct i2c_client *client = to_i2c_client(dev);
571 struct adm1026_data *data = i2c_get_clientdata(client);
572 int i;
573 long value, alarms, gpio;
574
575 down(&data->update_lock);
576 if (!data->valid
577 || time_after(jiffies, data->last_reading + ADM1026_DATA_INTERVAL)) {
578 /* Things that change quickly */
579 dev_dbg(&client->dev,"Reading sensor values\n");
580 for (i = 0;i <= 16;++i) {
581 data->in[i] =
582 adm1026_read_value(client, ADM1026_REG_IN[i]);
583 }
584
585 for (i = 0;i <= 7;++i) {
586 data->fan[i] =
587 adm1026_read_value(client, ADM1026_REG_FAN(i));
588 }
589
590 for (i = 0;i <= 2;++i) {
591 /* NOTE: temp[] is s8 and we assume 2's complement
592 * "conversion" in the assignment */
593 data->temp[i] =
594 adm1026_read_value(client, ADM1026_REG_TEMP[i]);
595 }
596
597 data->pwm1.pwm = adm1026_read_value(client,
598 ADM1026_REG_PWM);
599 data->analog_out = adm1026_read_value(client,
600 ADM1026_REG_DAC);
601 /* GPIO16 is MSbit of alarms, move it to gpio */
602 alarms = adm1026_read_value(client, ADM1026_REG_STATUS4);
603 gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
604 alarms &= 0x7f;
605 alarms <<= 8;
606 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS3);
607 alarms <<= 8;
608 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS2);
609 alarms <<= 8;
610 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS1);
611 data->alarms = alarms;
612
613 /* Read the GPIO values */
614 gpio |= adm1026_read_value(client,
615 ADM1026_REG_GPIO_STATUS_8_15);
616 gpio <<= 8;
617 gpio |= adm1026_read_value(client,
618 ADM1026_REG_GPIO_STATUS_0_7);
619 data->gpio = gpio;
620
621 data->last_reading = jiffies;
622 }; /* last_reading */
623
624 if (!data->valid ||
625 time_after(jiffies, data->last_config + ADM1026_CONFIG_INTERVAL)) {
626 /* Things that don't change often */
627 dev_dbg(&client->dev, "Reading config values\n");
628 for (i = 0;i <= 16;++i) {
629 data->in_min[i] = adm1026_read_value(client,
630 ADM1026_REG_IN_MIN[i]);
631 data->in_max[i] = adm1026_read_value(client,
632 ADM1026_REG_IN_MAX[i]);
633 }
634
635 value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3)
636 | (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7)
637 << 8);
638 for (i = 0;i <= 7;++i) {
639 data->fan_min[i] = adm1026_read_value(client,
640 ADM1026_REG_FAN_MIN(i));
641 data->fan_div[i] = DIV_FROM_REG(value & 0x03);
642 value >>= 2;
643 }
644
645 for (i = 0; i <= 2; ++i) {
646 /* NOTE: temp_xxx[] are s8 and we assume 2's
647 * complement "conversion" in the assignment
648 */
649 data->temp_min[i] = adm1026_read_value(client,
650 ADM1026_REG_TEMP_MIN[i]);
651 data->temp_max[i] = adm1026_read_value(client,
652 ADM1026_REG_TEMP_MAX[i]);
653 data->temp_tmin[i] = adm1026_read_value(client,
654 ADM1026_REG_TEMP_TMIN[i]);
655 data->temp_crit[i] = adm1026_read_value(client,
656 ADM1026_REG_TEMP_THERM[i]);
657 data->temp_offset[i] = adm1026_read_value(client,
658 ADM1026_REG_TEMP_OFFSET[i]);
659 }
660
661 /* Read the STATUS/alarm masks */
662 alarms = adm1026_read_value(client, ADM1026_REG_MASK4);
663 gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
664 alarms = (alarms & 0x7f) << 8;
665 alarms |= adm1026_read_value(client, ADM1026_REG_MASK3);
666 alarms <<= 8;
667 alarms |= adm1026_read_value(client, ADM1026_REG_MASK2);
668 alarms <<= 8;
669 alarms |= adm1026_read_value(client, ADM1026_REG_MASK1);
670 data->alarm_mask = alarms;
671
672 /* Read the GPIO values */
673 gpio |= adm1026_read_value(client,
674 ADM1026_REG_GPIO_MASK_8_15);
675 gpio <<= 8;
676 gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_0_7);
677 data->gpio_mask = gpio;
678
679 /* Read various values from CONFIG1 */
680 data->config1 = adm1026_read_value(client,
681 ADM1026_REG_CONFIG1);
682 if (data->config1 & CFG1_PWM_AFC) {
683 data->pwm1.enable = 2;
684 data->pwm1.auto_pwm_min =
685 PWM_MIN_FROM_REG(data->pwm1.pwm);
686 }
687 /* Read the GPIO config */
688 data->config2 = adm1026_read_value(client,
689 ADM1026_REG_CONFIG2);
690 data->config3 = adm1026_read_value(client,
691 ADM1026_REG_CONFIG3);
692 data->gpio_config[16] = (data->config3 >> 6) & 0x03;
693
694 value = 0;
695 for (i = 0;i <= 15;++i) {
696 if ((i & 0x03) == 0) {
697 value = adm1026_read_value(client,
698 ADM1026_REG_GPIO_CFG_0_3 + i/4);
699 }
700 data->gpio_config[i] = value & 0x03;
701 value >>= 2;
702 }
703
704 data->last_config = jiffies;
705 }; /* last_config */
706
707 dev_dbg(&client->dev, "Setting VID from GPIO11-15.\n");
708 data->vid = (data->gpio >> 11) & 0x1f;
709 data->valid = 1;
710 up(&data->update_lock);
711 return data;
712}
713
714static ssize_t show_in(struct device *dev, struct device_attribute *attr,
715 char *buf)
716{
717 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
718 int nr = sensor_attr->index;
719 struct adm1026_data *data = adm1026_update_device(dev);
720 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in[nr]));
721}
722static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
723 char *buf)
724{
725 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
726 int nr = sensor_attr->index;
727 struct adm1026_data *data = adm1026_update_device(dev);
728 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]));
729}
730static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
731 const char *buf, size_t count)
732{
733 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
734 int nr = sensor_attr->index;
735 struct i2c_client *client = to_i2c_client(dev);
736 struct adm1026_data *data = i2c_get_clientdata(client);
737 int val = simple_strtol(buf, NULL, 10);
738
739 down(&data->update_lock);
740 data->in_min[nr] = INS_TO_REG(nr, val);
741 adm1026_write_value(client, ADM1026_REG_IN_MIN[nr], data->in_min[nr]);
742 up(&data->update_lock);
743 return count;
744}
745static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
746 char *buf)
747{
748 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
749 int nr = sensor_attr->index;
750 struct adm1026_data *data = adm1026_update_device(dev);
751 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]));
752}
753static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
754 const char *buf, size_t count)
755{
756 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
757 int nr = sensor_attr->index;
758 struct i2c_client *client = to_i2c_client(dev);
759 struct adm1026_data *data = i2c_get_clientdata(client);
760 int val = simple_strtol(buf, NULL, 10);
761
762 down(&data->update_lock);
763 data->in_max[nr] = INS_TO_REG(nr, val);
764 adm1026_write_value(client, ADM1026_REG_IN_MAX[nr], data->in_max[nr]);
765 up(&data->update_lock);
766 return count;
767}
768
769#define in_reg(offset) \
770static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in, \
771 NULL, offset); \
772static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
773 show_in_min, set_in_min, offset); \
774static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
775 show_in_max, set_in_max, offset);
776
777
778in_reg(0);
779in_reg(1);
780in_reg(2);
781in_reg(3);
782in_reg(4);
783in_reg(5);
784in_reg(6);
785in_reg(7);
786in_reg(8);
787in_reg(9);
788in_reg(10);
789in_reg(11);
790in_reg(12);
791in_reg(13);
792in_reg(14);
793in_reg(15);
794
795static ssize_t show_in16(struct device *dev, struct device_attribute *attr, char *buf)
796{
797 struct adm1026_data *data = adm1026_update_device(dev);
798 return sprintf(buf,"%d\n", INS_FROM_REG(16, data->in[16]) -
799 NEG12_OFFSET);
800}
801static ssize_t show_in16_min(struct device *dev, struct device_attribute *attr, char *buf)
802{
803 struct adm1026_data *data = adm1026_update_device(dev);
804 return sprintf(buf,"%d\n", INS_FROM_REG(16, data->in_min[16])
805 - NEG12_OFFSET);
806}
807static ssize_t set_in16_min(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
808{
809 struct i2c_client *client = to_i2c_client(dev);
810 struct adm1026_data *data = i2c_get_clientdata(client);
811 int val = simple_strtol(buf, NULL, 10);
812
813 down(&data->update_lock);
814 data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
815 adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
816 up(&data->update_lock);
817 return count;
818}
819static ssize_t show_in16_max(struct device *dev, struct device_attribute *attr, char *buf)
820{
821 struct adm1026_data *data = adm1026_update_device(dev);
822 return sprintf(buf,"%d\n", INS_FROM_REG(16, data->in_max[16])
823 - NEG12_OFFSET);
824}
825static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
826{
827 struct i2c_client *client = to_i2c_client(dev);
828 struct adm1026_data *data = i2c_get_clientdata(client);
829 int val = simple_strtol(buf, NULL, 10);
830
831 down(&data->update_lock);
832 data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
833 adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
834 up(&data->update_lock);
835 return count;
836}
837
838static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_in16, NULL, 16);
839static SENSOR_DEVICE_ATTR(in16_min, S_IRUGO | S_IWUSR, show_in16_min, set_in16_min, 16);
840static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, set_in16_max, 16);
841
842
843
844
845/* Now add fan read/write functions */
846
847static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
848 char *buf)
849{
850 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
851 int nr = sensor_attr->index;
852 struct adm1026_data *data = adm1026_update_device(dev);
853 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
854 data->fan_div[nr]));
855}
856static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
857 char *buf)
858{
859 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
860 int nr = sensor_attr->index;
861 struct adm1026_data *data = adm1026_update_device(dev);
862 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
863 data->fan_div[nr]));
864}
865static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
866 const char *buf, size_t count)
867{
868 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
869 int nr = sensor_attr->index;
870 struct i2c_client *client = to_i2c_client(dev);
871 struct adm1026_data *data = i2c_get_clientdata(client);
872 int val = simple_strtol(buf, NULL, 10);
873
874 down(&data->update_lock);
875 data->fan_min[nr] = FAN_TO_REG(val, data->fan_div[nr]);
876 adm1026_write_value(client, ADM1026_REG_FAN_MIN(nr),
877 data->fan_min[nr]);
878 up(&data->update_lock);
879 return count;
880}
881
882#define fan_offset(offset) \
883static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan, NULL, \
884 offset - 1); \
885static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
886 show_fan_min, set_fan_min, offset - 1);
887
888fan_offset(1);
889fan_offset(2);
890fan_offset(3);
891fan_offset(4);
892fan_offset(5);
893fan_offset(6);
894fan_offset(7);
895fan_offset(8);
896
897/* Adjust fan_min to account for new fan divisor */
898static void fixup_fan_min(struct device *dev, int fan, int old_div)
899{
900 struct i2c_client *client = to_i2c_client(dev);
901 struct adm1026_data *data = i2c_get_clientdata(client);
902 int new_min;
903 int new_div = data->fan_div[fan];
904
905 /* 0 and 0xff are special. Don't adjust them */
906 if (data->fan_min[fan] == 0 || data->fan_min[fan] == 0xff) {
907 return;
908 }
909
910 new_min = data->fan_min[fan] * old_div / new_div;
911 new_min = SENSORS_LIMIT(new_min, 1, 254);
912 data->fan_min[fan] = new_min;
913 adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min);
914}
915
916/* Now add fan_div read/write functions */
917static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
918 char *buf)
919{
920 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
921 int nr = sensor_attr->index;
922 struct adm1026_data *data = adm1026_update_device(dev);
923 return sprintf(buf,"%d\n", data->fan_div[nr]);
924}
925static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
926 const char *buf, size_t count)
927{
928 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
929 int nr = sensor_attr->index;
930 struct i2c_client *client = to_i2c_client(dev);
931 struct adm1026_data *data = i2c_get_clientdata(client);
932 int val,orig_div,new_div,shift;
933
934 val = simple_strtol(buf, NULL, 10);
935 new_div = DIV_TO_REG(val);
936 if (new_div == 0) {
937 return -EINVAL;
938 }
939 down(&data->update_lock);
940 orig_div = data->fan_div[nr];
941 data->fan_div[nr] = DIV_FROM_REG(new_div);
942
943 if (nr < 4) { /* 0 <= nr < 4 */
944 shift = 2 * nr;
945 adm1026_write_value(client, ADM1026_REG_FAN_DIV_0_3,
946 ((DIV_TO_REG(orig_div) & (~(0x03 << shift))) |
947 (new_div << shift)));
948 } else { /* 3 < nr < 8 */
949 shift = 2 * (nr - 4);
950 adm1026_write_value(client, ADM1026_REG_FAN_DIV_4_7,
951 ((DIV_TO_REG(orig_div) & (~(0x03 << (2 * shift)))) |
952 (new_div << shift)));
953 }
954
955 if (data->fan_div[nr] != orig_div) {
956 fixup_fan_min(dev,nr,orig_div);
957 }
958 up(&data->update_lock);
959 return count;
960}
961
962#define fan_offset_div(offset) \
963static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
964 show_fan_div, set_fan_div, offset - 1);
965
966fan_offset_div(1);
967fan_offset_div(2);
968fan_offset_div(3);
969fan_offset_div(4);
970fan_offset_div(5);
971fan_offset_div(6);
972fan_offset_div(7);
973fan_offset_div(8);
974
975/* Temps */
976static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
977 char *buf)
978{
979 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
980 int nr = sensor_attr->index;
981 struct adm1026_data *data = adm1026_update_device(dev);
982 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp[nr]));
983}
984static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
985 char *buf)
986{
987 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
988 int nr = sensor_attr->index;
989 struct adm1026_data *data = adm1026_update_device(dev);
990 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]));
991}
992static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
993 const char *buf, size_t count)
994{
995 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
996 int nr = sensor_attr->index;
997 struct i2c_client *client = to_i2c_client(dev);
998 struct adm1026_data *data = i2c_get_clientdata(client);
999 int val = simple_strtol(buf, NULL, 10);
1000
1001 down(&data->update_lock);
1002 data->temp_min[nr] = TEMP_TO_REG(val);
1003 adm1026_write_value(client, ADM1026_REG_TEMP_MIN[nr],
1004 data->temp_min[nr]);
1005 up(&data->update_lock);
1006 return count;
1007}
1008static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
1009 char *buf)
1010{
1011 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1012 int nr = sensor_attr->index;
1013 struct adm1026_data *data = adm1026_update_device(dev);
1014 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]));
1015}
1016static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
1017 const char *buf, size_t count)
1018{
1019 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1020 int nr = sensor_attr->index;
1021 struct i2c_client *client = to_i2c_client(dev);
1022 struct adm1026_data *data = i2c_get_clientdata(client);
1023 int val = simple_strtol(buf, NULL, 10);
1024
1025 down(&data->update_lock);
1026 data->temp_max[nr] = TEMP_TO_REG(val);
1027 adm1026_write_value(client, ADM1026_REG_TEMP_MAX[nr],
1028 data->temp_max[nr]);
1029 up(&data->update_lock);
1030 return count;
1031}
1032
1033#define temp_reg(offset) \
1034static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \
1035 NULL, offset - 1); \
1036static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
1037 show_temp_min, set_temp_min, offset - 1); \
1038static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
1039 show_temp_max, set_temp_max, offset - 1);
1040
1041
1042temp_reg(1);
1043temp_reg(2);
1044temp_reg(3);
1045
1046static ssize_t show_temp_offset(struct device *dev,
1047 struct device_attribute *attr, char *buf)
1048{
1049 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1050 int nr = sensor_attr->index;
1051 struct adm1026_data *data = adm1026_update_device(dev);
1052 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
1053}
1054static ssize_t set_temp_offset(struct device *dev,
1055 struct device_attribute *attr, const char *buf,
1056 size_t count)
1057{
1058 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1059 int nr = sensor_attr->index;
1060 struct i2c_client *client = to_i2c_client(dev);
1061 struct adm1026_data *data = i2c_get_clientdata(client);
1062 int val = simple_strtol(buf, NULL, 10);
1063
1064 down(&data->update_lock);
1065 data->temp_offset[nr] = TEMP_TO_REG(val);
1066 adm1026_write_value(client, ADM1026_REG_TEMP_OFFSET[nr],
1067 data->temp_offset[nr]);
1068 up(&data->update_lock);
1069 return count;
1070}
1071
1072#define temp_offset_reg(offset) \
1073static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
1074 show_temp_offset, set_temp_offset, offset - 1);
1075
1076temp_offset_reg(1);
1077temp_offset_reg(2);
1078temp_offset_reg(3);
1079
1080static ssize_t show_temp_auto_point1_temp_hyst(struct device *dev,
1081 struct device_attribute *attr, char *buf)
1082{
1083 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1084 int nr = sensor_attr->index;
1085 struct adm1026_data *data = adm1026_update_device(dev);
1086 return sprintf(buf,"%d\n", TEMP_FROM_REG(
1087 ADM1026_FAN_ACTIVATION_TEMP_HYST + data->temp_tmin[nr]));
1088}
1089static ssize_t show_temp_auto_point2_temp(struct device *dev,
1090 struct device_attribute *attr, char *buf)
1091{
1092 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1093 int nr = sensor_attr->index;
1094 struct adm1026_data *data = adm1026_update_device(dev);
1095 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_tmin[nr] +
1096 ADM1026_FAN_CONTROL_TEMP_RANGE));
1097}
1098static ssize_t show_temp_auto_point1_temp(struct device *dev,
1099 struct device_attribute *attr, char *buf)
1100{
1101 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1102 int nr = sensor_attr->index;
1103 struct adm1026_data *data = adm1026_update_device(dev);
1104 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_tmin[nr]));
1105}
1106static ssize_t set_temp_auto_point1_temp(struct device *dev,
1107 struct device_attribute *attr, const char *buf, size_t count)
1108{
1109 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1110 int nr = sensor_attr->index;
1111 struct i2c_client *client = to_i2c_client(dev);
1112 struct adm1026_data *data = i2c_get_clientdata(client);
1113 int val = simple_strtol(buf, NULL, 10);
1114
1115 down(&data->update_lock);
1116 data->temp_tmin[nr] = TEMP_TO_REG(val);
1117 adm1026_write_value(client, ADM1026_REG_TEMP_TMIN[nr],
1118 data->temp_tmin[nr]);
1119 up(&data->update_lock);
1120 return count;
1121}
1122
1123#define temp_auto_point(offset) \
1124static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp, S_IRUGO | S_IWUSR, \
1125 show_temp_auto_point1_temp, set_temp_auto_point1_temp, \
1126 offset - 1); \
1127static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp_hyst, S_IRUGO, \
1128 show_temp_auto_point1_temp_hyst, NULL, offset - 1); \
1129static SENSOR_DEVICE_ATTR(temp##offset##_auto_point2_temp, S_IRUGO, \
1130 show_temp_auto_point2_temp, NULL, offset - 1);
1131
1132temp_auto_point(1);
1133temp_auto_point(2);
1134temp_auto_point(3);
1135
1136static ssize_t show_temp_crit_enable(struct device *dev,
1137 struct device_attribute *attr, char *buf)
1138{
1139 struct adm1026_data *data = adm1026_update_device(dev);
1140 return sprintf(buf,"%d\n", (data->config1 & CFG1_THERM_HOT) >> 4);
1141}
1142static ssize_t set_temp_crit_enable(struct device *dev,
1143 struct device_attribute *attr, const char *buf, size_t count)
1144{
1145 struct i2c_client *client = to_i2c_client(dev);
1146 struct adm1026_data *data = i2c_get_clientdata(client);
1147 int val = simple_strtol(buf, NULL, 10);
1148
1149 if ((val == 1) || (val==0)) {
1150 down(&data->update_lock);
1151 data->config1 = (data->config1 & ~CFG1_THERM_HOT) | (val << 4);
1152 adm1026_write_value(client, ADM1026_REG_CONFIG1,
1153 data->config1);
1154 up(&data->update_lock);
1155 }
1156 return count;
1157}
1158
1159#define temp_crit_enable(offset) \
1160static DEVICE_ATTR(temp##offset##_crit_enable, S_IRUGO | S_IWUSR, \
1161 show_temp_crit_enable, set_temp_crit_enable);
1162
1163temp_crit_enable(1);
1164temp_crit_enable(2);
1165temp_crit_enable(3);
1166
1167static ssize_t show_temp_crit(struct device *dev,
1168 struct device_attribute *attr, char *buf)
1169{
1170 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1171 int nr = sensor_attr->index;
1172 struct adm1026_data *data = adm1026_update_device(dev);
1173 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
1174}
1175static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
1176 const char *buf, size_t count)
1177{
1178 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1179 int nr = sensor_attr->index;
1180 struct i2c_client *client = to_i2c_client(dev);
1181 struct adm1026_data *data = i2c_get_clientdata(client);
1182 int val = simple_strtol(buf, NULL, 10);
1183
1184 down(&data->update_lock);
1185 data->temp_crit[nr] = TEMP_TO_REG(val);
1186 adm1026_write_value(client, ADM1026_REG_TEMP_THERM[nr],
1187 data->temp_crit[nr]);
1188 up(&data->update_lock);
1189 return count;
1190}
1191
1192#define temp_crit_reg(offset) \
1193static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
1194 show_temp_crit, set_temp_crit, offset - 1);
1195
1196temp_crit_reg(1);
1197temp_crit_reg(2);
1198temp_crit_reg(3);
1199
1200static ssize_t show_analog_out_reg(struct device *dev, struct device_attribute *attr, char *buf)
1201{
1202 struct adm1026_data *data = adm1026_update_device(dev);
1203 return sprintf(buf,"%d\n", DAC_FROM_REG(data->analog_out));
1204}
1205static ssize_t set_analog_out_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1206 size_t count)
1207{
1208 struct i2c_client *client = to_i2c_client(dev);
1209 struct adm1026_data *data = i2c_get_clientdata(client);
1210 int val = simple_strtol(buf, NULL, 10);
1211
1212 down(&data->update_lock);
1213 data->analog_out = DAC_TO_REG(val);
1214 adm1026_write_value(client, ADM1026_REG_DAC, data->analog_out);
1215 up(&data->update_lock);
1216 return count;
1217}
1218
1219static DEVICE_ATTR(analog_out, S_IRUGO | S_IWUSR, show_analog_out_reg,
1220 set_analog_out_reg);
1221
1222static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
1223{
1224 struct adm1026_data *data = adm1026_update_device(dev);
1225 return sprintf(buf,"%d\n", vid_from_reg(data->vid & 0x3f, data->vrm));
1226}
1227/* vid deprecated in favour of cpu0_vid, remove after 2005-11-11 */
1228static DEVICE_ATTR(vid, S_IRUGO, show_vid_reg, NULL);
1229static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1230
1231static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
1232{
1233 struct adm1026_data *data = adm1026_update_device(dev);
1234 return sprintf(buf,"%d\n", data->vrm);
1235}
1236static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1237 size_t count)
1238{
1239 struct i2c_client *client = to_i2c_client(dev);
1240 struct adm1026_data *data = i2c_get_clientdata(client);
1241
1242 data->vrm = simple_strtol(buf, NULL, 10);
1243 return count;
1244}
1245
1246static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1247
1248static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
1249{
1250 struct adm1026_data *data = adm1026_update_device(dev);
1251 return sprintf(buf, "%ld\n", (long) (data->alarms));
1252}
1253
1254static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
1255
1256static ssize_t show_alarm_mask(struct device *dev, struct device_attribute *attr, char *buf)
1257{
1258 struct adm1026_data *data = adm1026_update_device(dev);
1259 return sprintf(buf,"%ld\n", data->alarm_mask);
1260}
1261static ssize_t set_alarm_mask(struct device *dev, struct device_attribute *attr, const char *buf,
1262 size_t count)
1263{
1264 struct i2c_client *client = to_i2c_client(dev);
1265 struct adm1026_data *data = i2c_get_clientdata(client);
1266 int val = simple_strtol(buf, NULL, 10);
1267 unsigned long mask;
1268
1269 down(&data->update_lock);
1270 data->alarm_mask = val & 0x7fffffff;
1271 mask = data->alarm_mask
1272 | (data->gpio_mask & 0x10000 ? 0x80000000 : 0);
1273 adm1026_write_value(client, ADM1026_REG_MASK1,
1274 mask & 0xff);
1275 mask >>= 8;
1276 adm1026_write_value(client, ADM1026_REG_MASK2,
1277 mask & 0xff);
1278 mask >>= 8;
1279 adm1026_write_value(client, ADM1026_REG_MASK3,
1280 mask & 0xff);
1281 mask >>= 8;
1282 adm1026_write_value(client, ADM1026_REG_MASK4,
1283 mask & 0xff);
1284 up(&data->update_lock);
1285 return count;
1286}
1287
1288static DEVICE_ATTR(alarm_mask, S_IRUGO | S_IWUSR, show_alarm_mask,
1289 set_alarm_mask);
1290
1291
1292static ssize_t show_gpio(struct device *dev, struct device_attribute *attr, char *buf)
1293{
1294 struct adm1026_data *data = adm1026_update_device(dev);
1295 return sprintf(buf,"%ld\n", data->gpio);
1296}
1297static ssize_t set_gpio(struct device *dev, struct device_attribute *attr, const char *buf,
1298 size_t count)
1299{
1300 struct i2c_client *client = to_i2c_client(dev);
1301 struct adm1026_data *data = i2c_get_clientdata(client);
1302 int val = simple_strtol(buf, NULL, 10);
1303 long gpio;
1304
1305 down(&data->update_lock);
1306 data->gpio = val & 0x1ffff;
1307 gpio = data->gpio;
1308 adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_0_7,gpio & 0xff);
1309 gpio >>= 8;
1310 adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_8_15,gpio & 0xff);
1311 gpio = ((gpio >> 1) & 0x80) | (data->alarms >> 24 & 0x7f);
1312 adm1026_write_value(client, ADM1026_REG_STATUS4,gpio & 0xff);
1313 up(&data->update_lock);
1314 return count;
1315}
1316
1317static DEVICE_ATTR(gpio, S_IRUGO | S_IWUSR, show_gpio, set_gpio);
1318
1319
1320static ssize_t show_gpio_mask(struct device *dev, struct device_attribute *attr, char *buf)
1321{
1322 struct adm1026_data *data = adm1026_update_device(dev);
1323 return sprintf(buf,"%ld\n", data->gpio_mask);
1324}
1325static ssize_t set_gpio_mask(struct device *dev, struct device_attribute *attr, const char *buf,
1326 size_t count)
1327{
1328 struct i2c_client *client = to_i2c_client(dev);
1329 struct adm1026_data *data = i2c_get_clientdata(client);
1330 int val = simple_strtol(buf, NULL, 10);
1331 long mask;
1332
1333 down(&data->update_lock);
1334 data->gpio_mask = val & 0x1ffff;
1335 mask = data->gpio_mask;
1336 adm1026_write_value(client, ADM1026_REG_GPIO_MASK_0_7,mask & 0xff);
1337 mask >>= 8;
1338 adm1026_write_value(client, ADM1026_REG_GPIO_MASK_8_15,mask & 0xff);
1339 mask = ((mask >> 1) & 0x80) | (data->alarm_mask >> 24 & 0x7f);
1340 adm1026_write_value(client, ADM1026_REG_MASK1,mask & 0xff);
1341 up(&data->update_lock);
1342 return count;
1343}
1344
1345static DEVICE_ATTR(gpio_mask, S_IRUGO | S_IWUSR, show_gpio_mask, set_gpio_mask);
1346
1347static ssize_t show_pwm_reg(struct device *dev, struct device_attribute *attr, char *buf)
1348{
1349 struct adm1026_data *data = adm1026_update_device(dev);
1350 return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm1.pwm));
1351}
1352static ssize_t set_pwm_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1353 size_t count)
1354{
1355 struct i2c_client *client = to_i2c_client(dev);
1356 struct adm1026_data *data = i2c_get_clientdata(client);
1357
1358 if (data->pwm1.enable == 1) {
1359 int val = simple_strtol(buf, NULL, 10);
1360
1361 down(&data->update_lock);
1362 data->pwm1.pwm = PWM_TO_REG(val);
1363 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1364 up(&data->update_lock);
1365 }
1366 return count;
1367}
1368static ssize_t show_auto_pwm_min(struct device *dev, struct device_attribute *attr, char *buf)
1369{
1370 struct adm1026_data *data = adm1026_update_device(dev);
1371 return sprintf(buf,"%d\n", data->pwm1.auto_pwm_min);
1372}
1373static ssize_t set_auto_pwm_min(struct device *dev, struct device_attribute *attr, const char *buf,
1374 size_t count)
1375{
1376 struct i2c_client *client = to_i2c_client(dev);
1377 struct adm1026_data *data = i2c_get_clientdata(client);
1378 int val = simple_strtol(buf, NULL, 10);
1379
1380 down(&data->update_lock);
1381 data->pwm1.auto_pwm_min = SENSORS_LIMIT(val,0,255);
1382 if (data->pwm1.enable == 2) { /* apply immediately */
1383 data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
1384 PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
1385 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1386 }
1387 up(&data->update_lock);
1388 return count;
1389}
1390static ssize_t show_auto_pwm_max(struct device *dev, struct device_attribute *attr, char *buf)
1391{
1392 return sprintf(buf,"%d\n", ADM1026_PWM_MAX);
1393}
1394static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
1395{
1396 struct adm1026_data *data = adm1026_update_device(dev);
1397 return sprintf(buf,"%d\n", data->pwm1.enable);
1398}
1399static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr, const char *buf,
1400 size_t count)
1401{
1402 struct i2c_client *client = to_i2c_client(dev);
1403 struct adm1026_data *data = i2c_get_clientdata(client);
1404 int val = simple_strtol(buf, NULL, 10);
1405 int old_enable;
1406
1407 if ((val >= 0) && (val < 3)) {
1408 down(&data->update_lock);
1409 old_enable = data->pwm1.enable;
1410 data->pwm1.enable = val;
1411 data->config1 = (data->config1 & ~CFG1_PWM_AFC)
1412 | ((val == 2) ? CFG1_PWM_AFC : 0);
1413 adm1026_write_value(client, ADM1026_REG_CONFIG1,
1414 data->config1);
1415 if (val == 2) { /* apply pwm1_auto_pwm_min to pwm1 */
1416 data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
1417 PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
1418 adm1026_write_value(client, ADM1026_REG_PWM,
1419 data->pwm1.pwm);
1420 } else if (!((old_enable == 1) && (val == 1))) {
1421 /* set pwm to safe value */
1422 data->pwm1.pwm = 255;
1423 adm1026_write_value(client, ADM1026_REG_PWM,
1424 data->pwm1.pwm);
1425 }
1426 up(&data->update_lock);
1427 }
1428 return count;
1429}
1430
1431/* enable PWM fan control */
1432static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1433static DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1434static DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1435static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
1436 set_pwm_enable);
1437static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
1438 set_pwm_enable);
1439static DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
1440 set_pwm_enable);
1441static DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1442 show_auto_pwm_min, set_auto_pwm_min);
1443static DEVICE_ATTR(temp2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1444 show_auto_pwm_min, set_auto_pwm_min);
1445static DEVICE_ATTR(temp3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1446 show_auto_pwm_min, set_auto_pwm_min);
1447
1448static DEVICE_ATTR(temp1_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1449static DEVICE_ATTR(temp2_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1450static DEVICE_ATTR(temp3_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1451
1452int adm1026_detect(struct i2c_adapter *adapter, int address,
1453 int kind)
1454{
1455 int company, verstep;
1456 struct i2c_client *new_client;
1457 struct adm1026_data *data;
1458 int err = 0;
1459 const char *type_name = "";
1460
1461 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1462 /* We need to be able to do byte I/O */
1463 goto exit;
1464 };
1465
1466 /* OK. For now, we presume we have a valid client. We now create the
1467 client structure, even though we cannot fill it completely yet.
1468 But it allows us to access adm1026_{read,write}_value. */
1469
1470 if (!(data = kmalloc(sizeof(struct adm1026_data), GFP_KERNEL))) {
1471 err = -ENOMEM;
1472 goto exit;
1473 }
1474
1475 memset(data, 0, sizeof(struct adm1026_data));
1476
1477 new_client = &data->client;
1478 i2c_set_clientdata(new_client, data);
1479 new_client->addr = address;
1480 new_client->adapter = adapter;
1481 new_client->driver = &adm1026_driver;
1482 new_client->flags = 0;
1483
1484 /* Now, we do the remaining detection. */
1485
1486 company = adm1026_read_value(new_client, ADM1026_REG_COMPANY);
1487 verstep = adm1026_read_value(new_client, ADM1026_REG_VERSTEP);
1488
1489 dev_dbg(&new_client->dev, "Detecting device at %d,0x%02x with"
1490 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1491 i2c_adapter_id(new_client->adapter), new_client->addr,
1492 company, verstep);
1493
1494 /* If auto-detecting, Determine the chip type. */
1495 if (kind <= 0) {
1496 dev_dbg(&new_client->dev, "Autodetecting device at %d,0x%02x "
1497 "...\n", i2c_adapter_id(adapter), address);
1498 if (company == ADM1026_COMPANY_ANALOG_DEV
1499 && verstep == ADM1026_VERSTEP_ADM1026) {
1500 kind = adm1026;
1501 } else if (company == ADM1026_COMPANY_ANALOG_DEV
1502 && (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1503 dev_err(&adapter->dev, ": Unrecognized stepping "
1504 "0x%02x. Defaulting to ADM1026.\n", verstep);
1505 kind = adm1026;
1506 } else if ((verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1507 dev_err(&adapter->dev, ": Found version/stepping "
1508 "0x%02x. Assuming generic ADM1026.\n",
1509 verstep);
1510 kind = any_chip;
1511 } else {
1512 dev_dbg(&new_client->dev, ": Autodetection "
1513 "failed\n");
1514 /* Not an ADM1026 ... */
1515 if (kind == 0) { /* User used force=x,y */
1516 dev_err(&adapter->dev, "Generic ADM1026 not "
1517 "found at %d,0x%02x. Try "
1518 "force_adm1026.\n",
1519 i2c_adapter_id(adapter), address);
1520 }
1521 err = 0;
1522 goto exitfree;
1523 }
1524 }
1525
1526 /* Fill in the chip specific driver values */
1527 switch (kind) {
1528 case any_chip :
1529 type_name = "adm1026";
1530 break;
1531 case adm1026 :
1532 type_name = "adm1026";
1533 break;
1534 default :
1535 dev_err(&adapter->dev, ": Internal error, invalid "
1536 "kind (%d)!", kind);
1537 err = -EFAULT;
1538 goto exitfree;
1539 }
1540 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1541
1542 /* Fill in the remaining client fields */
1543 data->type = kind;
1544 data->valid = 0;
1545 init_MUTEX(&data->update_lock);
1546
1547 /* Tell the I2C layer a new client has arrived */
1548 if ((err = i2c_attach_client(new_client)))
1549 goto exitfree;
1550
1551 /* Set the VRM version */
1552 data->vrm = i2c_which_vrm();
1553
1554 /* Initialize the ADM1026 chip */
1555 adm1026_init_client(new_client);
1556
1557 /* Register sysfs hooks */
1558 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
1559 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
1560 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
1561 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
1562 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
1563 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
1564 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
1565 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
1566 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
1567 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
1568 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
1569 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
1570 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
1571 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
1572 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
1573 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
1574 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
1575 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
1576 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
1577 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
1578 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
1579 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
1580 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
1581 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
1582 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
1583 device_create_file(&new_client->dev, &sensor_dev_attr_in8_max.dev_attr);
1584 device_create_file(&new_client->dev, &sensor_dev_attr_in8_min.dev_attr);
1585 device_create_file(&new_client->dev, &sensor_dev_attr_in9_input.dev_attr);
1586 device_create_file(&new_client->dev, &sensor_dev_attr_in9_max.dev_attr);
1587 device_create_file(&new_client->dev, &sensor_dev_attr_in9_min.dev_attr);
1588 device_create_file(&new_client->dev, &sensor_dev_attr_in10_input.dev_attr);
1589 device_create_file(&new_client->dev, &sensor_dev_attr_in10_max.dev_attr);
1590 device_create_file(&new_client->dev, &sensor_dev_attr_in10_min.dev_attr);
1591 device_create_file(&new_client->dev, &sensor_dev_attr_in11_input.dev_attr);
1592 device_create_file(&new_client->dev, &sensor_dev_attr_in11_max.dev_attr);
1593 device_create_file(&new_client->dev, &sensor_dev_attr_in11_min.dev_attr);
1594 device_create_file(&new_client->dev, &sensor_dev_attr_in12_input.dev_attr);
1595 device_create_file(&new_client->dev, &sensor_dev_attr_in12_max.dev_attr);
1596 device_create_file(&new_client->dev, &sensor_dev_attr_in12_min.dev_attr);
1597 device_create_file(&new_client->dev, &sensor_dev_attr_in13_input.dev_attr);
1598 device_create_file(&new_client->dev, &sensor_dev_attr_in13_max.dev_attr);
1599 device_create_file(&new_client->dev, &sensor_dev_attr_in13_min.dev_attr);
1600 device_create_file(&new_client->dev, &sensor_dev_attr_in14_input.dev_attr);
1601 device_create_file(&new_client->dev, &sensor_dev_attr_in14_max.dev_attr);
1602 device_create_file(&new_client->dev, &sensor_dev_attr_in14_min.dev_attr);
1603 device_create_file(&new_client->dev, &sensor_dev_attr_in15_input.dev_attr);
1604 device_create_file(&new_client->dev, &sensor_dev_attr_in15_max.dev_attr);
1605 device_create_file(&new_client->dev, &sensor_dev_attr_in15_min.dev_attr);
1606 device_create_file(&new_client->dev, &sensor_dev_attr_in16_input.dev_attr);
1607 device_create_file(&new_client->dev, &sensor_dev_attr_in16_max.dev_attr);
1608 device_create_file(&new_client->dev, &sensor_dev_attr_in16_min.dev_attr);
1609 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_input.dev_attr);
1610 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_div.dev_attr);
1611 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_min.dev_attr);
1612 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_input.dev_attr);
1613 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_div.dev_attr);
1614 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_min.dev_attr);
1615 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_input.dev_attr);
1616 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_div.dev_attr);
1617 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_min.dev_attr);
1618 device_create_file(&new_client->dev, &sensor_dev_attr_fan4_input.dev_attr);
1619 device_create_file(&new_client->dev, &sensor_dev_attr_fan4_div.dev_attr);
1620 device_create_file(&new_client->dev, &sensor_dev_attr_fan4_min.dev_attr);
1621 device_create_file(&new_client->dev, &sensor_dev_attr_fan5_input.dev_attr);
1622 device_create_file(&new_client->dev, &sensor_dev_attr_fan5_div.dev_attr);
1623 device_create_file(&new_client->dev, &sensor_dev_attr_fan5_min.dev_attr);
1624 device_create_file(&new_client->dev, &sensor_dev_attr_fan6_input.dev_attr);
1625 device_create_file(&new_client->dev, &sensor_dev_attr_fan6_div.dev_attr);
1626 device_create_file(&new_client->dev, &sensor_dev_attr_fan6_min.dev_attr);
1627 device_create_file(&new_client->dev, &sensor_dev_attr_fan7_input.dev_attr);
1628 device_create_file(&new_client->dev, &sensor_dev_attr_fan7_div.dev_attr);
1629 device_create_file(&new_client->dev, &sensor_dev_attr_fan7_min.dev_attr);
1630 device_create_file(&new_client->dev, &sensor_dev_attr_fan8_input.dev_attr);
1631 device_create_file(&new_client->dev, &sensor_dev_attr_fan8_div.dev_attr);
1632 device_create_file(&new_client->dev, &sensor_dev_attr_fan8_min.dev_attr);
1633 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
1634 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
1635 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
1636 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
1637 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
1638 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
1639 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
1640 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
1641 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
1642 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_offset.dev_attr);
1643 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_offset.dev_attr);
1644 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_offset.dev_attr);
1645 device_create_file(&new_client->dev,
1646 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr);
1647 device_create_file(&new_client->dev,
1648 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr);
1649 device_create_file(&new_client->dev,
1650 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr);
1651 device_create_file(&new_client->dev,
1652 &sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr);
1653 device_create_file(&new_client->dev,
1654 &sensor_dev_attr_temp2_auto_point1_temp_hyst.dev_attr);
1655 device_create_file(&new_client->dev,
1656 &sensor_dev_attr_temp3_auto_point1_temp_hyst.dev_attr);
1657 device_create_file(&new_client->dev,
1658 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr);
1659 device_create_file(&new_client->dev,
1660 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr);
1661 device_create_file(&new_client->dev,
1662 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr);
1663 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_crit.dev_attr);
1664 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_crit.dev_attr);
1665 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_crit.dev_attr);
1666 device_create_file(&new_client->dev, &dev_attr_temp1_crit_enable);
1667 device_create_file(&new_client->dev, &dev_attr_temp2_crit_enable);
1668 device_create_file(&new_client->dev, &dev_attr_temp3_crit_enable);
1669 /* vid deprecated in favour of cpu0_vid, remove after 2005-11-11 */
1670 device_create_file(&new_client->dev, &dev_attr_vid);
1671 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
1672 device_create_file(&new_client->dev, &dev_attr_vrm);
1673 device_create_file(&new_client->dev, &dev_attr_alarms);
1674 device_create_file(&new_client->dev, &dev_attr_alarm_mask);
1675 device_create_file(&new_client->dev, &dev_attr_gpio);
1676 device_create_file(&new_client->dev, &dev_attr_gpio_mask);
1677 device_create_file(&new_client->dev, &dev_attr_pwm1);
1678 device_create_file(&new_client->dev, &dev_attr_pwm2);
1679 device_create_file(&new_client->dev, &dev_attr_pwm3);
1680 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
1681 device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
1682 device_create_file(&new_client->dev, &dev_attr_pwm3_enable);
1683 device_create_file(&new_client->dev, &dev_attr_temp1_auto_point1_pwm);
1684 device_create_file(&new_client->dev, &dev_attr_temp2_auto_point1_pwm);
1685 device_create_file(&new_client->dev, &dev_attr_temp3_auto_point1_pwm);
1686 device_create_file(&new_client->dev, &dev_attr_temp1_auto_point2_pwm);
1687 device_create_file(&new_client->dev, &dev_attr_temp2_auto_point2_pwm);
1688 device_create_file(&new_client->dev, &dev_attr_temp3_auto_point2_pwm);
1689 device_create_file(&new_client->dev, &dev_attr_analog_out);
1690 return 0;
1691
1692 /* Error out and cleanup code */
1693exitfree:
1694 kfree(new_client);
1695exit:
1696 return err;
1697}
1698static int __init sm_adm1026_init(void)
1699{
1700 return i2c_add_driver(&adm1026_driver);
1701}
1702
1703static void __exit sm_adm1026_exit(void)
1704{
1705 i2c_del_driver(&adm1026_driver);
1706}
1707
1708MODULE_LICENSE("GPL");
1709MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1710 "Justin Thiessen <jthiessen@penguincomputing.com>");
1711MODULE_DESCRIPTION("ADM1026 driver");
1712
1713module_init(sm_adm1026_init);
1714module_exit(sm_adm1026_exit);
diff --git a/drivers/hwmon/adm1031.c b/drivers/hwmon/adm1031.c
new file mode 100644
index 000000000000..9168e983ca1d
--- /dev/null
+++ b/drivers/hwmon/adm1031.c
@@ -0,0 +1,977 @@
1/*
2 adm1031.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Based on lm75.c and lm85.c
5 Supports adm1030 / adm1031
6 Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org>
7 Reworked by 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*/
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/jiffies.h>
28#include <linux/i2c.h>
29#include <linux/i2c-sensor.h>
30
31/* Following macros takes channel parameter starting from 0 to 2 */
32#define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr))
33#define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr))
34#define ADM1031_REG_PWM (0x22)
35#define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr))
36
37#define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4*(nr))
38#define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4*(nr))
39#define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4*(nr))
40
41#define ADM1031_REG_TEMP(nr) (0xa + (nr))
42#define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
43
44#define ADM1031_REG_STATUS(nr) (0x2 + (nr))
45
46#define ADM1031_REG_CONF1 0x0
47#define ADM1031_REG_CONF2 0x1
48#define ADM1031_REG_EXT_TEMP 0x6
49
50#define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */
51#define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */
52#define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */
53
54#define ADM1031_CONF2_PWM1_ENABLE 0x01
55#define ADM1031_CONF2_PWM2_ENABLE 0x02
56#define ADM1031_CONF2_TACH1_ENABLE 0x04
57#define ADM1031_CONF2_TACH2_ENABLE 0x08
58#define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
59
60/* Addresses to scan */
61static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
62static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
63
64/* Insmod parameters */
65SENSORS_INSMOD_2(adm1030, adm1031);
66
67typedef u8 auto_chan_table_t[8][2];
68
69/* Each client has this additional data */
70struct adm1031_data {
71 struct i2c_client client;
72 struct semaphore update_lock;
73 int chip_type;
74 char valid; /* !=0 if following fields are valid */
75 unsigned long last_updated; /* In jiffies */
76 /* The chan_select_table contains the possible configurations for
77 * auto fan control.
78 */
79 auto_chan_table_t *chan_select_table;
80 u16 alarm;
81 u8 conf1;
82 u8 conf2;
83 u8 fan[2];
84 u8 fan_div[2];
85 u8 fan_min[2];
86 u8 pwm[2];
87 u8 old_pwm[2];
88 s8 temp[3];
89 u8 ext_temp[3];
90 u8 auto_temp[3];
91 u8 auto_temp_min[3];
92 u8 auto_temp_off[3];
93 u8 auto_temp_max[3];
94 s8 temp_min[3];
95 s8 temp_max[3];
96 s8 temp_crit[3];
97};
98
99static int adm1031_attach_adapter(struct i2c_adapter *adapter);
100static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind);
101static void adm1031_init_client(struct i2c_client *client);
102static int adm1031_detach_client(struct i2c_client *client);
103static struct adm1031_data *adm1031_update_device(struct device *dev);
104
105/* This is the driver that will be inserted */
106static struct i2c_driver adm1031_driver = {
107 .owner = THIS_MODULE,
108 .name = "adm1031",
109 .flags = I2C_DF_NOTIFY,
110 .attach_adapter = adm1031_attach_adapter,
111 .detach_client = adm1031_detach_client,
112};
113
114static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
115{
116 return i2c_smbus_read_byte_data(client, reg);
117}
118
119static inline int
120adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
121{
122 return i2c_smbus_write_byte_data(client, reg, value);
123}
124
125
126#define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
127 ((val + 500) / 1000)))
128
129#define TEMP_FROM_REG(val) ((val) * 1000)
130
131#define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
132
133#define FAN_FROM_REG(reg, div) ((reg) ? (11250 * 60) / ((reg) * (div)) : 0)
134
135static int FAN_TO_REG(int reg, int div)
136{
137 int tmp;
138 tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div);
139 return tmp > 255 ? 255 : tmp;
140}
141
142#define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
143
144#define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
145#define PWM_FROM_REG(val) ((val) << 4)
146
147#define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
148#define FAN_CHAN_TO_REG(val, reg) \
149 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
150
151#define AUTO_TEMP_MIN_TO_REG(val, reg) \
152 ((((val)/500) & 0xf8)|((reg) & 0x7))
153#define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1<< ((reg)&0x7)))
154#define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
155
156#define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
157
158#define AUTO_TEMP_OFF_FROM_REG(reg) \
159 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
160
161#define AUTO_TEMP_MAX_FROM_REG(reg) \
162 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
163 AUTO_TEMP_MIN_FROM_REG(reg))
164
165static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
166{
167 int ret;
168 int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
169
170 range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
171 ret = ((reg & 0xf8) |
172 (range < 10000 ? 0 :
173 range < 20000 ? 1 :
174 range < 40000 ? 2 : range < 80000 ? 3 : 4));
175 return ret;
176}
177
178/* FAN auto control */
179#define GET_FAN_AUTO_BITFIELD(data, idx) \
180 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2]
181
182/* The tables below contains the possible values for the auto fan
183 * control bitfields. the index in the table is the register value.
184 * MSb is the auto fan control enable bit, so the four first entries
185 * in the table disables auto fan control when both bitfields are zero.
186 */
187static auto_chan_table_t auto_channel_select_table_adm1031 = {
188 {0, 0}, {0, 0}, {0, 0}, {0, 0},
189 {2 /*0b010 */ , 4 /*0b100 */ },
190 {2 /*0b010 */ , 2 /*0b010 */ },
191 {4 /*0b100 */ , 4 /*0b100 */ },
192 {7 /*0b111 */ , 7 /*0b111 */ },
193};
194
195static auto_chan_table_t auto_channel_select_table_adm1030 = {
196 {0, 0}, {0, 0}, {0, 0}, {0, 0},
197 {2 /*0b10 */ , 0},
198 {0xff /*invalid */ , 0},
199 {0xff /*invalid */ , 0},
200 {3 /*0b11 */ , 0},
201};
202
203/* That function checks if a bitfield is valid and returns the other bitfield
204 * nearest match if no exact match where found.
205 */
206static int
207get_fan_auto_nearest(struct adm1031_data *data,
208 int chan, u8 val, u8 reg, u8 * new_reg)
209{
210 int i;
211 int first_match = -1, exact_match = -1;
212 u8 other_reg_val =
213 (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
214
215 if (val == 0) {
216 *new_reg = 0;
217 return 0;
218 }
219
220 for (i = 0; i < 8; i++) {
221 if ((val == (*data->chan_select_table)[i][chan]) &&
222 ((*data->chan_select_table)[i][chan ? 0 : 1] ==
223 other_reg_val)) {
224 /* We found an exact match */
225 exact_match = i;
226 break;
227 } else if (val == (*data->chan_select_table)[i][chan] &&
228 first_match == -1) {
229 /* Save the first match in case of an exact match has not been
230 * found
231 */
232 first_match = i;
233 }
234 }
235
236 if (exact_match >= 0) {
237 *new_reg = exact_match;
238 } else if (first_match >= 0) {
239 *new_reg = first_match;
240 } else {
241 return -EINVAL;
242 }
243 return 0;
244}
245
246static ssize_t show_fan_auto_channel(struct device *dev, char *buf, int nr)
247{
248 struct adm1031_data *data = adm1031_update_device(dev);
249 return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
250}
251
252static ssize_t
253set_fan_auto_channel(struct device *dev, const char *buf, size_t count, int nr)
254{
255 struct i2c_client *client = to_i2c_client(dev);
256 struct adm1031_data *data = i2c_get_clientdata(client);
257 int val = simple_strtol(buf, NULL, 10);
258 u8 reg;
259 int ret;
260 u8 old_fan_mode;
261
262 old_fan_mode = data->conf1;
263
264 down(&data->update_lock);
265
266 if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg))) {
267 up(&data->update_lock);
268 return ret;
269 }
270 if (((data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1)) & ADM1031_CONF1_AUTO_MODE) ^
271 (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
272 if (data->conf1 & ADM1031_CONF1_AUTO_MODE){
273 /* Switch to Auto Fan Mode
274 * Save PWM registers
275 * Set PWM registers to 33% Both */
276 data->old_pwm[0] = data->pwm[0];
277 data->old_pwm[1] = data->pwm[1];
278 adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
279 } else {
280 /* Switch to Manual Mode */
281 data->pwm[0] = data->old_pwm[0];
282 data->pwm[1] = data->old_pwm[1];
283 /* Restore PWM registers */
284 adm1031_write_value(client, ADM1031_REG_PWM,
285 data->pwm[0] | (data->pwm[1] << 4));
286 }
287 }
288 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
289 adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
290 up(&data->update_lock);
291 return count;
292}
293
294#define fan_auto_channel_offset(offset) \
295static ssize_t show_fan_auto_channel_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
296{ \
297 return show_fan_auto_channel(dev, buf, offset - 1); \
298} \
299static ssize_t set_fan_auto_channel_##offset (struct device *dev, struct device_attribute *attr, \
300 const char *buf, size_t count) \
301{ \
302 return set_fan_auto_channel(dev, buf, count, offset - 1); \
303} \
304static DEVICE_ATTR(auto_fan##offset##_channel, S_IRUGO | S_IWUSR, \
305 show_fan_auto_channel_##offset, \
306 set_fan_auto_channel_##offset)
307
308fan_auto_channel_offset(1);
309fan_auto_channel_offset(2);
310
311/* Auto Temps */
312static ssize_t show_auto_temp_off(struct device *dev, char *buf, int nr)
313{
314 struct adm1031_data *data = adm1031_update_device(dev);
315 return sprintf(buf, "%d\n",
316 AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
317}
318static ssize_t show_auto_temp_min(struct device *dev, char *buf, int nr)
319{
320 struct adm1031_data *data = adm1031_update_device(dev);
321 return sprintf(buf, "%d\n",
322 AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
323}
324static ssize_t
325set_auto_temp_min(struct device *dev, const char *buf, size_t count, int nr)
326{
327 struct i2c_client *client = to_i2c_client(dev);
328 struct adm1031_data *data = i2c_get_clientdata(client);
329 int val = simple_strtol(buf, NULL, 10);
330
331 down(&data->update_lock);
332 data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
333 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
334 data->auto_temp[nr]);
335 up(&data->update_lock);
336 return count;
337}
338static ssize_t show_auto_temp_max(struct device *dev, char *buf, int nr)
339{
340 struct adm1031_data *data = adm1031_update_device(dev);
341 return sprintf(buf, "%d\n",
342 AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
343}
344static ssize_t
345set_auto_temp_max(struct device *dev, const char *buf, size_t count, int nr)
346{
347 struct i2c_client *client = to_i2c_client(dev);
348 struct adm1031_data *data = i2c_get_clientdata(client);
349 int val = simple_strtol(buf, NULL, 10);
350
351 down(&data->update_lock);
352 data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr], data->pwm[nr]);
353 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
354 data->temp_max[nr]);
355 up(&data->update_lock);
356 return count;
357}
358
359#define auto_temp_reg(offset) \
360static ssize_t show_auto_temp_##offset##_off (struct device *dev, struct device_attribute *attr, char *buf) \
361{ \
362 return show_auto_temp_off(dev, buf, offset - 1); \
363} \
364static ssize_t show_auto_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
365{ \
366 return show_auto_temp_min(dev, buf, offset - 1); \
367} \
368static ssize_t show_auto_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
369{ \
370 return show_auto_temp_max(dev, buf, offset - 1); \
371} \
372static ssize_t set_auto_temp_##offset##_min (struct device *dev, struct device_attribute *attr, \
373 const char *buf, size_t count) \
374{ \
375 return set_auto_temp_min(dev, buf, count, offset - 1); \
376} \
377static ssize_t set_auto_temp_##offset##_max (struct device *dev, struct device_attribute *attr, \
378 const char *buf, size_t count) \
379{ \
380 return set_auto_temp_max(dev, buf, count, offset - 1); \
381} \
382static DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
383 show_auto_temp_##offset##_off, NULL); \
384static DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
385 show_auto_temp_##offset##_min, set_auto_temp_##offset##_min);\
386static DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
387 show_auto_temp_##offset##_max, set_auto_temp_##offset##_max)
388
389auto_temp_reg(1);
390auto_temp_reg(2);
391auto_temp_reg(3);
392
393/* pwm */
394static ssize_t show_pwm(struct device *dev, char *buf, int nr)
395{
396 struct adm1031_data *data = adm1031_update_device(dev);
397 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
398}
399static ssize_t
400set_pwm(struct device *dev, const char *buf, size_t count, int nr)
401{
402 struct i2c_client *client = to_i2c_client(dev);
403 struct adm1031_data *data = i2c_get_clientdata(client);
404 int val = simple_strtol(buf, NULL, 10);
405 int reg;
406
407 down(&data->update_lock);
408 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
409 (((val>>4) & 0xf) != 5)) {
410 /* In automatic mode, the only PWM accepted is 33% */
411 up(&data->update_lock);
412 return -EINVAL;
413 }
414 data->pwm[nr] = PWM_TO_REG(val);
415 reg = adm1031_read_value(client, ADM1031_REG_PWM);
416 adm1031_write_value(client, ADM1031_REG_PWM,
417 nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
418 : (data->pwm[nr] & 0xf) | (reg & 0xf0));
419 up(&data->update_lock);
420 return count;
421}
422
423#define pwm_reg(offset) \
424static ssize_t show_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
425{ \
426 return show_pwm(dev, buf, offset - 1); \
427} \
428static ssize_t set_pwm_##offset (struct device *dev, struct device_attribute *attr, \
429 const char *buf, size_t count) \
430{ \
431 return set_pwm(dev, buf, count, offset - 1); \
432} \
433static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
434 show_pwm_##offset, set_pwm_##offset)
435
436pwm_reg(1);
437pwm_reg(2);
438
439/* Fans */
440
441/*
442 * That function checks the cases where the fan reading is not
443 * relevant. It is used to provide 0 as fan reading when the fan is
444 * not supposed to run
445 */
446static int trust_fan_readings(struct adm1031_data *data, int chan)
447{
448 int res = 0;
449
450 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
451 switch (data->conf1 & 0x60) {
452 case 0x00: /* remote temp1 controls fan1 remote temp2 controls fan2 */
453 res = data->temp[chan+1] >=
454 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
455 break;
456 case 0x20: /* remote temp1 controls both fans */
457 res =
458 data->temp[1] >=
459 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
460 break;
461 case 0x40: /* remote temp2 controls both fans */
462 res =
463 data->temp[2] >=
464 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
465 break;
466 case 0x60: /* max controls both fans */
467 res =
468 data->temp[0] >=
469 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
470 || data->temp[1] >=
471 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
472 || (data->chip_type == adm1031
473 && data->temp[2] >=
474 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
475 break;
476 }
477 } else {
478 res = data->pwm[chan] > 0;
479 }
480 return res;
481}
482
483
484static ssize_t show_fan(struct device *dev, char *buf, int nr)
485{
486 struct adm1031_data *data = adm1031_update_device(dev);
487 int value;
488
489 value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
490 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
491 return sprintf(buf, "%d\n", value);
492}
493
494static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
495{
496 struct adm1031_data *data = adm1031_update_device(dev);
497 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
498}
499static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
500{
501 struct adm1031_data *data = adm1031_update_device(dev);
502 return sprintf(buf, "%d\n",
503 FAN_FROM_REG(data->fan_min[nr],
504 FAN_DIV_FROM_REG(data->fan_div[nr])));
505}
506static ssize_t
507set_fan_min(struct device *dev, const char *buf, size_t count, int nr)
508{
509 struct i2c_client *client = to_i2c_client(dev);
510 struct adm1031_data *data = i2c_get_clientdata(client);
511 int val = simple_strtol(buf, NULL, 10);
512
513 down(&data->update_lock);
514 if (val) {
515 data->fan_min[nr] =
516 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
517 } else {
518 data->fan_min[nr] = 0xff;
519 }
520 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
521 up(&data->update_lock);
522 return count;
523}
524static ssize_t
525set_fan_div(struct device *dev, const char *buf, size_t count, int nr)
526{
527 struct i2c_client *client = to_i2c_client(dev);
528 struct adm1031_data *data = i2c_get_clientdata(client);
529 int val = simple_strtol(buf, NULL, 10);
530 u8 tmp;
531 int old_div;
532 int new_min;
533
534 tmp = val == 8 ? 0xc0 :
535 val == 4 ? 0x80 :
536 val == 2 ? 0x40 :
537 val == 1 ? 0x00 :
538 0xff;
539 if (tmp == 0xff)
540 return -EINVAL;
541
542 down(&data->update_lock);
543 old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
544 data->fan_div[nr] = (tmp & 0xC0) | (0x3f & data->fan_div[nr]);
545 new_min = data->fan_min[nr] * old_div /
546 FAN_DIV_FROM_REG(data->fan_div[nr]);
547 data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
548 data->fan[nr] = data->fan[nr] * old_div /
549 FAN_DIV_FROM_REG(data->fan_div[nr]);
550
551 adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
552 data->fan_div[nr]);
553 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
554 data->fan_min[nr]);
555 up(&data->update_lock);
556 return count;
557}
558
559#define fan_offset(offset) \
560static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
561{ \
562 return show_fan(dev, buf, offset - 1); \
563} \
564static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
565{ \
566 return show_fan_min(dev, buf, offset - 1); \
567} \
568static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
569{ \
570 return show_fan_div(dev, buf, offset - 1); \
571} \
572static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
573 const char *buf, size_t count) \
574{ \
575 return set_fan_min(dev, buf, count, offset - 1); \
576} \
577static ssize_t set_fan_##offset##_div (struct device *dev, struct device_attribute *attr, \
578 const char *buf, size_t count) \
579{ \
580 return set_fan_div(dev, buf, count, offset - 1); \
581} \
582static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, \
583 NULL); \
584static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
585 show_fan_##offset##_min, set_fan_##offset##_min); \
586static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
587 show_fan_##offset##_div, set_fan_##offset##_div); \
588static DEVICE_ATTR(auto_fan##offset##_min_pwm, S_IRUGO | S_IWUSR, \
589 show_pwm_##offset, set_pwm_##offset)
590
591fan_offset(1);
592fan_offset(2);
593
594
595/* Temps */
596static ssize_t show_temp(struct device *dev, char *buf, int nr)
597{
598 struct adm1031_data *data = adm1031_update_device(dev);
599 int ext;
600 ext = nr == 0 ?
601 ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
602 (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
603 return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
604}
605static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
606{
607 struct adm1031_data *data = adm1031_update_device(dev);
608 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
609}
610static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
611{
612 struct adm1031_data *data = adm1031_update_device(dev);
613 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
614}
615static ssize_t show_temp_crit(struct device *dev, char *buf, int nr)
616{
617 struct adm1031_data *data = adm1031_update_device(dev);
618 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
619}
620static ssize_t
621set_temp_min(struct device *dev, const char *buf, size_t count, int nr)
622{
623 struct i2c_client *client = to_i2c_client(dev);
624 struct adm1031_data *data = i2c_get_clientdata(client);
625 int val;
626
627 val = simple_strtol(buf, NULL, 10);
628 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
629 down(&data->update_lock);
630 data->temp_min[nr] = TEMP_TO_REG(val);
631 adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
632 data->temp_min[nr]);
633 up(&data->update_lock);
634 return count;
635}
636static ssize_t
637set_temp_max(struct device *dev, const char *buf, size_t count, int nr)
638{
639 struct i2c_client *client = to_i2c_client(dev);
640 struct adm1031_data *data = i2c_get_clientdata(client);
641 int val;
642
643 val = simple_strtol(buf, NULL, 10);
644 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
645 down(&data->update_lock);
646 data->temp_max[nr] = TEMP_TO_REG(val);
647 adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
648 data->temp_max[nr]);
649 up(&data->update_lock);
650 return count;
651}
652static ssize_t
653set_temp_crit(struct device *dev, const char *buf, size_t count, int nr)
654{
655 struct i2c_client *client = to_i2c_client(dev);
656 struct adm1031_data *data = i2c_get_clientdata(client);
657 int val;
658
659 val = simple_strtol(buf, NULL, 10);
660 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
661 down(&data->update_lock);
662 data->temp_crit[nr] = TEMP_TO_REG(val);
663 adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
664 data->temp_crit[nr]);
665 up(&data->update_lock);
666 return count;
667}
668
669#define temp_reg(offset) \
670static ssize_t show_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
671{ \
672 return show_temp(dev, buf, offset - 1); \
673} \
674static ssize_t show_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
675{ \
676 return show_temp_min(dev, buf, offset - 1); \
677} \
678static ssize_t show_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
679{ \
680 return show_temp_max(dev, buf, offset - 1); \
681} \
682static ssize_t show_temp_##offset##_crit (struct device *dev, struct device_attribute *attr, char *buf) \
683{ \
684 return show_temp_crit(dev, buf, offset - 1); \
685} \
686static ssize_t set_temp_##offset##_min (struct device *dev, struct device_attribute *attr, \
687 const char *buf, size_t count) \
688{ \
689 return set_temp_min(dev, buf, count, offset - 1); \
690} \
691static ssize_t set_temp_##offset##_max (struct device *dev, struct device_attribute *attr, \
692 const char *buf, size_t count) \
693{ \
694 return set_temp_max(dev, buf, count, offset - 1); \
695} \
696static ssize_t set_temp_##offset##_crit (struct device *dev, struct device_attribute *attr, \
697 const char *buf, size_t count) \
698{ \
699 return set_temp_crit(dev, buf, count, offset - 1); \
700} \
701static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, \
702 NULL); \
703static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
704 show_temp_##offset##_min, set_temp_##offset##_min); \
705static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
706 show_temp_##offset##_max, set_temp_##offset##_max); \
707static DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
708 show_temp_##offset##_crit, set_temp_##offset##_crit)
709
710temp_reg(1);
711temp_reg(2);
712temp_reg(3);
713
714/* Alarms */
715static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
716{
717 struct adm1031_data *data = adm1031_update_device(dev);
718 return sprintf(buf, "%d\n", data->alarm);
719}
720
721static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
722
723
724static int adm1031_attach_adapter(struct i2c_adapter *adapter)
725{
726 if (!(adapter->class & I2C_CLASS_HWMON))
727 return 0;
728 return i2c_detect(adapter, &addr_data, adm1031_detect);
729}
730
731/* This function is called by i2c_detect */
732static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind)
733{
734 struct i2c_client *new_client;
735 struct adm1031_data *data;
736 int err = 0;
737 const char *name = "";
738
739 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
740 goto exit;
741
742 if (!(data = kmalloc(sizeof(struct adm1031_data), GFP_KERNEL))) {
743 err = -ENOMEM;
744 goto exit;
745 }
746 memset(data, 0, sizeof(struct adm1031_data));
747
748 new_client = &data->client;
749 i2c_set_clientdata(new_client, data);
750 new_client->addr = address;
751 new_client->adapter = adapter;
752 new_client->driver = &adm1031_driver;
753 new_client->flags = 0;
754
755 if (kind < 0) {
756 int id, co;
757 id = i2c_smbus_read_byte_data(new_client, 0x3d);
758 co = i2c_smbus_read_byte_data(new_client, 0x3e);
759
760 if (!((id == 0x31 || id == 0x30) && co == 0x41))
761 goto exit_free;
762 kind = (id == 0x30) ? adm1030 : adm1031;
763 }
764
765 if (kind <= 0)
766 kind = adm1031;
767
768 /* Given the detected chip type, set the chip name and the
769 * auto fan control helper table. */
770 if (kind == adm1030) {
771 name = "adm1030";
772 data->chan_select_table = &auto_channel_select_table_adm1030;
773 } else if (kind == adm1031) {
774 name = "adm1031";
775 data->chan_select_table = &auto_channel_select_table_adm1031;
776 }
777 data->chip_type = kind;
778
779 strlcpy(new_client->name, name, I2C_NAME_SIZE);
780 data->valid = 0;
781 init_MUTEX(&data->update_lock);
782
783 /* Tell the I2C layer a new client has arrived */
784 if ((err = i2c_attach_client(new_client)))
785 goto exit_free;
786
787 /* Initialize the ADM1031 chip */
788 adm1031_init_client(new_client);
789
790 /* Register sysfs hooks */
791 device_create_file(&new_client->dev, &dev_attr_fan1_input);
792 device_create_file(&new_client->dev, &dev_attr_fan1_div);
793 device_create_file(&new_client->dev, &dev_attr_fan1_min);
794 device_create_file(&new_client->dev, &dev_attr_pwm1);
795 device_create_file(&new_client->dev, &dev_attr_auto_fan1_channel);
796 device_create_file(&new_client->dev, &dev_attr_temp1_input);
797 device_create_file(&new_client->dev, &dev_attr_temp1_min);
798 device_create_file(&new_client->dev, &dev_attr_temp1_max);
799 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
800 device_create_file(&new_client->dev, &dev_attr_temp2_input);
801 device_create_file(&new_client->dev, &dev_attr_temp2_min);
802 device_create_file(&new_client->dev, &dev_attr_temp2_max);
803 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
804
805 device_create_file(&new_client->dev, &dev_attr_auto_temp1_off);
806 device_create_file(&new_client->dev, &dev_attr_auto_temp1_min);
807 device_create_file(&new_client->dev, &dev_attr_auto_temp1_max);
808
809 device_create_file(&new_client->dev, &dev_attr_auto_temp2_off);
810 device_create_file(&new_client->dev, &dev_attr_auto_temp2_min);
811 device_create_file(&new_client->dev, &dev_attr_auto_temp2_max);
812
813 device_create_file(&new_client->dev, &dev_attr_auto_fan1_min_pwm);
814
815 device_create_file(&new_client->dev, &dev_attr_alarms);
816
817 if (kind == adm1031) {
818 device_create_file(&new_client->dev, &dev_attr_fan2_input);
819 device_create_file(&new_client->dev, &dev_attr_fan2_div);
820 device_create_file(&new_client->dev, &dev_attr_fan2_min);
821 device_create_file(&new_client->dev, &dev_attr_pwm2);
822 device_create_file(&new_client->dev,
823 &dev_attr_auto_fan2_channel);
824 device_create_file(&new_client->dev, &dev_attr_temp3_input);
825 device_create_file(&new_client->dev, &dev_attr_temp3_min);
826 device_create_file(&new_client->dev, &dev_attr_temp3_max);
827 device_create_file(&new_client->dev, &dev_attr_temp3_crit);
828 device_create_file(&new_client->dev, &dev_attr_auto_temp3_off);
829 device_create_file(&new_client->dev, &dev_attr_auto_temp3_min);
830 device_create_file(&new_client->dev, &dev_attr_auto_temp3_max);
831 device_create_file(&new_client->dev, &dev_attr_auto_fan2_min_pwm);
832 }
833
834 return 0;
835
836exit_free:
837 kfree(new_client);
838exit:
839 return err;
840}
841
842static int adm1031_detach_client(struct i2c_client *client)
843{
844 int ret;
845 if ((ret = i2c_detach_client(client)) != 0) {
846 return ret;
847 }
848 kfree(client);
849 return 0;
850}
851
852static void adm1031_init_client(struct i2c_client *client)
853{
854 unsigned int read_val;
855 unsigned int mask;
856 struct adm1031_data *data = i2c_get_clientdata(client);
857
858 mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
859 if (data->chip_type == adm1031) {
860 mask |= (ADM1031_CONF2_PWM2_ENABLE |
861 ADM1031_CONF2_TACH2_ENABLE);
862 }
863 /* Initialize the ADM1031 chip (enables fan speed reading ) */
864 read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
865 if ((read_val | mask) != read_val) {
866 adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
867 }
868
869 read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
870 if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
871 adm1031_write_value(client, ADM1031_REG_CONF1, read_val |
872 ADM1031_CONF1_MONITOR_ENABLE);
873 }
874
875}
876
877static struct adm1031_data *adm1031_update_device(struct device *dev)
878{
879 struct i2c_client *client = to_i2c_client(dev);
880 struct adm1031_data *data = i2c_get_clientdata(client);
881 int chan;
882
883 down(&data->update_lock);
884
885 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
886 || !data->valid) {
887
888 dev_dbg(&client->dev, "Starting adm1031 update\n");
889 for (chan = 0;
890 chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
891 u8 oldh, newh;
892
893 oldh =
894 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
895 data->ext_temp[chan] =
896 adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
897 newh =
898 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
899 if (newh != oldh) {
900 data->ext_temp[chan] =
901 adm1031_read_value(client,
902 ADM1031_REG_EXT_TEMP);
903#ifdef DEBUG
904 oldh =
905 adm1031_read_value(client,
906 ADM1031_REG_TEMP(chan));
907
908 /* oldh is actually newer */
909 if (newh != oldh)
910 dev_warn(&client->dev,
911 "Remote temperature may be "
912 "wrong.\n");
913#endif
914 }
915 data->temp[chan] = newh;
916
917 data->temp_min[chan] =
918 adm1031_read_value(client,
919 ADM1031_REG_TEMP_MIN(chan));
920 data->temp_max[chan] =
921 adm1031_read_value(client,
922 ADM1031_REG_TEMP_MAX(chan));
923 data->temp_crit[chan] =
924 adm1031_read_value(client,
925 ADM1031_REG_TEMP_CRIT(chan));
926 data->auto_temp[chan] =
927 adm1031_read_value(client,
928 ADM1031_REG_AUTO_TEMP(chan));
929
930 }
931
932 data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
933 data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);
934
935 data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
936 | (adm1031_read_value(client, ADM1031_REG_STATUS(1))
937 << 8);
938 if (data->chip_type == adm1030) {
939 data->alarm &= 0xc0ff;
940 }
941
942 for (chan=0; chan<(data->chip_type == adm1030 ? 1 : 2); chan++) {
943 data->fan_div[chan] =
944 adm1031_read_value(client, ADM1031_REG_FAN_DIV(chan));
945 data->fan_min[chan] =
946 adm1031_read_value(client, ADM1031_REG_FAN_MIN(chan));
947 data->fan[chan] =
948 adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan));
949 data->pwm[chan] =
950 0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >>
951 (4*chan));
952 }
953 data->last_updated = jiffies;
954 data->valid = 1;
955 }
956
957 up(&data->update_lock);
958
959 return data;
960}
961
962static int __init sensors_adm1031_init(void)
963{
964 return i2c_add_driver(&adm1031_driver);
965}
966
967static void __exit sensors_adm1031_exit(void)
968{
969 i2c_del_driver(&adm1031_driver);
970}
971
972MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
973MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
974MODULE_LICENSE("GPL");
975
976module_init(sensors_adm1031_init);
977module_exit(sensors_adm1031_exit);
diff --git a/drivers/hwmon/adm9240.c b/drivers/hwmon/adm9240.c
new file mode 100644
index 000000000000..5c68e9c311aa
--- /dev/null
+++ b/drivers/hwmon/adm9240.c
@@ -0,0 +1,791 @@
1/*
2 * adm9240.c Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 *
5 * Copyright (C) 1999 Frodo Looijaard <frodol@dds.nl>
6 * Philip Edelbrock <phil@netroedge.com>
7 * Copyright (C) 2003 Michiel Rook <michiel@grendelproject.nl>
8 * Copyright (C) 2005 Grant Coady <gcoady@gmail.com> with valuable
9 * guidance from Jean Delvare
10 *
11 * Driver supports Analog Devices ADM9240
12 * Dallas Semiconductor DS1780
13 * National Semiconductor LM81
14 *
15 * ADM9240 is the reference, DS1780 and LM81 are register compatibles
16 *
17 * Voltage Six inputs are scaled by chip, VID also reported
18 * Temperature Chip temperature to 0.5'C, maximum and max_hysteris
19 * Fans 2 fans, low speed alarm, automatic fan clock divider
20 * Alarms 16-bit map of active alarms
21 * Analog Out 0..1250 mV output
22 *
23 * Chassis Intrusion: clear CI latch with 'echo 1 > chassis_clear'
24 *
25 * Test hardware: Intel SE440BX-2 desktop motherboard --Grant
26 *
27 * LM81 extended temp reading not implemented
28 *
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 */
43
44#include <linux/init.h>
45#include <linux/module.h>
46#include <linux/slab.h>
47#include <linux/i2c.h>
48#include <linux/i2c-sensor.h>
49#include <linux/i2c-vid.h>
50
51/* Addresses to scan */
52static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
53 I2C_CLIENT_END };
54
55static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
56
57/* Insmod parameters */
58SENSORS_INSMOD_3(adm9240, ds1780, lm81);
59
60/* ADM9240 registers */
61#define ADM9240_REG_MAN_ID 0x3e
62#define ADM9240_REG_DIE_REV 0x3f
63#define ADM9240_REG_CONFIG 0x40
64
65#define ADM9240_REG_IN(nr) (0x20 + (nr)) /* 0..5 */
66#define ADM9240_REG_IN_MAX(nr) (0x2b + (nr) * 2)
67#define ADM9240_REG_IN_MIN(nr) (0x2c + (nr) * 2)
68#define ADM9240_REG_FAN(nr) (0x28 + (nr)) /* 0..1 */
69#define ADM9240_REG_FAN_MIN(nr) (0x3b + (nr))
70#define ADM9240_REG_INT(nr) (0x41 + (nr))
71#define ADM9240_REG_INT_MASK(nr) (0x43 + (nr))
72#define ADM9240_REG_TEMP 0x27
73#define ADM9240_REG_TEMP_HIGH 0x39
74#define ADM9240_REG_TEMP_HYST 0x3a
75#define ADM9240_REG_ANALOG_OUT 0x19
76#define ADM9240_REG_CHASSIS_CLEAR 0x46
77#define ADM9240_REG_VID_FAN_DIV 0x47
78#define ADM9240_REG_I2C_ADDR 0x48
79#define ADM9240_REG_VID4 0x49
80#define ADM9240_REG_TEMP_CONF 0x4b
81
82/* generalised scaling with integer rounding */
83static inline int SCALE(long val, int mul, int div)
84{
85 if (val < 0)
86 return (val * mul - div / 2) / div;
87 else
88 return (val * mul + div / 2) / div;
89}
90
91/* adm9240 internally scales voltage measurements */
92static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 };
93
94static inline unsigned int IN_FROM_REG(u8 reg, int n)
95{
96 return SCALE(reg, nom_mv[n], 192);
97}
98
99static inline u8 IN_TO_REG(unsigned long val, int n)
100{
101 return SENSORS_LIMIT(SCALE(val, 192, nom_mv[n]), 0, 255);
102}
103
104/* temperature range: -40..125, 127 disables temperature alarm */
105static inline s8 TEMP_TO_REG(long val)
106{
107 return SENSORS_LIMIT(SCALE(val, 1, 1000), -40, 127);
108}
109
110/* two fans, each with low fan speed limit */
111static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
112{
113 if (!reg) /* error */
114 return -1;
115
116 if (reg == 255)
117 return 0;
118
119 return SCALE(1350000, 1, reg * div);
120}
121
122/* analog out 0..1250mV */
123static inline u8 AOUT_TO_REG(unsigned long val)
124{
125 return SENSORS_LIMIT(SCALE(val, 255, 1250), 0, 255);
126}
127
128static inline unsigned int AOUT_FROM_REG(u8 reg)
129{
130 return SCALE(reg, 1250, 255);
131}
132
133static int adm9240_attach_adapter(struct i2c_adapter *adapter);
134static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind);
135static void adm9240_init_client(struct i2c_client *client);
136static int adm9240_detach_client(struct i2c_client *client);
137static struct adm9240_data *adm9240_update_device(struct device *dev);
138
139/* driver data */
140static struct i2c_driver adm9240_driver = {
141 .owner = THIS_MODULE,
142 .name = "adm9240",
143 .id = I2C_DRIVERID_ADM9240,
144 .flags = I2C_DF_NOTIFY,
145 .attach_adapter = adm9240_attach_adapter,
146 .detach_client = adm9240_detach_client,
147};
148
149/* per client data */
150struct adm9240_data {
151 enum chips type;
152 struct i2c_client client;
153 struct semaphore update_lock;
154 char valid;
155 unsigned long last_updated_measure;
156 unsigned long last_updated_config;
157
158 u8 in[6]; /* ro in0_input */
159 u8 in_max[6]; /* rw in0_max */
160 u8 in_min[6]; /* rw in0_min */
161 u8 fan[2]; /* ro fan1_input */
162 u8 fan_min[2]; /* rw fan1_min */
163 u8 fan_div[2]; /* rw fan1_div, read-only accessor */
164 s16 temp; /* ro temp1_input, 9-bit sign-extended */
165 s8 temp_high; /* rw temp1_max */
166 s8 temp_hyst; /* rw temp1_max_hyst */
167 u16 alarms; /* ro alarms */
168 u8 aout; /* rw aout_output */
169 u8 vid; /* ro vid */
170 u8 vrm; /* -- vrm set on startup, no accessor */
171};
172
173/* i2c byte read/write interface */
174static int adm9240_read_value(struct i2c_client *client, u8 reg)
175{
176 return i2c_smbus_read_byte_data(client, reg);
177}
178
179static int adm9240_write_value(struct i2c_client *client, u8 reg, u8 value)
180{
181 return i2c_smbus_write_byte_data(client, reg, value);
182}
183
184/*** sysfs accessors ***/
185
186/* temperature */
187#define show_temp(value, scale) \
188static ssize_t show_##value(struct device *dev, \
189 struct device_attribute *attr, \
190 char *buf) \
191{ \
192 struct adm9240_data *data = adm9240_update_device(dev); \
193 return sprintf(buf, "%d\n", data->value * scale); \
194}
195show_temp(temp_high, 1000);
196show_temp(temp_hyst, 1000);
197show_temp(temp, 500); /* 0.5'C per bit */
198
199#define set_temp(value, reg) \
200static ssize_t set_##value(struct device *dev, \
201 struct device_attribute *attr, \
202 const char *buf, size_t count) \
203{ \
204 struct i2c_client *client = to_i2c_client(dev); \
205 struct adm9240_data *data = adm9240_update_device(dev); \
206 long temp = simple_strtoul(buf, NULL, 10); \
207 \
208 down(&data->update_lock); \
209 data->value = TEMP_TO_REG(temp); \
210 adm9240_write_value(client, reg, data->value); \
211 up(&data->update_lock); \
212 return count; \
213}
214
215set_temp(temp_high, ADM9240_REG_TEMP_HIGH);
216set_temp(temp_hyst, ADM9240_REG_TEMP_HYST);
217
218static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
219 show_temp_high, set_temp_high);
220static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
221 show_temp_hyst, set_temp_hyst);
222static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
223
224/* voltage */
225static ssize_t show_in(struct device *dev, char *buf, int nr)
226{
227 struct adm9240_data *data = adm9240_update_device(dev);
228 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr], nr));
229}
230
231static ssize_t show_in_min(struct device *dev, char *buf, int nr)
232{
233 struct adm9240_data *data = adm9240_update_device(dev);
234 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr], nr));
235}
236
237static ssize_t show_in_max(struct device *dev, char *buf, int nr)
238{
239 struct adm9240_data *data = adm9240_update_device(dev);
240 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr], nr));
241}
242
243static ssize_t set_in_min(struct device *dev, const char *buf,
244 size_t count, int nr)
245{
246 struct i2c_client *client = to_i2c_client(dev);
247 struct adm9240_data *data = i2c_get_clientdata(client);
248 unsigned long val = simple_strtoul(buf, NULL, 10);
249
250 down(&data->update_lock);
251 data->in_min[nr] = IN_TO_REG(val, nr);
252 adm9240_write_value(client, ADM9240_REG_IN_MIN(nr), data->in_min[nr]);
253 up(&data->update_lock);
254 return count;
255}
256
257static ssize_t set_in_max(struct device *dev, const char *buf,
258 size_t count, int nr)
259{
260 struct i2c_client *client = to_i2c_client(dev);
261 struct adm9240_data *data = i2c_get_clientdata(client);
262 unsigned long val = simple_strtoul(buf, NULL, 10);
263
264 down(&data->update_lock);
265 data->in_max[nr] = IN_TO_REG(val, nr);
266 adm9240_write_value(client, ADM9240_REG_IN_MAX(nr), data->in_max[nr]);
267 up(&data->update_lock);
268 return count;
269}
270
271#define show_in_offset(offset) \
272static ssize_t show_in##offset(struct device *dev, \
273 struct device_attribute *attr, \
274 char *buf) \
275{ \
276 return show_in(dev, buf, offset); \
277} \
278static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL); \
279static ssize_t show_in##offset##_min(struct device *dev, \
280 struct device_attribute *attr, \
281 char *buf) \
282{ \
283 return show_in_min(dev, buf, offset); \
284} \
285static ssize_t show_in##offset##_max(struct device *dev, \
286 struct device_attribute *attr, \
287 char *buf) \
288{ \
289 return show_in_max(dev, buf, offset); \
290} \
291static ssize_t \
292set_in##offset##_min(struct device *dev, \
293 struct device_attribute *attr, const char *buf, \
294 size_t count) \
295{ \
296 return set_in_min(dev, buf, count, offset); \
297} \
298static ssize_t \
299set_in##offset##_max(struct device *dev, \
300 struct device_attribute *attr, const char *buf, \
301 size_t count) \
302{ \
303 return set_in_max(dev, buf, count, offset); \
304} \
305static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
306 show_in##offset##_min, set_in##offset##_min); \
307static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
308 show_in##offset##_max, set_in##offset##_max);
309
310show_in_offset(0);
311show_in_offset(1);
312show_in_offset(2);
313show_in_offset(3);
314show_in_offset(4);
315show_in_offset(5);
316
317/* fans */
318static ssize_t show_fan(struct device *dev, char *buf, int nr)
319{
320 struct adm9240_data *data = adm9240_update_device(dev);
321 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
322 1 << data->fan_div[nr]));
323}
324
325static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
326{
327 struct adm9240_data *data = adm9240_update_device(dev);
328 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
329 1 << data->fan_div[nr]));
330}
331
332static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
333{
334 struct adm9240_data *data = adm9240_update_device(dev);
335 return sprintf(buf, "%d\n", 1 << data->fan_div[nr]);
336}
337
338/* write new fan div, callers must hold data->update_lock */
339static void adm9240_write_fan_div(struct i2c_client *client, int nr,
340 u8 fan_div)
341{
342 u8 reg, old, shift = (nr + 2) * 2;
343
344 reg = adm9240_read_value(client, ADM9240_REG_VID_FAN_DIV);
345 old = (reg >> shift) & 3;
346 reg &= ~(3 << shift);
347 reg |= (fan_div << shift);
348 adm9240_write_value(client, ADM9240_REG_VID_FAN_DIV, reg);
349 dev_dbg(&client->dev, "fan%d clock divider changed from %u "
350 "to %u\n", nr + 1, 1 << old, 1 << fan_div);
351}
352
353/*
354 * set fan speed low limit:
355 *
356 * - value is zero: disable fan speed low limit alarm
357 *
358 * - value is below fan speed measurement range: enable fan speed low
359 * limit alarm to be asserted while fan speed too slow to measure
360 *
361 * - otherwise: select fan clock divider to suit fan speed low limit,
362 * measurement code may adjust registers to ensure fan speed reading
363 */
364static ssize_t set_fan_min(struct device *dev, const char *buf,
365 size_t count, int nr)
366{
367 struct i2c_client *client = to_i2c_client(dev);
368 struct adm9240_data *data = i2c_get_clientdata(client);
369 unsigned long val = simple_strtoul(buf, NULL, 10);
370 u8 new_div;
371
372 down(&data->update_lock);
373
374 if (!val) {
375 data->fan_min[nr] = 255;
376 new_div = data->fan_div[nr];
377
378 dev_dbg(&client->dev, "fan%u low limit set disabled\n",
379 nr + 1);
380
381 } else if (val < 1350000 / (8 * 254)) {
382 new_div = 3;
383 data->fan_min[nr] = 254;
384
385 dev_dbg(&client->dev, "fan%u low limit set minimum %u\n",
386 nr + 1, FAN_FROM_REG(254, 1 << new_div));
387
388 } else {
389 unsigned int new_min = 1350000 / val;
390
391 new_div = 0;
392 while (new_min > 192 && new_div < 3) {
393 new_div++;
394 new_min /= 2;
395 }
396 if (!new_min) /* keep > 0 */
397 new_min++;
398
399 data->fan_min[nr] = new_min;
400
401 dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n",
402 nr + 1, FAN_FROM_REG(new_min, 1 << new_div));
403 }
404
405 if (new_div != data->fan_div[nr]) {
406 data->fan_div[nr] = new_div;
407 adm9240_write_fan_div(client, nr, new_div);
408 }
409 adm9240_write_value(client, ADM9240_REG_FAN_MIN(nr),
410 data->fan_min[nr]);
411
412 up(&data->update_lock);
413 return count;
414}
415
416#define show_fan_offset(offset) \
417static ssize_t show_fan_##offset (struct device *dev, \
418 struct device_attribute *attr, \
419 char *buf) \
420{ \
421return show_fan(dev, buf, offset - 1); \
422} \
423static ssize_t show_fan_##offset##_div (struct device *dev, \
424 struct device_attribute *attr, \
425 char *buf) \
426{ \
427return show_fan_div(dev, buf, offset - 1); \
428} \
429static ssize_t show_fan_##offset##_min (struct device *dev, \
430 struct device_attribute *attr, \
431 char *buf) \
432{ \
433return show_fan_min(dev, buf, offset - 1); \
434} \
435static ssize_t set_fan_##offset##_min (struct device *dev, \
436 struct device_attribute *attr, \
437 const char *buf, size_t count) \
438{ \
439return set_fan_min(dev, buf, count, offset - 1); \
440} \
441static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
442 show_fan_##offset, NULL); \
443static DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
444 show_fan_##offset##_div, NULL); \
445static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
446 show_fan_##offset##_min, set_fan_##offset##_min);
447
448show_fan_offset(1);
449show_fan_offset(2);
450
451/* alarms */
452static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
453{
454 struct adm9240_data *data = adm9240_update_device(dev);
455 return sprintf(buf, "%u\n", data->alarms);
456}
457static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
458
459/* vid */
460static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
461{
462 struct adm9240_data *data = adm9240_update_device(dev);
463 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
464}
465static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
466
467/* analog output */
468static ssize_t show_aout(struct device *dev, struct device_attribute *attr, char *buf)
469{
470 struct adm9240_data *data = adm9240_update_device(dev);
471 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
472}
473
474static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
475{
476 struct i2c_client *client = to_i2c_client(dev);
477 struct adm9240_data *data = i2c_get_clientdata(client);
478 unsigned long val = simple_strtol(buf, NULL, 10);
479
480 down(&data->update_lock);
481 data->aout = AOUT_TO_REG(val);
482 adm9240_write_value(client, ADM9240_REG_ANALOG_OUT, data->aout);
483 up(&data->update_lock);
484 return count;
485}
486static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
487
488/* chassis_clear */
489static ssize_t chassis_clear(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
490{
491 struct i2c_client *client = to_i2c_client(dev);
492 unsigned long val = simple_strtol(buf, NULL, 10);
493
494 if (val == 1) {
495 adm9240_write_value(client, ADM9240_REG_CHASSIS_CLEAR, 0x80);
496 dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
497 }
498 return count;
499}
500static DEVICE_ATTR(chassis_clear, S_IWUSR, NULL, chassis_clear);
501
502
503/*** sensor chip detect and driver install ***/
504
505static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind)
506{
507 struct i2c_client *new_client;
508 struct adm9240_data *data;
509 int err = 0;
510 const char *name = "";
511 u8 man_id, die_rev;
512
513 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
514 goto exit;
515
516 if (!(data = kmalloc(sizeof(struct adm9240_data), GFP_KERNEL))) {
517 err = -ENOMEM;
518 goto exit;
519 }
520 memset(data, 0, sizeof(struct adm9240_data));
521
522 new_client = &data->client;
523 i2c_set_clientdata(new_client, data);
524 new_client->addr = address;
525 new_client->adapter = adapter;
526 new_client->driver = &adm9240_driver;
527 new_client->flags = 0;
528
529 if (kind == 0) {
530 kind = adm9240;
531 }
532
533 if (kind < 0) {
534
535 /* verify chip: reg address should match i2c address */
536 if (adm9240_read_value(new_client, ADM9240_REG_I2C_ADDR)
537 != address) {
538 dev_err(&adapter->dev, "detect fail: address match, "
539 "0x%02x\n", address);
540 goto exit_free;
541 }
542
543 /* check known chip manufacturer */
544 man_id = adm9240_read_value(new_client, ADM9240_REG_MAN_ID);
545
546 if (man_id == 0x23) {
547 kind = adm9240;
548 } else if (man_id == 0xda) {
549 kind = ds1780;
550 } else if (man_id == 0x01) {
551 kind = lm81;
552 } else {
553 dev_err(&adapter->dev, "detect fail: unknown manuf, "
554 "0x%02x\n", man_id);
555 goto exit_free;
556 }
557
558 /* successful detect, print chip info */
559 die_rev = adm9240_read_value(new_client, ADM9240_REG_DIE_REV);
560 dev_info(&adapter->dev, "found %s revision %u\n",
561 man_id == 0x23 ? "ADM9240" :
562 man_id == 0xda ? "DS1780" : "LM81", die_rev);
563 }
564
565 /* either forced or detected chip kind */
566 if (kind == adm9240) {
567 name = "adm9240";
568 } else if (kind == ds1780) {
569 name = "ds1780";
570 } else if (kind == lm81) {
571 name = "lm81";
572 }
573
574 /* fill in the remaining client fields and attach */
575 strlcpy(new_client->name, name, I2C_NAME_SIZE);
576 data->type = kind;
577 init_MUTEX(&data->update_lock);
578
579 if ((err = i2c_attach_client(new_client)))
580 goto exit_free;
581
582 adm9240_init_client(new_client);
583
584 /* populate sysfs filesystem */
585 device_create_file(&new_client->dev, &dev_attr_in0_input);
586 device_create_file(&new_client->dev, &dev_attr_in0_min);
587 device_create_file(&new_client->dev, &dev_attr_in0_max);
588 device_create_file(&new_client->dev, &dev_attr_in1_input);
589 device_create_file(&new_client->dev, &dev_attr_in1_min);
590 device_create_file(&new_client->dev, &dev_attr_in1_max);
591 device_create_file(&new_client->dev, &dev_attr_in2_input);
592 device_create_file(&new_client->dev, &dev_attr_in2_min);
593 device_create_file(&new_client->dev, &dev_attr_in2_max);
594 device_create_file(&new_client->dev, &dev_attr_in3_input);
595 device_create_file(&new_client->dev, &dev_attr_in3_min);
596 device_create_file(&new_client->dev, &dev_attr_in3_max);
597 device_create_file(&new_client->dev, &dev_attr_in4_input);
598 device_create_file(&new_client->dev, &dev_attr_in4_min);
599 device_create_file(&new_client->dev, &dev_attr_in4_max);
600 device_create_file(&new_client->dev, &dev_attr_in5_input);
601 device_create_file(&new_client->dev, &dev_attr_in5_min);
602 device_create_file(&new_client->dev, &dev_attr_in5_max);
603 device_create_file(&new_client->dev, &dev_attr_temp1_max);
604 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
605 device_create_file(&new_client->dev, &dev_attr_temp1_input);
606 device_create_file(&new_client->dev, &dev_attr_fan1_input);
607 device_create_file(&new_client->dev, &dev_attr_fan1_div);
608 device_create_file(&new_client->dev, &dev_attr_fan1_min);
609 device_create_file(&new_client->dev, &dev_attr_fan2_input);
610 device_create_file(&new_client->dev, &dev_attr_fan2_div);
611 device_create_file(&new_client->dev, &dev_attr_fan2_min);
612 device_create_file(&new_client->dev, &dev_attr_alarms);
613 device_create_file(&new_client->dev, &dev_attr_aout_output);
614 device_create_file(&new_client->dev, &dev_attr_chassis_clear);
615 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
616
617 return 0;
618exit_free:
619 kfree(new_client);
620exit:
621 return err;
622}
623
624static int adm9240_attach_adapter(struct i2c_adapter *adapter)
625{
626 if (!(adapter->class & I2C_CLASS_HWMON))
627 return 0;
628 return i2c_detect(adapter, &addr_data, adm9240_detect);
629}
630
631static int adm9240_detach_client(struct i2c_client *client)
632{
633 int err;
634
635 if ((err = i2c_detach_client(client))) {
636 dev_err(&client->dev, "Client deregistration failed, "
637 "client not detached.\n");
638 return err;
639 }
640
641 kfree(i2c_get_clientdata(client));
642 return 0;
643}
644
645static void adm9240_init_client(struct i2c_client *client)
646{
647 struct adm9240_data *data = i2c_get_clientdata(client);
648 u8 conf = adm9240_read_value(client, ADM9240_REG_CONFIG);
649 u8 mode = adm9240_read_value(client, ADM9240_REG_TEMP_CONF) & 3;
650
651 data->vrm = i2c_which_vrm(); /* need this to report vid as mV */
652
653 dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10,
654 data->vrm % 10);
655
656 if (conf & 1) { /* measurement cycle running: report state */
657
658 dev_info(&client->dev, "status: config 0x%02x mode %u\n",
659 conf, mode);
660
661 } else { /* cold start: open limits before starting chip */
662 int i;
663
664 for (i = 0; i < 6; i++)
665 {
666 adm9240_write_value(client,
667 ADM9240_REG_IN_MIN(i), 0);
668 adm9240_write_value(client,
669 ADM9240_REG_IN_MAX(i), 255);
670 }
671 adm9240_write_value(client, ADM9240_REG_FAN_MIN(0), 255);
672 adm9240_write_value(client, ADM9240_REG_FAN_MIN(1), 255);
673 adm9240_write_value(client, ADM9240_REG_TEMP_HIGH, 127);
674 adm9240_write_value(client, ADM9240_REG_TEMP_HYST, 127);
675
676 /* start measurement cycle */
677 adm9240_write_value(client, ADM9240_REG_CONFIG, 1);
678
679 dev_info(&client->dev, "cold start: config was 0x%02x "
680 "mode %u\n", conf, mode);
681 }
682}
683
684static struct adm9240_data *adm9240_update_device(struct device *dev)
685{
686 struct i2c_client *client = to_i2c_client(dev);
687 struct adm9240_data *data = i2c_get_clientdata(client);
688 int i;
689
690 down(&data->update_lock);
691
692 /* minimum measurement cycle: 1.75 seconds */
693 if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
694 || !data->valid) {
695
696 for (i = 0; i < 6; i++) /* read voltages */
697 {
698 data->in[i] = adm9240_read_value(client,
699 ADM9240_REG_IN(i));
700 }
701 data->alarms = adm9240_read_value(client,
702 ADM9240_REG_INT(0)) |
703 adm9240_read_value(client,
704 ADM9240_REG_INT(1)) << 8;
705
706 /* read temperature: assume temperature changes less than
707 * 0.5'C per two measurement cycles thus ignore possible
708 * but unlikely aliasing error on lsb reading. --Grant */
709 data->temp = ((adm9240_read_value(client,
710 ADM9240_REG_TEMP) << 8) |
711 adm9240_read_value(client,
712 ADM9240_REG_TEMP_CONF)) / 128;
713
714 for (i = 0; i < 2; i++) /* read fans */
715 {
716 data->fan[i] = adm9240_read_value(client,
717 ADM9240_REG_FAN(i));
718
719 /* adjust fan clock divider on overflow */
720 if (data->valid && data->fan[i] == 255 &&
721 data->fan_div[i] < 3) {
722
723 adm9240_write_fan_div(client, i,
724 ++data->fan_div[i]);
725
726 /* adjust fan_min if active, but not to 0 */
727 if (data->fan_min[i] < 255 &&
728 data->fan_min[i] >= 2)
729 data->fan_min[i] /= 2;
730 }
731 }
732 data->last_updated_measure = jiffies;
733 }
734
735 /* minimum config reading cycle: 300 seconds */
736 if (time_after(jiffies, data->last_updated_config + (HZ * 300))
737 || !data->valid) {
738
739 for (i = 0; i < 6; i++)
740 {
741 data->in_min[i] = adm9240_read_value(client,
742 ADM9240_REG_IN_MIN(i));
743 data->in_max[i] = adm9240_read_value(client,
744 ADM9240_REG_IN_MAX(i));
745 }
746 for (i = 0; i < 2; i++)
747 {
748 data->fan_min[i] = adm9240_read_value(client,
749 ADM9240_REG_FAN_MIN(i));
750 }
751 data->temp_high = adm9240_read_value(client,
752 ADM9240_REG_TEMP_HIGH);
753 data->temp_hyst = adm9240_read_value(client,
754 ADM9240_REG_TEMP_HYST);
755
756 /* read fan divs and 5-bit VID */
757 i = adm9240_read_value(client, ADM9240_REG_VID_FAN_DIV);
758 data->fan_div[0] = (i >> 4) & 3;
759 data->fan_div[1] = (i >> 6) & 3;
760 data->vid = i & 0x0f;
761 data->vid |= (adm9240_read_value(client,
762 ADM9240_REG_VID4) & 1) << 4;
763 /* read analog out */
764 data->aout = adm9240_read_value(client,
765 ADM9240_REG_ANALOG_OUT);
766
767 data->last_updated_config = jiffies;
768 data->valid = 1;
769 }
770 up(&data->update_lock);
771 return data;
772}
773
774static int __init sensors_adm9240_init(void)
775{
776 return i2c_add_driver(&adm9240_driver);
777}
778
779static void __exit sensors_adm9240_exit(void)
780{
781 i2c_del_driver(&adm9240_driver);
782}
783
784MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
785 "Grant Coady <gcoady@gmail.com> and others");
786MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
787MODULE_LICENSE("GPL");
788
789module_init(sensors_adm9240_init);
790module_exit(sensors_adm9240_exit);
791
diff --git a/drivers/hwmon/asb100.c b/drivers/hwmon/asb100.c
new file mode 100644
index 000000000000..70d996d6fe0a
--- /dev/null
+++ b/drivers/hwmon/asb100.c
@@ -0,0 +1,1065 @@
1/*
2 asb100.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4
5 Copyright (C) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
6
7 (derived from w83781d.c)
8
9 Copyright (C) 1998 - 2003 Frodo Looijaard <frodol@dds.nl>,
10 Philip Edelbrock <phil@netroedge.com>, and
11 Mark Studebaker <mdsxyz123@yahoo.com>
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26*/
27
28/*
29 This driver supports the hardware sensor chips: Asus ASB100 and
30 ASB100-A "BACH".
31
32 ASB100-A supports pwm1, while plain ASB100 does not. There is no known
33 way for the driver to tell which one is there.
34
35 Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
36 asb100 7 3 1 4 0x31 0x0694 yes no
37*/
38
39#include <linux/module.h>
40#include <linux/slab.h>
41#include <linux/i2c.h>
42#include <linux/i2c-sensor.h>
43#include <linux/i2c-vid.h>
44#include <linux/init.h>
45#include <linux/jiffies.h>
46#include "lm75.h"
47
48/*
49 HISTORY:
50 2003-12-29 1.0.0 Ported from lm_sensors project for kernel 2.6
51*/
52#define ASB100_VERSION "1.0.0"
53
54/* I2C addresses to scan */
55static unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
56
57/* ISA addresses to scan (none) */
58static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
59
60/* Insmod parameters */
61SENSORS_INSMOD_1(asb100);
62I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
63 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
64
65/* Voltage IN registers 0-6 */
66#define ASB100_REG_IN(nr) (0x20 + (nr))
67#define ASB100_REG_IN_MAX(nr) (0x2b + (nr * 2))
68#define ASB100_REG_IN_MIN(nr) (0x2c + (nr * 2))
69
70/* FAN IN registers 1-3 */
71#define ASB100_REG_FAN(nr) (0x28 + (nr))
72#define ASB100_REG_FAN_MIN(nr) (0x3b + (nr))
73
74/* TEMPERATURE registers 1-4 */
75static const u16 asb100_reg_temp[] = {0, 0x27, 0x150, 0x250, 0x17};
76static const u16 asb100_reg_temp_max[] = {0, 0x39, 0x155, 0x255, 0x18};
77static const u16 asb100_reg_temp_hyst[] = {0, 0x3a, 0x153, 0x253, 0x19};
78
79#define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])
80#define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])
81#define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])
82
83#define ASB100_REG_TEMP2_CONFIG 0x0152
84#define ASB100_REG_TEMP3_CONFIG 0x0252
85
86
87#define ASB100_REG_CONFIG 0x40
88#define ASB100_REG_ALARM1 0x41
89#define ASB100_REG_ALARM2 0x42
90#define ASB100_REG_SMIM1 0x43
91#define ASB100_REG_SMIM2 0x44
92#define ASB100_REG_VID_FANDIV 0x47
93#define ASB100_REG_I2C_ADDR 0x48
94#define ASB100_REG_CHIPID 0x49
95#define ASB100_REG_I2C_SUBADDR 0x4a
96#define ASB100_REG_PIN 0x4b
97#define ASB100_REG_IRQ 0x4c
98#define ASB100_REG_BANK 0x4e
99#define ASB100_REG_CHIPMAN 0x4f
100
101#define ASB100_REG_WCHIPID 0x58
102
103/* bit 7 -> enable, bits 0-3 -> duty cycle */
104#define ASB100_REG_PWM1 0x59
105
106/* CONVERSIONS
107 Rounding and limit checking is only done on the TO_REG variants. */
108
109/* These constants are a guess, consistent w/ w83781d */
110#define ASB100_IN_MIN ( 0)
111#define ASB100_IN_MAX (4080)
112
113/* IN: 1/1000 V (0V to 4.08V)
114 REG: 16mV/bit */
115static u8 IN_TO_REG(unsigned val)
116{
117 unsigned nval = SENSORS_LIMIT(val, ASB100_IN_MIN, ASB100_IN_MAX);
118 return (nval + 8) / 16;
119}
120
121static unsigned IN_FROM_REG(u8 reg)
122{
123 return reg * 16;
124}
125
126static u8 FAN_TO_REG(long rpm, int div)
127{
128 if (rpm == -1)
129 return 0;
130 if (rpm == 0)
131 return 255;
132 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
133 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
134}
135
136static int FAN_FROM_REG(u8 val, int div)
137{
138 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
139}
140
141/* These constants are a guess, consistent w/ w83781d */
142#define ASB100_TEMP_MIN (-128000)
143#define ASB100_TEMP_MAX ( 127000)
144
145/* TEMP: 0.001C/bit (-128C to +127C)
146 REG: 1C/bit, two's complement */
147static u8 TEMP_TO_REG(int temp)
148{
149 int ntemp = SENSORS_LIMIT(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX);
150 ntemp += (ntemp<0 ? -500 : 500);
151 return (u8)(ntemp / 1000);
152}
153
154static int TEMP_FROM_REG(u8 reg)
155{
156 return (s8)reg * 1000;
157}
158
159/* PWM: 0 - 255 per sensors documentation
160 REG: (6.25% duty cycle per bit) */
161static u8 ASB100_PWM_TO_REG(int pwm)
162{
163 pwm = SENSORS_LIMIT(pwm, 0, 255);
164 return (u8)(pwm / 16);
165}
166
167static int ASB100_PWM_FROM_REG(u8 reg)
168{
169 return reg * 16;
170}
171
172#define DIV_FROM_REG(val) (1 << (val))
173
174/* FAN DIV: 1, 2, 4, or 8 (defaults to 2)
175 REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */
176static u8 DIV_TO_REG(long val)
177{
178 return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1;
179}
180
181/* For each registered client, we need to keep some data in memory. That
182 data is pointed to by client->data. The structure itself is
183 dynamically allocated, at the same time the client itself is allocated. */
184struct asb100_data {
185 struct i2c_client client;
186 struct semaphore lock;
187 enum chips type;
188
189 struct semaphore update_lock;
190 unsigned long last_updated; /* In jiffies */
191
192 /* array of 2 pointers to subclients */
193 struct i2c_client *lm75[2];
194
195 char valid; /* !=0 if following fields are valid */
196 u8 in[7]; /* Register value */
197 u8 in_max[7]; /* Register value */
198 u8 in_min[7]; /* Register value */
199 u8 fan[3]; /* Register value */
200 u8 fan_min[3]; /* Register value */
201 u16 temp[4]; /* Register value (0 and 3 are u8 only) */
202 u16 temp_max[4]; /* Register value (0 and 3 are u8 only) */
203 u16 temp_hyst[4]; /* Register value (0 and 3 are u8 only) */
204 u8 fan_div[3]; /* Register encoding, right justified */
205 u8 pwm; /* Register encoding */
206 u8 vid; /* Register encoding, combined */
207 u32 alarms; /* Register encoding, combined */
208 u8 vrm;
209};
210
211static int asb100_read_value(struct i2c_client *client, u16 reg);
212static void asb100_write_value(struct i2c_client *client, u16 reg, u16 val);
213
214static int asb100_attach_adapter(struct i2c_adapter *adapter);
215static int asb100_detect(struct i2c_adapter *adapter, int address, int kind);
216static int asb100_detach_client(struct i2c_client *client);
217static struct asb100_data *asb100_update_device(struct device *dev);
218static void asb100_init_client(struct i2c_client *client);
219
220static struct i2c_driver asb100_driver = {
221 .owner = THIS_MODULE,
222 .name = "asb100",
223 .id = I2C_DRIVERID_ASB100,
224 .flags = I2C_DF_NOTIFY,
225 .attach_adapter = asb100_attach_adapter,
226 .detach_client = asb100_detach_client,
227};
228
229/* 7 Voltages */
230#define show_in_reg(reg) \
231static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
232{ \
233 struct asb100_data *data = asb100_update_device(dev); \
234 return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
235}
236
237show_in_reg(in)
238show_in_reg(in_min)
239show_in_reg(in_max)
240
241#define set_in_reg(REG, reg) \
242static ssize_t set_in_##reg(struct device *dev, const char *buf, \
243 size_t count, int nr) \
244{ \
245 struct i2c_client *client = to_i2c_client(dev); \
246 struct asb100_data *data = i2c_get_clientdata(client); \
247 unsigned long val = simple_strtoul(buf, NULL, 10); \
248 \
249 down(&data->update_lock); \
250 data->in_##reg[nr] = IN_TO_REG(val); \
251 asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
252 data->in_##reg[nr]); \
253 up(&data->update_lock); \
254 return count; \
255}
256
257set_in_reg(MIN, min)
258set_in_reg(MAX, max)
259
260#define sysfs_in(offset) \
261static ssize_t \
262 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
263{ \
264 return show_in(dev, buf, offset); \
265} \
266static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
267 show_in##offset, NULL); \
268static ssize_t \
269 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
270{ \
271 return show_in_min(dev, buf, offset); \
272} \
273static ssize_t \
274 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
275{ \
276 return show_in_max(dev, buf, offset); \
277} \
278static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
279 const char *buf, size_t count) \
280{ \
281 return set_in_min(dev, buf, count, offset); \
282} \
283static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
284 const char *buf, size_t count) \
285{ \
286 return set_in_max(dev, buf, count, offset); \
287} \
288static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
289 show_in##offset##_min, set_in##offset##_min); \
290static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
291 show_in##offset##_max, set_in##offset##_max);
292
293sysfs_in(0);
294sysfs_in(1);
295sysfs_in(2);
296sysfs_in(3);
297sysfs_in(4);
298sysfs_in(5);
299sysfs_in(6);
300
301#define device_create_file_in(client, offset) do { \
302 device_create_file(&client->dev, &dev_attr_in##offset##_input); \
303 device_create_file(&client->dev, &dev_attr_in##offset##_min); \
304 device_create_file(&client->dev, &dev_attr_in##offset##_max); \
305} while (0)
306
307/* 3 Fans */
308static ssize_t show_fan(struct device *dev, char *buf, int nr)
309{
310 struct asb100_data *data = asb100_update_device(dev);
311 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
312 DIV_FROM_REG(data->fan_div[nr])));
313}
314
315static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
316{
317 struct asb100_data *data = asb100_update_device(dev);
318 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
319 DIV_FROM_REG(data->fan_div[nr])));
320}
321
322static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
323{
324 struct asb100_data *data = asb100_update_device(dev);
325 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
326}
327
328static ssize_t set_fan_min(struct device *dev, const char *buf,
329 size_t count, int nr)
330{
331 struct i2c_client *client = to_i2c_client(dev);
332 struct asb100_data *data = i2c_get_clientdata(client);
333 u32 val = simple_strtoul(buf, NULL, 10);
334
335 down(&data->update_lock);
336 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
337 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
338 up(&data->update_lock);
339 return count;
340}
341
342/* Note: we save and restore the fan minimum here, because its value is
343 determined in part by the fan divisor. This follows the principle of
344 least suprise; the user doesn't expect the fan minimum to change just
345 because the divisor changed. */
346static ssize_t set_fan_div(struct device *dev, const char *buf,
347 size_t count, int nr)
348{
349 struct i2c_client *client = to_i2c_client(dev);
350 struct asb100_data *data = i2c_get_clientdata(client);
351 unsigned long min;
352 unsigned long val = simple_strtoul(buf, NULL, 10);
353 int reg;
354
355 down(&data->update_lock);
356
357 min = FAN_FROM_REG(data->fan_min[nr],
358 DIV_FROM_REG(data->fan_div[nr]));
359 data->fan_div[nr] = DIV_TO_REG(val);
360
361 switch(nr) {
362 case 0: /* fan 1 */
363 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
364 reg = (reg & 0xcf) | (data->fan_div[0] << 4);
365 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
366 break;
367
368 case 1: /* fan 2 */
369 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
370 reg = (reg & 0x3f) | (data->fan_div[1] << 6);
371 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
372 break;
373
374 case 2: /* fan 3 */
375 reg = asb100_read_value(client, ASB100_REG_PIN);
376 reg = (reg & 0x3f) | (data->fan_div[2] << 6);
377 asb100_write_value(client, ASB100_REG_PIN, reg);
378 break;
379 }
380
381 data->fan_min[nr] =
382 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
383 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
384
385 up(&data->update_lock);
386
387 return count;
388}
389
390#define sysfs_fan(offset) \
391static ssize_t show_fan##offset(struct device *dev, struct device_attribute *attr, char *buf) \
392{ \
393 return show_fan(dev, buf, offset - 1); \
394} \
395static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
396{ \
397 return show_fan_min(dev, buf, offset - 1); \
398} \
399static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
400{ \
401 return show_fan_div(dev, buf, offset - 1); \
402} \
403static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
404 size_t count) \
405{ \
406 return set_fan_min(dev, buf, count, offset - 1); \
407} \
408static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \
409 size_t count) \
410{ \
411 return set_fan_div(dev, buf, count, offset - 1); \
412} \
413static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
414 show_fan##offset, NULL); \
415static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
416 show_fan##offset##_min, set_fan##offset##_min); \
417static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
418 show_fan##offset##_div, set_fan##offset##_div);
419
420sysfs_fan(1);
421sysfs_fan(2);
422sysfs_fan(3);
423
424#define device_create_file_fan(client, offset) do { \
425 device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
426 device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
427 device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
428} while (0)
429
430/* 4 Temp. Sensors */
431static int sprintf_temp_from_reg(u16 reg, char *buf, int nr)
432{
433 int ret = 0;
434
435 switch (nr) {
436 case 1: case 2:
437 ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg));
438 break;
439 case 0: case 3: default:
440 ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
441 break;
442 }
443 return ret;
444}
445
446#define show_temp_reg(reg) \
447static ssize_t show_##reg(struct device *dev, char *buf, int nr) \
448{ \
449 struct asb100_data *data = asb100_update_device(dev); \
450 return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
451}
452
453show_temp_reg(temp);
454show_temp_reg(temp_max);
455show_temp_reg(temp_hyst);
456
457#define set_temp_reg(REG, reg) \
458static ssize_t set_##reg(struct device *dev, const char *buf, \
459 size_t count, int nr) \
460{ \
461 struct i2c_client *client = to_i2c_client(dev); \
462 struct asb100_data *data = i2c_get_clientdata(client); \
463 unsigned long val = simple_strtoul(buf, NULL, 10); \
464 \
465 down(&data->update_lock); \
466 switch (nr) { \
467 case 1: case 2: \
468 data->reg[nr] = LM75_TEMP_TO_REG(val); \
469 break; \
470 case 0: case 3: default: \
471 data->reg[nr] = TEMP_TO_REG(val); \
472 break; \
473 } \
474 asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
475 data->reg[nr]); \
476 up(&data->update_lock); \
477 return count; \
478}
479
480set_temp_reg(MAX, temp_max);
481set_temp_reg(HYST, temp_hyst);
482
483#define sysfs_temp(num) \
484static ssize_t show_temp##num(struct device *dev, struct device_attribute *attr, char *buf) \
485{ \
486 return show_temp(dev, buf, num-1); \
487} \
488static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL); \
489static ssize_t show_temp_max##num(struct device *dev, struct device_attribute *attr, char *buf) \
490{ \
491 return show_temp_max(dev, buf, num-1); \
492} \
493static ssize_t set_temp_max##num(struct device *dev, struct device_attribute *attr, const char *buf, \
494 size_t count) \
495{ \
496 return set_temp_max(dev, buf, count, num-1); \
497} \
498static DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
499 show_temp_max##num, set_temp_max##num); \
500static ssize_t show_temp_hyst##num(struct device *dev, struct device_attribute *attr, char *buf) \
501{ \
502 return show_temp_hyst(dev, buf, num-1); \
503} \
504static ssize_t set_temp_hyst##num(struct device *dev, struct device_attribute *attr, const char *buf, \
505 size_t count) \
506{ \
507 return set_temp_hyst(dev, buf, count, num-1); \
508} \
509static DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
510 show_temp_hyst##num, set_temp_hyst##num);
511
512sysfs_temp(1);
513sysfs_temp(2);
514sysfs_temp(3);
515sysfs_temp(4);
516
517/* VID */
518#define device_create_file_temp(client, num) do { \
519 device_create_file(&client->dev, &dev_attr_temp##num##_input); \
520 device_create_file(&client->dev, &dev_attr_temp##num##_max); \
521 device_create_file(&client->dev, &dev_attr_temp##num##_max_hyst); \
522} while (0)
523
524static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
525{
526 struct asb100_data *data = asb100_update_device(dev);
527 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
528}
529
530static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
531#define device_create_file_vid(client) \
532device_create_file(&client->dev, &dev_attr_cpu0_vid)
533
534/* VRM */
535static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
536{
537 struct asb100_data *data = asb100_update_device(dev);
538 return sprintf(buf, "%d\n", data->vrm);
539}
540
541static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
542{
543 struct i2c_client *client = to_i2c_client(dev);
544 struct asb100_data *data = i2c_get_clientdata(client);
545 unsigned long val = simple_strtoul(buf, NULL, 10);
546 data->vrm = val;
547 return count;
548}
549
550/* Alarms */
551static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
552#define device_create_file_vrm(client) \
553device_create_file(&client->dev, &dev_attr_vrm);
554
555static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
556{
557 struct asb100_data *data = asb100_update_device(dev);
558 return sprintf(buf, "%u\n", data->alarms);
559}
560
561static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
562#define device_create_file_alarms(client) \
563device_create_file(&client->dev, &dev_attr_alarms)
564
565/* 1 PWM */
566static ssize_t show_pwm1(struct device *dev, struct device_attribute *attr, char *buf)
567{
568 struct asb100_data *data = asb100_update_device(dev);
569 return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
570}
571
572static ssize_t set_pwm1(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
573{
574 struct i2c_client *client = to_i2c_client(dev);
575 struct asb100_data *data = i2c_get_clientdata(client);
576 unsigned long val = simple_strtoul(buf, NULL, 10);
577
578 down(&data->update_lock);
579 data->pwm &= 0x80; /* keep the enable bit */
580 data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
581 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
582 up(&data->update_lock);
583 return count;
584}
585
586static ssize_t show_pwm_enable1(struct device *dev, struct device_attribute *attr, char *buf)
587{
588 struct asb100_data *data = asb100_update_device(dev);
589 return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
590}
591
592static ssize_t set_pwm_enable1(struct device *dev, struct device_attribute *attr, const char *buf,
593 size_t count)
594{
595 struct i2c_client *client = to_i2c_client(dev);
596 struct asb100_data *data = i2c_get_clientdata(client);
597 unsigned long val = simple_strtoul(buf, NULL, 10);
598
599 down(&data->update_lock);
600 data->pwm &= 0x0f; /* keep the duty cycle bits */
601 data->pwm |= (val ? 0x80 : 0x00);
602 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
603 up(&data->update_lock);
604 return count;
605}
606
607static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm1, set_pwm1);
608static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
609 show_pwm_enable1, set_pwm_enable1);
610#define device_create_file_pwm1(client) do { \
611 device_create_file(&new_client->dev, &dev_attr_pwm1); \
612 device_create_file(&new_client->dev, &dev_attr_pwm1_enable); \
613} while (0)
614
615/* This function is called when:
616 asb100_driver is inserted (when this module is loaded), for each
617 available adapter
618 when a new adapter is inserted (and asb100_driver is still present)
619 */
620static int asb100_attach_adapter(struct i2c_adapter *adapter)
621{
622 if (!(adapter->class & I2C_CLASS_HWMON))
623 return 0;
624 return i2c_detect(adapter, &addr_data, asb100_detect);
625}
626
627static int asb100_detect_subclients(struct i2c_adapter *adapter, int address,
628 int kind, struct i2c_client *new_client)
629{
630 int i, id, err;
631 struct asb100_data *data = i2c_get_clientdata(new_client);
632
633 data->lm75[0] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
634 if (!(data->lm75[0])) {
635 err = -ENOMEM;
636 goto ERROR_SC_0;
637 }
638 memset(data->lm75[0], 0x00, sizeof(struct i2c_client));
639
640 data->lm75[1] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
641 if (!(data->lm75[1])) {
642 err = -ENOMEM;
643 goto ERROR_SC_1;
644 }
645 memset(data->lm75[1], 0x00, sizeof(struct i2c_client));
646
647 id = i2c_adapter_id(adapter);
648
649 if (force_subclients[0] == id && force_subclients[1] == address) {
650 for (i = 2; i <= 3; i++) {
651 if (force_subclients[i] < 0x48 ||
652 force_subclients[i] > 0x4f) {
653 dev_err(&new_client->dev, "invalid subclient "
654 "address %d; must be 0x48-0x4f\n",
655 force_subclients[i]);
656 err = -ENODEV;
657 goto ERROR_SC_2;
658 }
659 }
660 asb100_write_value(new_client, ASB100_REG_I2C_SUBADDR,
661 (force_subclients[2] & 0x07) |
662 ((force_subclients[3] & 0x07) <<4));
663 data->lm75[0]->addr = force_subclients[2];
664 data->lm75[1]->addr = force_subclients[3];
665 } else {
666 int val = asb100_read_value(new_client, ASB100_REG_I2C_SUBADDR);
667 data->lm75[0]->addr = 0x48 + (val & 0x07);
668 data->lm75[1]->addr = 0x48 + ((val >> 4) & 0x07);
669 }
670
671 if(data->lm75[0]->addr == data->lm75[1]->addr) {
672 dev_err(&new_client->dev, "duplicate addresses 0x%x "
673 "for subclients\n", data->lm75[0]->addr);
674 err = -ENODEV;
675 goto ERROR_SC_2;
676 }
677
678 for (i = 0; i <= 1; i++) {
679 i2c_set_clientdata(data->lm75[i], NULL);
680 data->lm75[i]->adapter = adapter;
681 data->lm75[i]->driver = &asb100_driver;
682 data->lm75[i]->flags = 0;
683 strlcpy(data->lm75[i]->name, "asb100 subclient", I2C_NAME_SIZE);
684 }
685
686 if ((err = i2c_attach_client(data->lm75[0]))) {
687 dev_err(&new_client->dev, "subclient %d registration "
688 "at address 0x%x failed.\n", i, data->lm75[0]->addr);
689 goto ERROR_SC_2;
690 }
691
692 if ((err = i2c_attach_client(data->lm75[1]))) {
693 dev_err(&new_client->dev, "subclient %d registration "
694 "at address 0x%x failed.\n", i, data->lm75[1]->addr);
695 goto ERROR_SC_3;
696 }
697
698 return 0;
699
700/* Undo inits in case of errors */
701ERROR_SC_3:
702 i2c_detach_client(data->lm75[0]);
703ERROR_SC_2:
704 kfree(data->lm75[1]);
705ERROR_SC_1:
706 kfree(data->lm75[0]);
707ERROR_SC_0:
708 return err;
709}
710
711static int asb100_detect(struct i2c_adapter *adapter, int address, int kind)
712{
713 int err;
714 struct i2c_client *new_client;
715 struct asb100_data *data;
716
717 /* asb100 is SMBus only */
718 if (i2c_is_isa_adapter(adapter)) {
719 pr_debug("asb100.o: detect failed, "
720 "cannot attach to legacy adapter!\n");
721 err = -ENODEV;
722 goto ERROR0;
723 }
724
725 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
726 pr_debug("asb100.o: detect failed, "
727 "smbus byte data not supported!\n");
728 err = -ENODEV;
729 goto ERROR0;
730 }
731
732 /* OK. For now, we presume we have a valid client. We now create the
733 client structure, even though we cannot fill it completely yet.
734 But it allows us to access asb100_{read,write}_value. */
735
736 if (!(data = kmalloc(sizeof(struct asb100_data), GFP_KERNEL))) {
737 pr_debug("asb100.o: detect failed, kmalloc failed!\n");
738 err = -ENOMEM;
739 goto ERROR0;
740 }
741 memset(data, 0, sizeof(struct asb100_data));
742
743 new_client = &data->client;
744 init_MUTEX(&data->lock);
745 i2c_set_clientdata(new_client, data);
746 new_client->addr = address;
747 new_client->adapter = adapter;
748 new_client->driver = &asb100_driver;
749 new_client->flags = 0;
750
751 /* Now, we do the remaining detection. */
752
753 /* The chip may be stuck in some other bank than bank 0. This may
754 make reading other information impossible. Specify a force=... or
755 force_*=... parameter, and the chip will be reset to the right
756 bank. */
757 if (kind < 0) {
758
759 int val1 = asb100_read_value(new_client, ASB100_REG_BANK);
760 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
761
762 /* If we're in bank 0 */
763 if ( (!(val1 & 0x07)) &&
764 /* Check for ASB100 ID (low byte) */
765 ( ((!(val1 & 0x80)) && (val2 != 0x94)) ||
766 /* Check for ASB100 ID (high byte ) */
767 ((val1 & 0x80) && (val2 != 0x06)) ) ) {
768 pr_debug("asb100.o: detect failed, "
769 "bad chip id 0x%02x!\n", val2);
770 err = -ENODEV;
771 goto ERROR1;
772 }
773
774 } /* kind < 0 */
775
776 /* We have either had a force parameter, or we have already detected
777 Winbond. Put it now into bank 0 and Vendor ID High Byte */
778 asb100_write_value(new_client, ASB100_REG_BANK,
779 (asb100_read_value(new_client, ASB100_REG_BANK) & 0x78) | 0x80);
780
781 /* Determine the chip type. */
782 if (kind <= 0) {
783 int val1 = asb100_read_value(new_client, ASB100_REG_WCHIPID);
784 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
785
786 if ((val1 == 0x31) && (val2 == 0x06))
787 kind = asb100;
788 else {
789 if (kind == 0)
790 dev_warn(&new_client->dev, "ignoring "
791 "'force' parameter for unknown chip "
792 "at adapter %d, address 0x%02x.\n",
793 i2c_adapter_id(adapter), address);
794 err = -ENODEV;
795 goto ERROR1;
796 }
797 }
798
799 /* Fill in remaining client fields and put it into the global list */
800 strlcpy(new_client->name, "asb100", I2C_NAME_SIZE);
801 data->type = kind;
802
803 data->valid = 0;
804 init_MUTEX(&data->update_lock);
805
806 /* Tell the I2C layer a new client has arrived */
807 if ((err = i2c_attach_client(new_client)))
808 goto ERROR1;
809
810 /* Attach secondary lm75 clients */
811 if ((err = asb100_detect_subclients(adapter, address, kind,
812 new_client)))
813 goto ERROR2;
814
815 /* Initialize the chip */
816 asb100_init_client(new_client);
817
818 /* A few vars need to be filled upon startup */
819 data->fan_min[0] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(0));
820 data->fan_min[1] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(1));
821 data->fan_min[2] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(2));
822
823 /* Register sysfs hooks */
824 device_create_file_in(new_client, 0);
825 device_create_file_in(new_client, 1);
826 device_create_file_in(new_client, 2);
827 device_create_file_in(new_client, 3);
828 device_create_file_in(new_client, 4);
829 device_create_file_in(new_client, 5);
830 device_create_file_in(new_client, 6);
831
832 device_create_file_fan(new_client, 1);
833 device_create_file_fan(new_client, 2);
834 device_create_file_fan(new_client, 3);
835
836 device_create_file_temp(new_client, 1);
837 device_create_file_temp(new_client, 2);
838 device_create_file_temp(new_client, 3);
839 device_create_file_temp(new_client, 4);
840
841 device_create_file_vid(new_client);
842 device_create_file_vrm(new_client);
843
844 device_create_file_alarms(new_client);
845
846 device_create_file_pwm1(new_client);
847
848 return 0;
849
850ERROR2:
851 i2c_detach_client(new_client);
852ERROR1:
853 kfree(data);
854ERROR0:
855 return err;
856}
857
858static int asb100_detach_client(struct i2c_client *client)
859{
860 int err;
861
862 if ((err = i2c_detach_client(client))) {
863 dev_err(&client->dev, "client deregistration failed; "
864 "client not detached.\n");
865 return err;
866 }
867
868 if (i2c_get_clientdata(client)==NULL) {
869 /* subclients */
870 kfree(client);
871 } else {
872 /* main client */
873 kfree(i2c_get_clientdata(client));
874 }
875
876 return 0;
877}
878
879/* The SMBus locks itself, usually, but nothing may access the chip between
880 bank switches. */
881static int asb100_read_value(struct i2c_client *client, u16 reg)
882{
883 struct asb100_data *data = i2c_get_clientdata(client);
884 struct i2c_client *cl;
885 int res, bank;
886
887 down(&data->lock);
888
889 bank = (reg >> 8) & 0x0f;
890 if (bank > 2)
891 /* switch banks */
892 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
893
894 if (bank == 0 || bank > 2) {
895 res = i2c_smbus_read_byte_data(client, reg & 0xff);
896 } else {
897 /* switch to subclient */
898 cl = data->lm75[bank - 1];
899
900 /* convert from ISA to LM75 I2C addresses */
901 switch (reg & 0xff) {
902 case 0x50: /* TEMP */
903 res = swab16(i2c_smbus_read_word_data (cl, 0));
904 break;
905 case 0x52: /* CONFIG */
906 res = i2c_smbus_read_byte_data(cl, 1);
907 break;
908 case 0x53: /* HYST */
909 res = swab16(i2c_smbus_read_word_data (cl, 2));
910 break;
911 case 0x55: /* MAX */
912 default:
913 res = swab16(i2c_smbus_read_word_data (cl, 3));
914 break;
915 }
916 }
917
918 if (bank > 2)
919 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
920
921 up(&data->lock);
922
923 return res;
924}
925
926static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
927{
928 struct asb100_data *data = i2c_get_clientdata(client);
929 struct i2c_client *cl;
930 int bank;
931
932 down(&data->lock);
933
934 bank = (reg >> 8) & 0x0f;
935 if (bank > 2)
936 /* switch banks */
937 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
938
939 if (bank == 0 || bank > 2) {
940 i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
941 } else {
942 /* switch to subclient */
943 cl = data->lm75[bank - 1];
944
945 /* convert from ISA to LM75 I2C addresses */
946 switch (reg & 0xff) {
947 case 0x52: /* CONFIG */
948 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
949 break;
950 case 0x53: /* HYST */
951 i2c_smbus_write_word_data(cl, 2, swab16(value));
952 break;
953 case 0x55: /* MAX */
954 i2c_smbus_write_word_data(cl, 3, swab16(value));
955 break;
956 }
957 }
958
959 if (bank > 2)
960 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
961
962 up(&data->lock);
963}
964
965static void asb100_init_client(struct i2c_client *client)
966{
967 struct asb100_data *data = i2c_get_clientdata(client);
968 int vid = 0;
969
970 vid = asb100_read_value(client, ASB100_REG_VID_FANDIV) & 0x0f;
971 vid |= (asb100_read_value(client, ASB100_REG_CHIPID) & 0x01) << 4;
972 data->vrm = i2c_which_vrm();
973 vid = vid_from_reg(vid, data->vrm);
974
975 /* Start monitoring */
976 asb100_write_value(client, ASB100_REG_CONFIG,
977 (asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
978}
979
980static struct asb100_data *asb100_update_device(struct device *dev)
981{
982 struct i2c_client *client = to_i2c_client(dev);
983 struct asb100_data *data = i2c_get_clientdata(client);
984 int i;
985
986 down(&data->update_lock);
987
988 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
989 || !data->valid) {
990
991 dev_dbg(&client->dev, "starting device update...\n");
992
993 /* 7 voltage inputs */
994 for (i = 0; i < 7; i++) {
995 data->in[i] = asb100_read_value(client,
996 ASB100_REG_IN(i));
997 data->in_min[i] = asb100_read_value(client,
998 ASB100_REG_IN_MIN(i));
999 data->in_max[i] = asb100_read_value(client,
1000 ASB100_REG_IN_MAX(i));
1001 }
1002
1003 /* 3 fan inputs */
1004 for (i = 0; i < 3; i++) {
1005 data->fan[i] = asb100_read_value(client,
1006 ASB100_REG_FAN(i));
1007 data->fan_min[i] = asb100_read_value(client,
1008 ASB100_REG_FAN_MIN(i));
1009 }
1010
1011 /* 4 temperature inputs */
1012 for (i = 1; i <= 4; i++) {
1013 data->temp[i-1] = asb100_read_value(client,
1014 ASB100_REG_TEMP(i));
1015 data->temp_max[i-1] = asb100_read_value(client,
1016 ASB100_REG_TEMP_MAX(i));
1017 data->temp_hyst[i-1] = asb100_read_value(client,
1018 ASB100_REG_TEMP_HYST(i));
1019 }
1020
1021 /* VID and fan divisors */
1022 i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
1023 data->vid = i & 0x0f;
1024 data->vid |= (asb100_read_value(client,
1025 ASB100_REG_CHIPID) & 0x01) << 4;
1026 data->fan_div[0] = (i >> 4) & 0x03;
1027 data->fan_div[1] = (i >> 6) & 0x03;
1028 data->fan_div[2] = (asb100_read_value(client,
1029 ASB100_REG_PIN) >> 6) & 0x03;
1030
1031 /* PWM */
1032 data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
1033
1034 /* alarms */
1035 data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
1036 (asb100_read_value(client, ASB100_REG_ALARM2) << 8);
1037
1038 data->last_updated = jiffies;
1039 data->valid = 1;
1040
1041 dev_dbg(&client->dev, "... device update complete\n");
1042 }
1043
1044 up(&data->update_lock);
1045
1046 return data;
1047}
1048
1049static int __init asb100_init(void)
1050{
1051 return i2c_add_driver(&asb100_driver);
1052}
1053
1054static void __exit asb100_exit(void)
1055{
1056 i2c_del_driver(&asb100_driver);
1057}
1058
1059MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1060MODULE_DESCRIPTION("ASB100 Bach driver");
1061MODULE_LICENSE("GPL");
1062
1063module_init(asb100_init);
1064module_exit(asb100_exit);
1065
diff --git a/drivers/hwmon/atxp1.c b/drivers/hwmon/atxp1.c
new file mode 100644
index 000000000000..0bcf82b4c07b
--- /dev/null
+++ b/drivers/hwmon/atxp1.c
@@ -0,0 +1,361 @@
1/*
2 atxp1.c - kernel module for setting CPU VID and general purpose
3 I/Os using the Attansic ATXP1 chip.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19*/
20
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/i2c.h>
25#include <linux/i2c-sensor.h>
26#include <linux/i2c-vid.h>
27
28MODULE_LICENSE("GPL");
29MODULE_DESCRIPTION("System voltages control via Attansic ATXP1");
30MODULE_VERSION("0.6.2");
31MODULE_AUTHOR("Sebastian Witt <se.witt@gmx.net>");
32
33#define ATXP1_VID 0x00
34#define ATXP1_CVID 0x01
35#define ATXP1_GPIO1 0x06
36#define ATXP1_GPIO2 0x0a
37#define ATXP1_VIDENA 0x20
38#define ATXP1_VIDMASK 0x1f
39#define ATXP1_GPIO1MASK 0x0f
40
41static unsigned short normal_i2c[] = { 0x37, 0x4e, I2C_CLIENT_END };
42static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
43
44SENSORS_INSMOD_1(atxp1);
45
46static int atxp1_attach_adapter(struct i2c_adapter * adapter);
47static int atxp1_detach_client(struct i2c_client * client);
48static struct atxp1_data * atxp1_update_device(struct device *dev);
49static int atxp1_detect(struct i2c_adapter *adapter, int address, int kind);
50
51static struct i2c_driver atxp1_driver = {
52 .owner = THIS_MODULE,
53 .name = "atxp1",
54 .flags = I2C_DF_NOTIFY,
55 .attach_adapter = atxp1_attach_adapter,
56 .detach_client = atxp1_detach_client,
57};
58
59struct atxp1_data {
60 struct i2c_client client;
61 struct semaphore update_lock;
62 unsigned long last_updated;
63 u8 valid;
64 struct {
65 u8 vid; /* VID output register */
66 u8 cpu_vid; /* VID input from CPU */
67 u8 gpio1; /* General purpose I/O register 1 */
68 u8 gpio2; /* General purpose I/O register 2 */
69 } reg;
70 u8 vrm; /* Detected CPU VRM */
71};
72
73static struct atxp1_data * atxp1_update_device(struct device *dev)
74{
75 struct i2c_client *client;
76 struct atxp1_data *data;
77
78 client = to_i2c_client(dev);
79 data = i2c_get_clientdata(client);
80
81 down(&data->update_lock);
82
83 if ((jiffies - data->last_updated > HZ) ||
84 (jiffies < data->last_updated) ||
85 !data->valid) {
86
87 /* Update local register data */
88 data->reg.vid = i2c_smbus_read_byte_data(client, ATXP1_VID);
89 data->reg.cpu_vid = i2c_smbus_read_byte_data(client, ATXP1_CVID);
90 data->reg.gpio1 = i2c_smbus_read_byte_data(client, ATXP1_GPIO1);
91 data->reg.gpio2 = i2c_smbus_read_byte_data(client, ATXP1_GPIO2);
92
93 data->valid = 1;
94 }
95
96 up(&data->update_lock);
97
98 return(data);
99}
100
101/* sys file functions for cpu0_vid */
102static ssize_t atxp1_showvcore(struct device *dev, struct device_attribute *attr, char *buf)
103{
104 int size;
105 struct atxp1_data *data;
106
107 data = atxp1_update_device(dev);
108
109 size = sprintf(buf, "%d\n", vid_from_reg(data->reg.vid & ATXP1_VIDMASK, data->vrm));
110
111 return size;
112}
113
114static ssize_t atxp1_storevcore(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
115{
116 struct atxp1_data *data;
117 struct i2c_client *client;
118 char vid;
119 char cvid;
120 unsigned int vcore;
121
122 client = to_i2c_client(dev);
123 data = atxp1_update_device(dev);
124
125 vcore = simple_strtoul(buf, NULL, 10);
126 vcore /= 25;
127 vcore *= 25;
128
129 /* Calculate VID */
130 vid = vid_to_reg(vcore, data->vrm);
131
132 if (vid < 0) {
133 dev_err(dev, "VID calculation failed.\n");
134 return -1;
135 }
136
137 /* If output enabled, use control register value. Otherwise original CPU VID */
138 if (data->reg.vid & ATXP1_VIDENA)
139 cvid = data->reg.vid & ATXP1_VIDMASK;
140 else
141 cvid = data->reg.cpu_vid;
142
143 /* Nothing changed, aborting */
144 if (vid == cvid)
145 return count;
146
147 dev_dbg(dev, "Setting VCore to %d mV (0x%02x)\n", vcore, vid);
148
149 /* Write every 25 mV step to increase stability */
150 if (cvid > vid) {
151 for (; cvid >= vid; cvid--) {
152 i2c_smbus_write_byte_data(client, ATXP1_VID, cvid | ATXP1_VIDENA);
153 }
154 }
155 else {
156 for (; cvid <= vid; cvid++) {
157 i2c_smbus_write_byte_data(client, ATXP1_VID, cvid | ATXP1_VIDENA);
158 }
159 }
160
161 data->valid = 0;
162
163 return count;
164}
165
166/* CPU core reference voltage
167 unit: millivolt
168*/
169static DEVICE_ATTR(cpu0_vid, S_IRUGO | S_IWUSR, atxp1_showvcore, atxp1_storevcore);
170
171/* sys file functions for GPIO1 */
172static ssize_t atxp1_showgpio1(struct device *dev, struct device_attribute *attr, char *buf)
173{
174 int size;
175 struct atxp1_data *data;
176
177 data = atxp1_update_device(dev);
178
179 size = sprintf(buf, "0x%02x\n", data->reg.gpio1 & ATXP1_GPIO1MASK);
180
181 return size;
182}
183
184static ssize_t atxp1_storegpio1(struct device *dev, struct device_attribute *attr, const char*buf, size_t count)
185{
186 struct atxp1_data *data;
187 struct i2c_client *client;
188 unsigned int value;
189
190 client = to_i2c_client(dev);
191 data = atxp1_update_device(dev);
192
193 value = simple_strtoul(buf, NULL, 16);
194
195 value &= ATXP1_GPIO1MASK;
196
197 if (value != (data->reg.gpio1 & ATXP1_GPIO1MASK)) {
198 dev_info(dev, "Writing 0x%x to GPIO1.\n", value);
199
200 i2c_smbus_write_byte_data(client, ATXP1_GPIO1, value);
201
202 data->valid = 0;
203 }
204
205 return count;
206}
207
208/* GPIO1 data register
209 unit: Four bit as hex (e.g. 0x0f)
210*/
211static DEVICE_ATTR(gpio1, S_IRUGO | S_IWUSR, atxp1_showgpio1, atxp1_storegpio1);
212
213/* sys file functions for GPIO2 */
214static ssize_t atxp1_showgpio2(struct device *dev, struct device_attribute *attr, char *buf)
215{
216 int size;
217 struct atxp1_data *data;
218
219 data = atxp1_update_device(dev);
220
221 size = sprintf(buf, "0x%02x\n", data->reg.gpio2);
222
223 return size;
224}
225
226static ssize_t atxp1_storegpio2(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
227{
228 struct atxp1_data *data;
229 struct i2c_client *client;
230 unsigned int value;
231
232 client = to_i2c_client(dev);
233 data = atxp1_update_device(dev);
234
235 value = simple_strtoul(buf, NULL, 16) & 0xff;
236
237 if (value != data->reg.gpio2) {
238 dev_info(dev, "Writing 0x%x to GPIO1.\n", value);
239
240 i2c_smbus_write_byte_data(client, ATXP1_GPIO2, value);
241
242 data->valid = 0;
243 }
244
245 return count;
246}
247
248/* GPIO2 data register
249 unit: Eight bit as hex (e.g. 0xff)
250*/
251static DEVICE_ATTR(gpio2, S_IRUGO | S_IWUSR, atxp1_showgpio2, atxp1_storegpio2);
252
253
254static int atxp1_attach_adapter(struct i2c_adapter *adapter)
255{
256 return i2c_detect(adapter, &addr_data, &atxp1_detect);
257};
258
259static int atxp1_detect(struct i2c_adapter *adapter, int address, int kind)
260{
261 struct i2c_client * new_client;
262 struct atxp1_data * data;
263 int err = 0;
264 u8 temp;
265
266 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
267 goto exit;
268
269 if (!(data = kmalloc(sizeof(struct atxp1_data), GFP_KERNEL))) {
270 err = -ENOMEM;
271 goto exit;
272 }
273
274 memset(data, 0, sizeof(struct atxp1_data));
275 new_client = &data->client;
276 i2c_set_clientdata(new_client, data);
277
278 new_client->addr = address;
279 new_client->adapter = adapter;
280 new_client->driver = &atxp1_driver;
281 new_client->flags = 0;
282
283 /* Detect ATXP1, checking if vendor ID registers are all zero */
284 if (!((i2c_smbus_read_byte_data(new_client, 0x3e) == 0) &&
285 (i2c_smbus_read_byte_data(new_client, 0x3f) == 0) &&
286 (i2c_smbus_read_byte_data(new_client, 0xfe) == 0) &&
287 (i2c_smbus_read_byte_data(new_client, 0xff) == 0) )) {
288
289 /* No vendor ID, now checking if registers 0x10,0x11 (non-existent)
290 * showing the same as register 0x00 */
291 temp = i2c_smbus_read_byte_data(new_client, 0x00);
292
293 if (!((i2c_smbus_read_byte_data(new_client, 0x10) == temp) &&
294 (i2c_smbus_read_byte_data(new_client, 0x11) == temp) ))
295 goto exit_free;
296 }
297
298 /* Get VRM */
299 data->vrm = i2c_which_vrm();
300
301 if ((data->vrm != 90) && (data->vrm != 91)) {
302 dev_err(&new_client->dev, "Not supporting VRM %d.%d\n",
303 data->vrm / 10, data->vrm % 10);
304 goto exit_free;
305 }
306
307 strncpy(new_client->name, "atxp1", I2C_NAME_SIZE);
308
309 data->valid = 0;
310
311 init_MUTEX(&data->update_lock);
312
313 err = i2c_attach_client(new_client);
314
315 if (err)
316 {
317 dev_err(&new_client->dev, "Attach client error.\n");
318 goto exit_free;
319 }
320
321 device_create_file(&new_client->dev, &dev_attr_gpio1);
322 device_create_file(&new_client->dev, &dev_attr_gpio2);
323 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
324
325 dev_info(&new_client->dev, "Using VRM: %d.%d\n",
326 data->vrm / 10, data->vrm % 10);
327
328 return 0;
329
330exit_free:
331 kfree(data);
332exit:
333 return err;
334};
335
336static int atxp1_detach_client(struct i2c_client * client)
337{
338 int err;
339
340 err = i2c_detach_client(client);
341
342 if (err)
343 dev_err(&client->dev, "Failed to detach client.\n");
344 else
345 kfree(i2c_get_clientdata(client));
346
347 return err;
348};
349
350static int __init atxp1_init(void)
351{
352 return i2c_add_driver(&atxp1_driver);
353};
354
355static void __exit atxp1_exit(void)
356{
357 i2c_del_driver(&atxp1_driver);
358};
359
360module_init(atxp1_init);
361module_exit(atxp1_exit);
diff --git a/drivers/hwmon/ds1621.c b/drivers/hwmon/ds1621.c
new file mode 100644
index 000000000000..5360d58804f6
--- /dev/null
+++ b/drivers/hwmon/ds1621.c
@@ -0,0 +1,341 @@
1/*
2 ds1621.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Christian W. Zuckschwerdt <zany@triq.net> 2000-11-23
5 based on lm75.c by Frodo Looijaard <frodol@dds.nl>
6 Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
7 the help of 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*/
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/jiffies.h>
28#include <linux/i2c.h>
29#include <linux/i2c-sensor.h>
30#include "lm75.h"
31
32/* Addresses to scan */
33static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
34 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
35static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
36
37/* Insmod parameters */
38SENSORS_INSMOD_1(ds1621);
39static int polarity = -1;
40module_param(polarity, int, 0);
41MODULE_PARM_DESC(polarity, "Output's polarity: 0 = active high, 1 = active low");
42
43/* Many DS1621 constants specified below */
44/* Config register used for detection */
45/* 7 6 5 4 3 2 1 0 */
46/* |Done|THF |TLF |NVB | X | X |POL |1SHOT| */
47#define DS1621_REG_CONFIG_NVB 0x10
48#define DS1621_REG_CONFIG_POLARITY 0x02
49#define DS1621_REG_CONFIG_1SHOT 0x01
50#define DS1621_REG_CONFIG_DONE 0x80
51
52/* The DS1621 registers */
53#define DS1621_REG_TEMP 0xAA /* word, RO */
54#define DS1621_REG_TEMP_MIN 0xA1 /* word, RW */
55#define DS1621_REG_TEMP_MAX 0xA2 /* word, RW */
56#define DS1621_REG_CONF 0xAC /* byte, RW */
57#define DS1621_COM_START 0xEE /* no data */
58#define DS1621_COM_STOP 0x22 /* no data */
59
60/* The DS1621 configuration register */
61#define DS1621_ALARM_TEMP_HIGH 0x40
62#define DS1621_ALARM_TEMP_LOW 0x20
63
64/* Conversions. Rounding and limit checking is only done on the TO_REG
65 variants. Note that you should be a bit careful with which arguments
66 these macros are called: arguments may be evaluated more than once.
67 Fixing this is just not worth it. */
68#define ALARMS_FROM_REG(val) ((val) & \
69 (DS1621_ALARM_TEMP_HIGH | DS1621_ALARM_TEMP_LOW))
70
71/* Each client has this additional data */
72struct ds1621_data {
73 struct i2c_client client;
74 struct semaphore update_lock;
75 char valid; /* !=0 if following fields are valid */
76 unsigned long last_updated; /* In jiffies */
77
78 u16 temp, temp_min, temp_max; /* Register values, word */
79 u8 conf; /* Register encoding, combined */
80};
81
82static int ds1621_attach_adapter(struct i2c_adapter *adapter);
83static int ds1621_detect(struct i2c_adapter *adapter, int address,
84 int kind);
85static void ds1621_init_client(struct i2c_client *client);
86static int ds1621_detach_client(struct i2c_client *client);
87static struct ds1621_data *ds1621_update_client(struct device *dev);
88
89/* This is the driver that will be inserted */
90static struct i2c_driver ds1621_driver = {
91 .owner = THIS_MODULE,
92 .name = "ds1621",
93 .id = I2C_DRIVERID_DS1621,
94 .flags = I2C_DF_NOTIFY,
95 .attach_adapter = ds1621_attach_adapter,
96 .detach_client = ds1621_detach_client,
97};
98
99/* All registers are word-sized, except for the configuration register.
100 DS1621 uses a high-byte first convention, which is exactly opposite to
101 the usual practice. */
102static int ds1621_read_value(struct i2c_client *client, u8 reg)
103{
104 if (reg == DS1621_REG_CONF)
105 return i2c_smbus_read_byte_data(client, reg);
106 else
107 return swab16(i2c_smbus_read_word_data(client, reg));
108}
109
110/* All registers are word-sized, except for the configuration register.
111 DS1621 uses a high-byte first convention, which is exactly opposite to
112 the usual practice. */
113static int ds1621_write_value(struct i2c_client *client, u8 reg, u16 value)
114{
115 if (reg == DS1621_REG_CONF)
116 return i2c_smbus_write_byte_data(client, reg, value);
117 else
118 return i2c_smbus_write_word_data(client, reg, swab16(value));
119}
120
121static void ds1621_init_client(struct i2c_client *client)
122{
123 int reg = ds1621_read_value(client, DS1621_REG_CONF);
124 /* switch to continuous conversion mode */
125 reg &= ~ DS1621_REG_CONFIG_1SHOT;
126
127 /* setup output polarity */
128 if (polarity == 0)
129 reg &= ~DS1621_REG_CONFIG_POLARITY;
130 else if (polarity == 1)
131 reg |= DS1621_REG_CONFIG_POLARITY;
132
133 ds1621_write_value(client, DS1621_REG_CONF, reg);
134
135 /* start conversion */
136 i2c_smbus_write_byte(client, DS1621_COM_START);
137}
138
139#define show(value) \
140static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
141{ \
142 struct ds1621_data *data = ds1621_update_client(dev); \
143 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->value)); \
144}
145
146show(temp);
147show(temp_min);
148show(temp_max);
149
150#define set_temp(suffix, value, reg) \
151static ssize_t set_temp_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
152 size_t count) \
153{ \
154 struct i2c_client *client = to_i2c_client(dev); \
155 struct ds1621_data *data = ds1621_update_client(dev); \
156 u16 val = LM75_TEMP_TO_REG(simple_strtoul(buf, NULL, 10)); \
157 \
158 down(&data->update_lock); \
159 data->value = val; \
160 ds1621_write_value(client, reg, data->value); \
161 up(&data->update_lock); \
162 return count; \
163}
164
165set_temp(min, temp_min, DS1621_REG_TEMP_MIN);
166set_temp(max, temp_max, DS1621_REG_TEMP_MAX);
167
168static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
169{
170 struct ds1621_data *data = ds1621_update_client(dev);
171 return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->conf));
172}
173
174static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
175static DEVICE_ATTR(temp1_input, S_IRUGO , show_temp, NULL);
176static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO , show_temp_min, set_temp_min);
177static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
178
179
180static int ds1621_attach_adapter(struct i2c_adapter *adapter)
181{
182 return i2c_detect(adapter, &addr_data, ds1621_detect);
183}
184
185/* This function is called by i2c_detect */
186int ds1621_detect(struct i2c_adapter *adapter, int address,
187 int kind)
188{
189 int conf, temp;
190 struct i2c_client *new_client;
191 struct ds1621_data *data;
192 int err = 0;
193
194 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
195 | I2C_FUNC_SMBUS_WORD_DATA
196 | I2C_FUNC_SMBUS_WRITE_BYTE))
197 goto exit;
198
199 /* OK. For now, we presume we have a valid client. We now create the
200 client structure, even though we cannot fill it completely yet.
201 But it allows us to access ds1621_{read,write}_value. */
202 if (!(data = kmalloc(sizeof(struct ds1621_data), GFP_KERNEL))) {
203 err = -ENOMEM;
204 goto exit;
205 }
206 memset(data, 0, sizeof(struct ds1621_data));
207
208 new_client = &data->client;
209 i2c_set_clientdata(new_client, data);
210 new_client->addr = address;
211 new_client->adapter = adapter;
212 new_client->driver = &ds1621_driver;
213 new_client->flags = 0;
214
215
216 /* Now, we do the remaining detection. It is lousy. */
217 if (kind < 0) {
218 /* The NVB bit should be low if no EEPROM write has been
219 requested during the latest 10ms, which is highly
220 improbable in our case. */
221 conf = ds1621_read_value(new_client, DS1621_REG_CONF);
222 if (conf & DS1621_REG_CONFIG_NVB)
223 goto exit_free;
224 /* The 7 lowest bits of a temperature should always be 0. */
225 temp = ds1621_read_value(new_client, DS1621_REG_TEMP);
226 if (temp & 0x007f)
227 goto exit_free;
228 temp = ds1621_read_value(new_client, DS1621_REG_TEMP_MIN);
229 if (temp & 0x007f)
230 goto exit_free;
231 temp = ds1621_read_value(new_client, DS1621_REG_TEMP_MAX);
232 if (temp & 0x007f)
233 goto exit_free;
234 }
235
236 /* Determine the chip type - only one kind supported! */
237 if (kind <= 0)
238 kind = ds1621;
239
240 /* Fill in remaining client fields and put it into the global list */
241 strlcpy(new_client->name, "ds1621", I2C_NAME_SIZE);
242 data->valid = 0;
243 init_MUTEX(&data->update_lock);
244
245 /* Tell the I2C layer a new client has arrived */
246 if ((err = i2c_attach_client(new_client)))
247 goto exit_free;
248
249 /* Initialize the DS1621 chip */
250 ds1621_init_client(new_client);
251
252 /* Register sysfs hooks */
253 device_create_file(&new_client->dev, &dev_attr_alarms);
254 device_create_file(&new_client->dev, &dev_attr_temp1_input);
255 device_create_file(&new_client->dev, &dev_attr_temp1_min);
256 device_create_file(&new_client->dev, &dev_attr_temp1_max);
257
258 return 0;
259
260/* OK, this is not exactly good programming practice, usually. But it is
261 very code-efficient in this case. */
262 exit_free:
263 kfree(data);
264 exit:
265 return err;
266}
267
268static int ds1621_detach_client(struct i2c_client *client)
269{
270 int err;
271
272 if ((err = i2c_detach_client(client))) {
273 dev_err(&client->dev, "Client deregistration failed, "
274 "client not detached.\n");
275 return err;
276 }
277
278 kfree(i2c_get_clientdata(client));
279
280 return 0;
281}
282
283
284static struct ds1621_data *ds1621_update_client(struct device *dev)
285{
286 struct i2c_client *client = to_i2c_client(dev);
287 struct ds1621_data *data = i2c_get_clientdata(client);
288 u8 new_conf;
289
290 down(&data->update_lock);
291
292 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
293 || !data->valid) {
294
295 dev_dbg(&client->dev, "Starting ds1621 update\n");
296
297 data->conf = ds1621_read_value(client, DS1621_REG_CONF);
298
299 data->temp = ds1621_read_value(client, DS1621_REG_TEMP);
300
301 data->temp_min = ds1621_read_value(client,
302 DS1621_REG_TEMP_MIN);
303 data->temp_max = ds1621_read_value(client,
304 DS1621_REG_TEMP_MAX);
305
306 /* reset alarms if necessary */
307 new_conf = data->conf;
308 if (data->temp < data->temp_min)
309 new_conf &= ~DS1621_ALARM_TEMP_LOW;
310 if (data->temp > data->temp_max)
311 new_conf &= ~DS1621_ALARM_TEMP_HIGH;
312 if (data->conf != new_conf)
313 ds1621_write_value(client, DS1621_REG_CONF,
314 new_conf);
315
316 data->last_updated = jiffies;
317 data->valid = 1;
318 }
319
320 up(&data->update_lock);
321
322 return data;
323}
324
325static int __init ds1621_init(void)
326{
327 return i2c_add_driver(&ds1621_driver);
328}
329
330static void __exit ds1621_exit(void)
331{
332 i2c_del_driver(&ds1621_driver);
333}
334
335
336MODULE_AUTHOR("Christian W. Zuckschwerdt <zany@triq.net>");
337MODULE_DESCRIPTION("DS1621 driver");
338MODULE_LICENSE("GPL");
339
340module_init(ds1621_init);
341module_exit(ds1621_exit);
diff --git a/drivers/hwmon/fscher.c b/drivers/hwmon/fscher.c
new file mode 100644
index 000000000000..da411741c2c5
--- /dev/null
+++ b/drivers/hwmon/fscher.c
@@ -0,0 +1,691 @@
1/*
2 * fscher.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 2003, 2004 Reinhard Nissl <rnissl@gmx.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21/*
22 * fujitsu siemens hermes chip,
23 * module based on fscpos.c
24 * Copyright (C) 2000 Hermann Jung <hej@odn.de>
25 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
26 * and Philip Edelbrock <phil@netroedge.com>
27 */
28
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/slab.h>
32#include <linux/jiffies.h>
33#include <linux/i2c.h>
34#include <linux/i2c-sensor.h>
35
36/*
37 * Addresses to scan
38 */
39
40static unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
41static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
42
43/*
44 * Insmod parameters
45 */
46
47SENSORS_INSMOD_1(fscher);
48
49/*
50 * The FSCHER registers
51 */
52
53/* chip identification */
54#define FSCHER_REG_IDENT_0 0x00
55#define FSCHER_REG_IDENT_1 0x01
56#define FSCHER_REG_IDENT_2 0x02
57#define FSCHER_REG_REVISION 0x03
58
59/* global control and status */
60#define FSCHER_REG_EVENT_STATE 0x04
61#define FSCHER_REG_CONTROL 0x05
62
63/* watchdog */
64#define FSCHER_REG_WDOG_PRESET 0x28
65#define FSCHER_REG_WDOG_STATE 0x23
66#define FSCHER_REG_WDOG_CONTROL 0x21
67
68/* fan 0 */
69#define FSCHER_REG_FAN0_MIN 0x55
70#define FSCHER_REG_FAN0_ACT 0x0e
71#define FSCHER_REG_FAN0_STATE 0x0d
72#define FSCHER_REG_FAN0_RIPPLE 0x0f
73
74/* fan 1 */
75#define FSCHER_REG_FAN1_MIN 0x65
76#define FSCHER_REG_FAN1_ACT 0x6b
77#define FSCHER_REG_FAN1_STATE 0x62
78#define FSCHER_REG_FAN1_RIPPLE 0x6f
79
80/* fan 2 */
81#define FSCHER_REG_FAN2_MIN 0xb5
82#define FSCHER_REG_FAN2_ACT 0xbb
83#define FSCHER_REG_FAN2_STATE 0xb2
84#define FSCHER_REG_FAN2_RIPPLE 0xbf
85
86/* voltage supervision */
87#define FSCHER_REG_VOLT_12 0x45
88#define FSCHER_REG_VOLT_5 0x42
89#define FSCHER_REG_VOLT_BATT 0x48
90
91/* temperature 0 */
92#define FSCHER_REG_TEMP0_ACT 0x64
93#define FSCHER_REG_TEMP0_STATE 0x71
94
95/* temperature 1 */
96#define FSCHER_REG_TEMP1_ACT 0x32
97#define FSCHER_REG_TEMP1_STATE 0x81
98
99/* temperature 2 */
100#define FSCHER_REG_TEMP2_ACT 0x35
101#define FSCHER_REG_TEMP2_STATE 0x91
102
103/*
104 * Functions declaration
105 */
106
107static int fscher_attach_adapter(struct i2c_adapter *adapter);
108static int fscher_detect(struct i2c_adapter *adapter, int address, int kind);
109static int fscher_detach_client(struct i2c_client *client);
110static struct fscher_data *fscher_update_device(struct device *dev);
111static void fscher_init_client(struct i2c_client *client);
112
113static int fscher_read_value(struct i2c_client *client, u8 reg);
114static int fscher_write_value(struct i2c_client *client, u8 reg, u8 value);
115
116/*
117 * Driver data (common to all clients)
118 */
119
120static struct i2c_driver fscher_driver = {
121 .owner = THIS_MODULE,
122 .name = "fscher",
123 .id = I2C_DRIVERID_FSCHER,
124 .flags = I2C_DF_NOTIFY,
125 .attach_adapter = fscher_attach_adapter,
126 .detach_client = fscher_detach_client,
127};
128
129/*
130 * Client data (each client gets its own)
131 */
132
133struct fscher_data {
134 struct i2c_client client;
135 struct semaphore update_lock;
136 char valid; /* zero until following fields are valid */
137 unsigned long last_updated; /* in jiffies */
138
139 /* register values */
140 u8 revision; /* revision of chip */
141 u8 global_event; /* global event status */
142 u8 global_control; /* global control register */
143 u8 watchdog[3]; /* watchdog */
144 u8 volt[3]; /* 12, 5, battery voltage */
145 u8 temp_act[3]; /* temperature */
146 u8 temp_status[3]; /* status of sensor */
147 u8 fan_act[3]; /* fans revolutions per second */
148 u8 fan_status[3]; /* fan status */
149 u8 fan_min[3]; /* fan min value for rps */
150 u8 fan_ripple[3]; /* divider for rps */
151};
152
153/*
154 * Sysfs stuff
155 */
156
157#define sysfs_r(kind, sub, offset, reg) \
158static ssize_t show_##kind##sub (struct fscher_data *, char *, int); \
159static ssize_t show_##kind##offset##sub (struct device *, struct device_attribute *attr, char *); \
160static ssize_t show_##kind##offset##sub (struct device *dev, struct device_attribute *attr, char *buf) \
161{ \
162 struct fscher_data *data = fscher_update_device(dev); \
163 return show_##kind##sub(data, buf, (offset)); \
164}
165
166#define sysfs_w(kind, sub, offset, reg) \
167static ssize_t set_##kind##sub (struct i2c_client *, struct fscher_data *, const char *, size_t, int, int); \
168static ssize_t set_##kind##offset##sub (struct device *, struct device_attribute *attr, const char *, size_t); \
169static ssize_t set_##kind##offset##sub (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
170{ \
171 struct i2c_client *client = to_i2c_client(dev); \
172 struct fscher_data *data = i2c_get_clientdata(client); \
173 return set_##kind##sub(client, data, buf, count, (offset), reg); \
174}
175
176#define sysfs_rw_n(kind, sub, offset, reg) \
177sysfs_r(kind, sub, offset, reg) \
178sysfs_w(kind, sub, offset, reg) \
179static DEVICE_ATTR(kind##offset##sub, S_IRUGO | S_IWUSR, show_##kind##offset##sub, set_##kind##offset##sub);
180
181#define sysfs_rw(kind, sub, reg) \
182sysfs_r(kind, sub, 0, reg) \
183sysfs_w(kind, sub, 0, reg) \
184static DEVICE_ATTR(kind##sub, S_IRUGO | S_IWUSR, show_##kind##0##sub, set_##kind##0##sub);
185
186#define sysfs_ro_n(kind, sub, offset, reg) \
187sysfs_r(kind, sub, offset, reg) \
188static DEVICE_ATTR(kind##offset##sub, S_IRUGO, show_##kind##offset##sub, NULL);
189
190#define sysfs_ro(kind, sub, reg) \
191sysfs_r(kind, sub, 0, reg) \
192static DEVICE_ATTR(kind, S_IRUGO, show_##kind##0##sub, NULL);
193
194#define sysfs_fan(offset, reg_status, reg_min, reg_ripple, reg_act) \
195sysfs_rw_n(pwm, , offset, reg_min) \
196sysfs_rw_n(fan, _status, offset, reg_status) \
197sysfs_rw_n(fan, _div , offset, reg_ripple) \
198sysfs_ro_n(fan, _input , offset, reg_act)
199
200#define sysfs_temp(offset, reg_status, reg_act) \
201sysfs_rw_n(temp, _status, offset, reg_status) \
202sysfs_ro_n(temp, _input , offset, reg_act)
203
204#define sysfs_in(offset, reg_act) \
205sysfs_ro_n(in, _input, offset, reg_act)
206
207#define sysfs_revision(reg_revision) \
208sysfs_ro(revision, , reg_revision)
209
210#define sysfs_alarms(reg_events) \
211sysfs_ro(alarms, , reg_events)
212
213#define sysfs_control(reg_control) \
214sysfs_rw(control, , reg_control)
215
216#define sysfs_watchdog(reg_control, reg_status, reg_preset) \
217sysfs_rw(watchdog, _control, reg_control) \
218sysfs_rw(watchdog, _status , reg_status) \
219sysfs_rw(watchdog, _preset , reg_preset)
220
221sysfs_fan(1, FSCHER_REG_FAN0_STATE, FSCHER_REG_FAN0_MIN,
222 FSCHER_REG_FAN0_RIPPLE, FSCHER_REG_FAN0_ACT)
223sysfs_fan(2, FSCHER_REG_FAN1_STATE, FSCHER_REG_FAN1_MIN,
224 FSCHER_REG_FAN1_RIPPLE, FSCHER_REG_FAN1_ACT)
225sysfs_fan(3, FSCHER_REG_FAN2_STATE, FSCHER_REG_FAN2_MIN,
226 FSCHER_REG_FAN2_RIPPLE, FSCHER_REG_FAN2_ACT)
227
228sysfs_temp(1, FSCHER_REG_TEMP0_STATE, FSCHER_REG_TEMP0_ACT)
229sysfs_temp(2, FSCHER_REG_TEMP1_STATE, FSCHER_REG_TEMP1_ACT)
230sysfs_temp(3, FSCHER_REG_TEMP2_STATE, FSCHER_REG_TEMP2_ACT)
231
232sysfs_in(0, FSCHER_REG_VOLT_12)
233sysfs_in(1, FSCHER_REG_VOLT_5)
234sysfs_in(2, FSCHER_REG_VOLT_BATT)
235
236sysfs_revision(FSCHER_REG_REVISION)
237sysfs_alarms(FSCHER_REG_EVENTS)
238sysfs_control(FSCHER_REG_CONTROL)
239sysfs_watchdog(FSCHER_REG_WDOG_CONTROL, FSCHER_REG_WDOG_STATE, FSCHER_REG_WDOG_PRESET)
240
241#define device_create_file_fan(client, offset) \
242do { \
243 device_create_file(&client->dev, &dev_attr_fan##offset##_status); \
244 device_create_file(&client->dev, &dev_attr_pwm##offset); \
245 device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
246 device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
247} while (0)
248
249#define device_create_file_temp(client, offset) \
250do { \
251 device_create_file(&client->dev, &dev_attr_temp##offset##_status); \
252 device_create_file(&client->dev, &dev_attr_temp##offset##_input); \
253} while (0)
254
255#define device_create_file_in(client, offset) \
256do { \
257 device_create_file(&client->dev, &dev_attr_in##offset##_input); \
258} while (0)
259
260#define device_create_file_revision(client) \
261do { \
262 device_create_file(&client->dev, &dev_attr_revision); \
263} while (0)
264
265#define device_create_file_alarms(client) \
266do { \
267 device_create_file(&client->dev, &dev_attr_alarms); \
268} while (0)
269
270#define device_create_file_control(client) \
271do { \
272 device_create_file(&client->dev, &dev_attr_control); \
273} while (0)
274
275#define device_create_file_watchdog(client) \
276do { \
277 device_create_file(&client->dev, &dev_attr_watchdog_status); \
278 device_create_file(&client->dev, &dev_attr_watchdog_control); \
279 device_create_file(&client->dev, &dev_attr_watchdog_preset); \
280} while (0)
281
282/*
283 * Real code
284 */
285
286static int fscher_attach_adapter(struct i2c_adapter *adapter)
287{
288 if (!(adapter->class & I2C_CLASS_HWMON))
289 return 0;
290 return i2c_detect(adapter, &addr_data, fscher_detect);
291}
292
293static int fscher_detect(struct i2c_adapter *adapter, int address, int kind)
294{
295 struct i2c_client *new_client;
296 struct fscher_data *data;
297 int err = 0;
298
299 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
300 goto exit;
301
302 /* OK. For now, we presume we have a valid client. We now create the
303 * client structure, even though we cannot fill it completely yet.
304 * But it allows us to access i2c_smbus_read_byte_data. */
305 if (!(data = kmalloc(sizeof(struct fscher_data), GFP_KERNEL))) {
306 err = -ENOMEM;
307 goto exit;
308 }
309 memset(data, 0, sizeof(struct fscher_data));
310
311 /* The common I2C client data is placed right before the
312 * Hermes-specific data. */
313 new_client = &data->client;
314 i2c_set_clientdata(new_client, data);
315 new_client->addr = address;
316 new_client->adapter = adapter;
317 new_client->driver = &fscher_driver;
318 new_client->flags = 0;
319
320 /* Do the remaining detection unless force or force_fscher parameter */
321 if (kind < 0) {
322 if ((i2c_smbus_read_byte_data(new_client,
323 FSCHER_REG_IDENT_0) != 0x48) /* 'H' */
324 || (i2c_smbus_read_byte_data(new_client,
325 FSCHER_REG_IDENT_1) != 0x45) /* 'E' */
326 || (i2c_smbus_read_byte_data(new_client,
327 FSCHER_REG_IDENT_2) != 0x52)) /* 'R' */
328 goto exit_free;
329 }
330
331 /* Fill in the remaining client fields and put it into the
332 * global list */
333 strlcpy(new_client->name, "fscher", I2C_NAME_SIZE);
334 data->valid = 0;
335 init_MUTEX(&data->update_lock);
336
337 /* Tell the I2C layer a new client has arrived */
338 if ((err = i2c_attach_client(new_client)))
339 goto exit_free;
340
341 fscher_init_client(new_client);
342
343 /* Register sysfs hooks */
344 device_create_file_revision(new_client);
345 device_create_file_alarms(new_client);
346 device_create_file_control(new_client);
347 device_create_file_watchdog(new_client);
348
349 device_create_file_in(new_client, 0);
350 device_create_file_in(new_client, 1);
351 device_create_file_in(new_client, 2);
352
353 device_create_file_fan(new_client, 1);
354 device_create_file_fan(new_client, 2);
355 device_create_file_fan(new_client, 3);
356
357 device_create_file_temp(new_client, 1);
358 device_create_file_temp(new_client, 2);
359 device_create_file_temp(new_client, 3);
360
361 return 0;
362
363exit_free:
364 kfree(data);
365exit:
366 return err;
367}
368
369static int fscher_detach_client(struct i2c_client *client)
370{
371 int err;
372
373 if ((err = i2c_detach_client(client))) {
374 dev_err(&client->dev, "Client deregistration failed, "
375 "client not detached.\n");
376 return err;
377 }
378
379 kfree(i2c_get_clientdata(client));
380 return 0;
381}
382
383static int fscher_read_value(struct i2c_client *client, u8 reg)
384{
385 dev_dbg(&client->dev, "read reg 0x%02x\n", reg);
386
387 return i2c_smbus_read_byte_data(client, reg);
388}
389
390static int fscher_write_value(struct i2c_client *client, u8 reg, u8 value)
391{
392 dev_dbg(&client->dev, "write reg 0x%02x, val 0x%02x\n",
393 reg, value);
394
395 return i2c_smbus_write_byte_data(client, reg, value);
396}
397
398/* Called when we have found a new FSC Hermes. */
399static void fscher_init_client(struct i2c_client *client)
400{
401 struct fscher_data *data = i2c_get_clientdata(client);
402
403 /* Read revision from chip */
404 data->revision = fscher_read_value(client, FSCHER_REG_REVISION);
405}
406
407static struct fscher_data *fscher_update_device(struct device *dev)
408{
409 struct i2c_client *client = to_i2c_client(dev);
410 struct fscher_data *data = i2c_get_clientdata(client);
411
412 down(&data->update_lock);
413
414 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
415
416 dev_dbg(&client->dev, "Starting fscher update\n");
417
418 data->temp_act[0] = fscher_read_value(client, FSCHER_REG_TEMP0_ACT);
419 data->temp_act[1] = fscher_read_value(client, FSCHER_REG_TEMP1_ACT);
420 data->temp_act[2] = fscher_read_value(client, FSCHER_REG_TEMP2_ACT);
421 data->temp_status[0] = fscher_read_value(client, FSCHER_REG_TEMP0_STATE);
422 data->temp_status[1] = fscher_read_value(client, FSCHER_REG_TEMP1_STATE);
423 data->temp_status[2] = fscher_read_value(client, FSCHER_REG_TEMP2_STATE);
424
425 data->volt[0] = fscher_read_value(client, FSCHER_REG_VOLT_12);
426 data->volt[1] = fscher_read_value(client, FSCHER_REG_VOLT_5);
427 data->volt[2] = fscher_read_value(client, FSCHER_REG_VOLT_BATT);
428
429 data->fan_act[0] = fscher_read_value(client, FSCHER_REG_FAN0_ACT);
430 data->fan_act[1] = fscher_read_value(client, FSCHER_REG_FAN1_ACT);
431 data->fan_act[2] = fscher_read_value(client, FSCHER_REG_FAN2_ACT);
432 data->fan_status[0] = fscher_read_value(client, FSCHER_REG_FAN0_STATE);
433 data->fan_status[1] = fscher_read_value(client, FSCHER_REG_FAN1_STATE);
434 data->fan_status[2] = fscher_read_value(client, FSCHER_REG_FAN2_STATE);
435 data->fan_min[0] = fscher_read_value(client, FSCHER_REG_FAN0_MIN);
436 data->fan_min[1] = fscher_read_value(client, FSCHER_REG_FAN1_MIN);
437 data->fan_min[2] = fscher_read_value(client, FSCHER_REG_FAN2_MIN);
438 data->fan_ripple[0] = fscher_read_value(client, FSCHER_REG_FAN0_RIPPLE);
439 data->fan_ripple[1] = fscher_read_value(client, FSCHER_REG_FAN1_RIPPLE);
440 data->fan_ripple[2] = fscher_read_value(client, FSCHER_REG_FAN2_RIPPLE);
441
442 data->watchdog[0] = fscher_read_value(client, FSCHER_REG_WDOG_PRESET);
443 data->watchdog[1] = fscher_read_value(client, FSCHER_REG_WDOG_STATE);
444 data->watchdog[2] = fscher_read_value(client, FSCHER_REG_WDOG_CONTROL);
445
446 data->global_event = fscher_read_value(client, FSCHER_REG_EVENT_STATE);
447
448 data->last_updated = jiffies;
449 data->valid = 1;
450 }
451
452 up(&data->update_lock);
453
454 return data;
455}
456
457
458
459#define FAN_INDEX_FROM_NUM(nr) ((nr) - 1)
460
461static ssize_t set_fan_status(struct i2c_client *client, struct fscher_data *data,
462 const char *buf, size_t count, int nr, int reg)
463{
464 /* bits 0..1, 3..7 reserved => mask with 0x04 */
465 unsigned long v = simple_strtoul(buf, NULL, 10) & 0x04;
466
467 down(&data->update_lock);
468 data->fan_status[FAN_INDEX_FROM_NUM(nr)] &= ~v;
469 fscher_write_value(client, reg, v);
470 up(&data->update_lock);
471 return count;
472}
473
474static ssize_t show_fan_status(struct fscher_data *data, char *buf, int nr)
475{
476 /* bits 0..1, 3..7 reserved => mask with 0x04 */
477 return sprintf(buf, "%u\n", data->fan_status[FAN_INDEX_FROM_NUM(nr)] & 0x04);
478}
479
480static ssize_t set_pwm(struct i2c_client *client, struct fscher_data *data,
481 const char *buf, size_t count, int nr, int reg)
482{
483 unsigned long v = simple_strtoul(buf, NULL, 10);
484
485 down(&data->update_lock);
486 data->fan_min[FAN_INDEX_FROM_NUM(nr)] = v > 0xff ? 0xff : v;
487 fscher_write_value(client, reg, data->fan_min[FAN_INDEX_FROM_NUM(nr)]);
488 up(&data->update_lock);
489 return count;
490}
491
492static ssize_t show_pwm(struct fscher_data *data, char *buf, int nr)
493{
494 return sprintf(buf, "%u\n", data->fan_min[FAN_INDEX_FROM_NUM(nr)]);
495}
496
497static ssize_t set_fan_div(struct i2c_client *client, struct fscher_data *data,
498 const char *buf, size_t count, int nr, int reg)
499{
500 /* supported values: 2, 4, 8 */
501 unsigned long v = simple_strtoul(buf, NULL, 10);
502
503 switch (v) {
504 case 2: v = 1; break;
505 case 4: v = 2; break;
506 case 8: v = 3; break;
507 default:
508 dev_err(&client->dev, "fan_div value %ld not "
509 "supported. Choose one of 2, 4 or 8!\n", v);
510 return -EINVAL;
511 }
512
513 down(&data->update_lock);
514
515 /* bits 2..7 reserved => mask with 0x03 */
516 data->fan_ripple[FAN_INDEX_FROM_NUM(nr)] &= ~0x03;
517 data->fan_ripple[FAN_INDEX_FROM_NUM(nr)] |= v;
518
519 fscher_write_value(client, reg, data->fan_ripple[FAN_INDEX_FROM_NUM(nr)]);
520 up(&data->update_lock);
521 return count;
522}
523
524static ssize_t show_fan_div(struct fscher_data *data, char *buf, int nr)
525{
526 /* bits 2..7 reserved => mask with 0x03 */
527 return sprintf(buf, "%u\n", 1 << (data->fan_ripple[FAN_INDEX_FROM_NUM(nr)] & 0x03));
528}
529
530#define RPM_FROM_REG(val) (val*60)
531
532static ssize_t show_fan_input (struct fscher_data *data, char *buf, int nr)
533{
534 return sprintf(buf, "%u\n", RPM_FROM_REG(data->fan_act[FAN_INDEX_FROM_NUM(nr)]));
535}
536
537
538
539#define TEMP_INDEX_FROM_NUM(nr) ((nr) - 1)
540
541static ssize_t set_temp_status(struct i2c_client *client, struct fscher_data *data,
542 const char *buf, size_t count, int nr, int reg)
543{
544 /* bits 2..7 reserved, 0 read only => mask with 0x02 */
545 unsigned long v = simple_strtoul(buf, NULL, 10) & 0x02;
546
547 down(&data->update_lock);
548 data->temp_status[TEMP_INDEX_FROM_NUM(nr)] &= ~v;
549 fscher_write_value(client, reg, v);
550 up(&data->update_lock);
551 return count;
552}
553
554static ssize_t show_temp_status(struct fscher_data *data, char *buf, int nr)
555{
556 /* bits 2..7 reserved => mask with 0x03 */
557 return sprintf(buf, "%u\n", data->temp_status[TEMP_INDEX_FROM_NUM(nr)] & 0x03);
558}
559
560#define TEMP_FROM_REG(val) (((val) - 128) * 1000)
561
562static ssize_t show_temp_input(struct fscher_data *data, char *buf, int nr)
563{
564 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_act[TEMP_INDEX_FROM_NUM(nr)]));
565}
566
567/*
568 * The final conversion is specified in sensors.conf, as it depends on
569 * mainboard specific values. We export the registers contents as
570 * pseudo-hundredths-of-Volts (range 0V - 2.55V). Not that it makes much
571 * sense per se, but it minimizes the conversions count and keeps the
572 * values within a usual range.
573 */
574#define VOLT_FROM_REG(val) ((val) * 10)
575
576static ssize_t show_in_input(struct fscher_data *data, char *buf, int nr)
577{
578 return sprintf(buf, "%u\n", VOLT_FROM_REG(data->volt[nr]));
579}
580
581
582
583static ssize_t show_revision(struct fscher_data *data, char *buf, int nr)
584{
585 return sprintf(buf, "%u\n", data->revision);
586}
587
588
589
590static ssize_t show_alarms(struct fscher_data *data, char *buf, int nr)
591{
592 /* bits 2, 5..6 reserved => mask with 0x9b */
593 return sprintf(buf, "%u\n", data->global_event & 0x9b);
594}
595
596
597
598static ssize_t set_control(struct i2c_client *client, struct fscher_data *data,
599 const char *buf, size_t count, int nr, int reg)
600{
601 /* bits 1..7 reserved => mask with 0x01 */
602 unsigned long v = simple_strtoul(buf, NULL, 10) & 0x01;
603
604 down(&data->update_lock);
605 data->global_control &= ~v;
606 fscher_write_value(client, reg, v);
607 up(&data->update_lock);
608 return count;
609}
610
611static ssize_t show_control(struct fscher_data *data, char *buf, int nr)
612{
613 /* bits 1..7 reserved => mask with 0x01 */
614 return sprintf(buf, "%u\n", data->global_control & 0x01);
615}
616
617
618
619static ssize_t set_watchdog_control(struct i2c_client *client, struct
620 fscher_data *data, const char *buf, size_t count,
621 int nr, int reg)
622{
623 /* bits 0..3 reserved => mask with 0xf0 */
624 unsigned long v = simple_strtoul(buf, NULL, 10) & 0xf0;
625
626 down(&data->update_lock);
627 data->watchdog[2] &= ~0xf0;
628 data->watchdog[2] |= v;
629 fscher_write_value(client, reg, data->watchdog[2]);
630 up(&data->update_lock);
631 return count;
632}
633
634static ssize_t show_watchdog_control(struct fscher_data *data, char *buf, int nr)
635{
636 /* bits 0..3 reserved, bit 5 write only => mask with 0xd0 */
637 return sprintf(buf, "%u\n", data->watchdog[2] & 0xd0);
638}
639
640static ssize_t set_watchdog_status(struct i2c_client *client, struct fscher_data *data,
641 const char *buf, size_t count, int nr, int reg)
642{
643 /* bits 0, 2..7 reserved => mask with 0x02 */
644 unsigned long v = simple_strtoul(buf, NULL, 10) & 0x02;
645
646 down(&data->update_lock);
647 data->watchdog[1] &= ~v;
648 fscher_write_value(client, reg, v);
649 up(&data->update_lock);
650 return count;
651}
652
653static ssize_t show_watchdog_status(struct fscher_data *data, char *buf, int nr)
654{
655 /* bits 0, 2..7 reserved => mask with 0x02 */
656 return sprintf(buf, "%u\n", data->watchdog[1] & 0x02);
657}
658
659static ssize_t set_watchdog_preset(struct i2c_client *client, struct fscher_data *data,
660 const char *buf, size_t count, int nr, int reg)
661{
662 unsigned long v = simple_strtoul(buf, NULL, 10) & 0xff;
663
664 down(&data->update_lock);
665 data->watchdog[0] = v;
666 fscher_write_value(client, reg, data->watchdog[0]);
667 up(&data->update_lock);
668 return count;
669}
670
671static ssize_t show_watchdog_preset(struct fscher_data *data, char *buf, int nr)
672{
673 return sprintf(buf, "%u\n", data->watchdog[0]);
674}
675
676static int __init sensors_fscher_init(void)
677{
678 return i2c_add_driver(&fscher_driver);
679}
680
681static void __exit sensors_fscher_exit(void)
682{
683 i2c_del_driver(&fscher_driver);
684}
685
686MODULE_AUTHOR("Reinhard Nissl <rnissl@gmx.de>");
687MODULE_DESCRIPTION("FSC Hermes driver");
688MODULE_LICENSE("GPL");
689
690module_init(sensors_fscher_init);
691module_exit(sensors_fscher_exit);
diff --git a/drivers/hwmon/fscpos.c b/drivers/hwmon/fscpos.c
new file mode 100644
index 000000000000..3beaa6191ef4
--- /dev/null
+++ b/drivers/hwmon/fscpos.c
@@ -0,0 +1,641 @@
1/*
2 fscpos.c - Kernel module for hardware monitoring with FSC Poseidon chips
3 Copyright (C) 2004, 2005 Stefan Ott <stefan@desire.ch>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20/*
21 fujitsu siemens poseidon chip,
22 module based on the old fscpos module by Hermann Jung <hej@odn.de> and
23 the fscher module by Reinhard Nissl <rnissl@gmx.de>
24
25 original module based on lm80.c
26 Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
27 and Philip Edelbrock <phil@netroedge.com>
28
29 Thanks to Jean Delvare for reviewing my code and suggesting a lot of
30 improvements.
31*/
32
33#include <linux/module.h>
34#include <linux/slab.h>
35#include <linux/i2c.h>
36#include <linux/i2c-sensor.h>
37#include <linux/init.h>
38
39/*
40 * Addresses to scan
41 */
42static unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
43static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
44
45/*
46 * Insmod parameters
47 */
48SENSORS_INSMOD_1(fscpos);
49
50/*
51 * The FSCPOS registers
52 */
53
54/* chip identification */
55#define FSCPOS_REG_IDENT_0 0x00
56#define FSCPOS_REG_IDENT_1 0x01
57#define FSCPOS_REG_IDENT_2 0x02
58#define FSCPOS_REG_REVISION 0x03
59
60/* global control and status */
61#define FSCPOS_REG_EVENT_STATE 0x04
62#define FSCPOS_REG_CONTROL 0x05
63
64/* watchdog */
65#define FSCPOS_REG_WDOG_PRESET 0x28
66#define FSCPOS_REG_WDOG_STATE 0x23
67#define FSCPOS_REG_WDOG_CONTROL 0x21
68
69/* voltages */
70#define FSCPOS_REG_VOLT_12 0x45
71#define FSCPOS_REG_VOLT_5 0x42
72#define FSCPOS_REG_VOLT_BATT 0x48
73
74/* fans - the chip does not support minimum speed for fan2 */
75static u8 FSCPOS_REG_PWM[] = { 0x55, 0x65 };
76static u8 FSCPOS_REG_FAN_ACT[] = { 0x0e, 0x6b, 0xab };
77static u8 FSCPOS_REG_FAN_STATE[] = { 0x0d, 0x62, 0xa2 };
78static u8 FSCPOS_REG_FAN_RIPPLE[] = { 0x0f, 0x6f, 0xaf };
79
80/* temperatures */
81static u8 FSCPOS_REG_TEMP_ACT[] = { 0x64, 0x32, 0x35 };
82static u8 FSCPOS_REG_TEMP_STATE[] = { 0x71, 0x81, 0x91 };
83
84/*
85 * Functions declaration
86 */
87static int fscpos_attach_adapter(struct i2c_adapter *adapter);
88static int fscpos_detect(struct i2c_adapter *adapter, int address, int kind);
89static int fscpos_detach_client(struct i2c_client *client);
90
91static int fscpos_read_value(struct i2c_client *client, u8 register);
92static int fscpos_write_value(struct i2c_client *client, u8 register, u8 value);
93static struct fscpos_data *fscpos_update_device(struct device *dev);
94static void fscpos_init_client(struct i2c_client *client);
95
96static void reset_fan_alarm(struct i2c_client *client, int nr);
97
98/*
99 * Driver data (common to all clients)
100 */
101static struct i2c_driver fscpos_driver = {
102 .owner = THIS_MODULE,
103 .name = "fscpos",
104 .id = I2C_DRIVERID_FSCPOS,
105 .flags = I2C_DF_NOTIFY,
106 .attach_adapter = fscpos_attach_adapter,
107 .detach_client = fscpos_detach_client,
108};
109
110/*
111 * Client data (each client gets its own)
112 */
113struct fscpos_data {
114 struct i2c_client client;
115 struct semaphore update_lock;
116 char valid; /* 0 until following fields are valid */
117 unsigned long last_updated; /* In jiffies */
118
119 /* register values */
120 u8 revision; /* revision of chip */
121 u8 global_event; /* global event status */
122 u8 global_control; /* global control register */
123 u8 wdog_control; /* watchdog control */
124 u8 wdog_state; /* watchdog status */
125 u8 wdog_preset; /* watchdog preset */
126 u8 volt[3]; /* 12, 5, battery current */
127 u8 temp_act[3]; /* temperature */
128 u8 temp_status[3]; /* status of sensor */
129 u8 fan_act[3]; /* fans revolutions per second */
130 u8 fan_status[3]; /* fan status */
131 u8 pwm[2]; /* fan min value for rps */
132 u8 fan_ripple[3]; /* divider for rps */
133};
134
135/* Temperature */
136#define TEMP_FROM_REG(val) (((val) - 128) * 1000)
137
138static ssize_t show_temp_input(struct fscpos_data *data, char *buf, int nr)
139{
140 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_act[nr - 1]));
141}
142
143static ssize_t show_temp_status(struct fscpos_data *data, char *buf, int nr)
144{
145 /* bits 2..7 reserved => mask with 0x03 */
146 return sprintf(buf, "%u\n", data->temp_status[nr - 1] & 0x03);
147}
148
149static ssize_t show_temp_reset(struct fscpos_data *data, char *buf, int nr)
150{
151 return sprintf(buf, "1\n");
152}
153
154static ssize_t set_temp_reset(struct i2c_client *client, struct fscpos_data
155 *data, const char *buf, size_t count, int nr, int reg)
156{
157 unsigned long v = simple_strtoul(buf, NULL, 10);
158 if (v != 1) {
159 dev_err(&client->dev, "temp_reset value %ld not supported. "
160 "Use 1 to reset the alarm!\n", v);
161 return -EINVAL;
162 }
163
164 dev_info(&client->dev, "You used the temp_reset feature which has not "
165 "been proplerly tested. Please report your "
166 "experience to the module author.\n");
167
168 /* Supported value: 2 (clears the status) */
169 fscpos_write_value(client, FSCPOS_REG_TEMP_STATE[nr], 2);
170 return count;
171}
172
173/* Fans */
174#define RPM_FROM_REG(val) ((val) * 60)
175
176static ssize_t show_fan_status(struct fscpos_data *data, char *buf, int nr)
177{
178 /* bits 0..1, 3..7 reserved => mask with 0x04 */
179 return sprintf(buf, "%u\n", data->fan_status[nr - 1] & 0x04);
180}
181
182static ssize_t show_fan_input(struct fscpos_data *data, char *buf, int nr)
183{
184 return sprintf(buf, "%u\n", RPM_FROM_REG(data->fan_act[nr - 1]));
185}
186
187static ssize_t show_fan_ripple(struct fscpos_data *data, char *buf, int nr)
188{
189 /* bits 2..7 reserved => mask with 0x03 */
190 return sprintf(buf, "%u\n", data->fan_ripple[nr - 1] & 0x03);
191}
192
193static ssize_t set_fan_ripple(struct i2c_client *client, struct fscpos_data
194 *data, const char *buf, size_t count, int nr, int reg)
195{
196 /* supported values: 2, 4, 8 */
197 unsigned long v = simple_strtoul(buf, NULL, 10);
198
199 switch (v) {
200 case 2: v = 1; break;
201 case 4: v = 2; break;
202 case 8: v = 3; break;
203 default:
204 dev_err(&client->dev, "fan_ripple value %ld not supported. "
205 "Must be one of 2, 4 or 8!\n", v);
206 return -EINVAL;
207 }
208
209 down(&data->update_lock);
210 /* bits 2..7 reserved => mask with 0x03 */
211 data->fan_ripple[nr - 1] &= ~0x03;
212 data->fan_ripple[nr - 1] |= v;
213
214 fscpos_write_value(client, reg, data->fan_ripple[nr - 1]);
215 up(&data->update_lock);
216 return count;
217}
218
219static ssize_t show_pwm(struct fscpos_data *data, char *buf, int nr)
220{
221 return sprintf(buf, "%u\n", data->pwm[nr - 1]);
222}
223
224static ssize_t set_pwm(struct i2c_client *client, struct fscpos_data *data,
225 const char *buf, size_t count, int nr, int reg)
226{
227 unsigned long v = simple_strtoul(buf, NULL, 10);
228
229 /* Range: 0..255 */
230 if (v < 0) v = 0;
231 if (v > 255) v = 255;
232
233 down(&data->update_lock);
234 data->pwm[nr - 1] = v;
235 fscpos_write_value(client, reg, data->pwm[nr - 1]);
236 up(&data->update_lock);
237 return count;
238}
239
240static void reset_fan_alarm(struct i2c_client *client, int nr)
241{
242 fscpos_write_value(client, FSCPOS_REG_FAN_STATE[nr], 4);
243}
244
245/* Volts */
246#define VOLT_FROM_REG(val, mult) ((val) * (mult) / 255)
247
248static ssize_t show_volt_12(struct device *dev, struct device_attribute *attr, char *buf)
249{
250 struct fscpos_data *data = fscpos_update_device(dev);
251 return sprintf(buf, "%u\n", VOLT_FROM_REG(data->volt[0], 14200));
252}
253
254static ssize_t show_volt_5(struct device *dev, struct device_attribute *attr, char *buf)
255{
256 struct fscpos_data *data = fscpos_update_device(dev);
257 return sprintf(buf, "%u\n", VOLT_FROM_REG(data->volt[1], 6600));
258}
259
260static ssize_t show_volt_batt(struct device *dev, struct device_attribute *attr, char *buf)
261{
262 struct fscpos_data *data = fscpos_update_device(dev);
263 return sprintf(buf, "%u\n", VOLT_FROM_REG(data->volt[2], 3300));
264}
265
266/* Watchdog */
267static ssize_t show_wdog_control(struct fscpos_data *data, char *buf)
268{
269 /* bits 0..3 reserved, bit 6 write only => mask with 0xb0 */
270 return sprintf(buf, "%u\n", data->wdog_control & 0xb0);
271}
272
273static ssize_t set_wdog_control(struct i2c_client *client, struct fscpos_data
274 *data, const char *buf, size_t count, int reg)
275{
276 /* bits 0..3 reserved => mask with 0xf0 */
277 unsigned long v = simple_strtoul(buf, NULL, 10) & 0xf0;
278
279 down(&data->update_lock);
280 data->wdog_control &= ~0xf0;
281 data->wdog_control |= v;
282 fscpos_write_value(client, reg, data->wdog_control);
283 up(&data->update_lock);
284 return count;
285}
286
287static ssize_t show_wdog_state(struct fscpos_data *data, char *buf)
288{
289 /* bits 0, 2..7 reserved => mask with 0x02 */
290 return sprintf(buf, "%u\n", data->wdog_state & 0x02);
291}
292
293static ssize_t set_wdog_state(struct i2c_client *client, struct fscpos_data
294 *data, const char *buf, size_t count, int reg)
295{
296 unsigned long v = simple_strtoul(buf, NULL, 10) & 0x02;
297
298 /* Valid values: 2 (clear) */
299 if (v != 2) {
300 dev_err(&client->dev, "wdog_state value %ld not supported. "
301 "Must be 2 to clear the state!\n", v);
302 return -EINVAL;
303 }
304
305 down(&data->update_lock);
306 data->wdog_state &= ~v;
307 fscpos_write_value(client, reg, v);
308 up(&data->update_lock);
309 return count;
310}
311
312static ssize_t show_wdog_preset(struct fscpos_data *data, char *buf)
313{
314 return sprintf(buf, "%u\n", data->wdog_preset);
315}
316
317static ssize_t set_wdog_preset(struct i2c_client *client, struct fscpos_data
318 *data, const char *buf, size_t count, int reg)
319{
320 unsigned long v = simple_strtoul(buf, NULL, 10) & 0xff;
321
322 down(&data->update_lock);
323 data->wdog_preset = v;
324 fscpos_write_value(client, reg, data->wdog_preset);
325 up(&data->update_lock);
326 return count;
327}
328
329/* Event */
330static ssize_t show_event(struct device *dev, struct device_attribute *attr, char *buf)
331{
332 /* bits 5..7 reserved => mask with 0x1f */
333 struct fscpos_data *data = fscpos_update_device(dev);
334 return sprintf(buf, "%u\n", data->global_event & 0x9b);
335}
336
337/*
338 * Sysfs stuff
339 */
340#define create_getter(kind, sub) \
341 static ssize_t sysfs_show_##kind##sub(struct device *dev, struct device_attribute *attr, char *buf) \
342 { \
343 struct fscpos_data *data = fscpos_update_device(dev); \
344 return show_##kind##sub(data, buf); \
345 }
346
347#define create_getter_n(kind, offset, sub) \
348 static ssize_t sysfs_show_##kind##offset##sub(struct device *dev, struct device_attribute *attr, char\
349 *buf) \
350 { \
351 struct fscpos_data *data = fscpos_update_device(dev); \
352 return show_##kind##sub(data, buf, offset); \
353 }
354
355#define create_setter(kind, sub, reg) \
356 static ssize_t sysfs_set_##kind##sub (struct device *dev, struct device_attribute *attr, const char \
357 *buf, size_t count) \
358 { \
359 struct i2c_client *client = to_i2c_client(dev); \
360 struct fscpos_data *data = i2c_get_clientdata(client); \
361 return set_##kind##sub(client, data, buf, count, reg); \
362 }
363
364#define create_setter_n(kind, offset, sub, reg) \
365 static ssize_t sysfs_set_##kind##offset##sub (struct device *dev, struct device_attribute *attr, \
366 const char *buf, size_t count) \
367 { \
368 struct i2c_client *client = to_i2c_client(dev); \
369 struct fscpos_data *data = i2c_get_clientdata(client); \
370 return set_##kind##sub(client, data, buf, count, offset, reg);\
371 }
372
373#define create_sysfs_device_ro(kind, sub, offset) \
374 static DEVICE_ATTR(kind##offset##sub, S_IRUGO, \
375 sysfs_show_##kind##offset##sub, NULL);
376
377#define create_sysfs_device_rw(kind, sub, offset) \
378 static DEVICE_ATTR(kind##offset##sub, S_IRUGO | S_IWUSR, \
379 sysfs_show_##kind##offset##sub, sysfs_set_##kind##offset##sub);
380
381#define sysfs_ro_n(kind, sub, offset) \
382 create_getter_n(kind, offset, sub); \
383 create_sysfs_device_ro(kind, sub, offset);
384
385#define sysfs_rw_n(kind, sub, offset, reg) \
386 create_getter_n(kind, offset, sub); \
387 create_setter_n(kind, offset, sub, reg); \
388 create_sysfs_device_rw(kind, sub, offset);
389
390#define sysfs_rw(kind, sub, reg) \
391 create_getter(kind, sub); \
392 create_setter(kind, sub, reg); \
393 create_sysfs_device_rw(kind, sub,);
394
395#define sysfs_fan_with_min(offset, reg_status, reg_ripple, reg_min) \
396 sysfs_fan(offset, reg_status, reg_ripple); \
397 sysfs_rw_n(pwm,, offset, reg_min);
398
399#define sysfs_fan(offset, reg_status, reg_ripple) \
400 sysfs_ro_n(fan, _input, offset); \
401 sysfs_ro_n(fan, _status, offset); \
402 sysfs_rw_n(fan, _ripple, offset, reg_ripple);
403
404#define sysfs_temp(offset, reg_status) \
405 sysfs_ro_n(temp, _input, offset); \
406 sysfs_ro_n(temp, _status, offset); \
407 sysfs_rw_n(temp, _reset, offset, reg_status);
408
409#define sysfs_watchdog(reg_wdog_preset, reg_wdog_state, reg_wdog_control) \
410 sysfs_rw(wdog, _control, reg_wdog_control); \
411 sysfs_rw(wdog, _preset, reg_wdog_preset); \
412 sysfs_rw(wdog, _state, reg_wdog_state);
413
414sysfs_fan_with_min(1, FSCPOS_REG_FAN_STATE[0], FSCPOS_REG_FAN_RIPPLE[0],
415 FSCPOS_REG_PWM[0]);
416sysfs_fan_with_min(2, FSCPOS_REG_FAN_STATE[1], FSCPOS_REG_FAN_RIPPLE[1],
417 FSCPOS_REG_PWM[1]);
418sysfs_fan(3, FSCPOS_REG_FAN_STATE[2], FSCPOS_REG_FAN_RIPPLE[2]);
419
420sysfs_temp(1, FSCPOS_REG_TEMP_STATE[0]);
421sysfs_temp(2, FSCPOS_REG_TEMP_STATE[1]);
422sysfs_temp(3, FSCPOS_REG_TEMP_STATE[2]);
423
424sysfs_watchdog(FSCPOS_REG_WDOG_PRESET, FSCPOS_REG_WDOG_STATE,
425 FSCPOS_REG_WDOG_CONTROL);
426
427static DEVICE_ATTR(event, S_IRUGO, show_event, NULL);
428static DEVICE_ATTR(in0_input, S_IRUGO, show_volt_12, NULL);
429static DEVICE_ATTR(in1_input, S_IRUGO, show_volt_5, NULL);
430static DEVICE_ATTR(in2_input, S_IRUGO, show_volt_batt, NULL);
431
432static int fscpos_attach_adapter(struct i2c_adapter *adapter)
433{
434 if (!(adapter->class & I2C_CLASS_HWMON))
435 return 0;
436 return i2c_detect(adapter, &addr_data, fscpos_detect);
437}
438
439int fscpos_detect(struct i2c_adapter *adapter, int address, int kind)
440{
441 struct i2c_client *new_client;
442 struct fscpos_data *data;
443 int err = 0;
444
445 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
446 goto exit;
447
448 /*
449 * OK. For now, we presume we have a valid client. We now create the
450 * client structure, even though we cannot fill it completely yet.
451 * But it allows us to access fscpos_{read,write}_value.
452 */
453
454 if (!(data = kmalloc(sizeof(struct fscpos_data), GFP_KERNEL))) {
455 err = -ENOMEM;
456 goto exit;
457 }
458 memset(data, 0, sizeof(struct fscpos_data));
459
460 new_client = &data->client;
461 i2c_set_clientdata(new_client, data);
462 new_client->addr = address;
463 new_client->adapter = adapter;
464 new_client->driver = &fscpos_driver;
465 new_client->flags = 0;
466
467 /* Do the remaining detection unless force or force_fscpos parameter */
468 if (kind < 0) {
469 if ((fscpos_read_value(new_client, FSCPOS_REG_IDENT_0)
470 != 0x50) /* 'P' */
471 || (fscpos_read_value(new_client, FSCPOS_REG_IDENT_1)
472 != 0x45) /* 'E' */
473 || (fscpos_read_value(new_client, FSCPOS_REG_IDENT_2)
474 != 0x47))/* 'G' */
475 {
476 dev_dbg(&new_client->dev, "fscpos detection failed\n");
477 goto exit_free;
478 }
479 }
480
481 /* Fill in the remaining client fields and put it in the global list */
482 strlcpy(new_client->name, "fscpos", I2C_NAME_SIZE);
483
484 data->valid = 0;
485 init_MUTEX(&data->update_lock);
486
487 /* Tell the I2C layer a new client has arrived */
488 if ((err = i2c_attach_client(new_client)))
489 goto exit_free;
490
491 /* Inizialize the fscpos chip */
492 fscpos_init_client(new_client);
493
494 /* Announce that the chip was found */
495 dev_info(&new_client->dev, "Found fscpos chip, rev %u\n", data->revision);
496
497 /* Register sysfs hooks */
498 device_create_file(&new_client->dev, &dev_attr_event);
499 device_create_file(&new_client->dev, &dev_attr_in0_input);
500 device_create_file(&new_client->dev, &dev_attr_in1_input);
501 device_create_file(&new_client->dev, &dev_attr_in2_input);
502 device_create_file(&new_client->dev, &dev_attr_wdog_control);
503 device_create_file(&new_client->dev, &dev_attr_wdog_preset);
504 device_create_file(&new_client->dev, &dev_attr_wdog_state);
505 device_create_file(&new_client->dev, &dev_attr_temp1_input);
506 device_create_file(&new_client->dev, &dev_attr_temp1_status);
507 device_create_file(&new_client->dev, &dev_attr_temp1_reset);
508 device_create_file(&new_client->dev, &dev_attr_temp2_input);
509 device_create_file(&new_client->dev, &dev_attr_temp2_status);
510 device_create_file(&new_client->dev, &dev_attr_temp2_reset);
511 device_create_file(&new_client->dev, &dev_attr_temp3_input);
512 device_create_file(&new_client->dev, &dev_attr_temp3_status);
513 device_create_file(&new_client->dev, &dev_attr_temp3_reset);
514 device_create_file(&new_client->dev, &dev_attr_fan1_input);
515 device_create_file(&new_client->dev, &dev_attr_fan1_status);
516 device_create_file(&new_client->dev, &dev_attr_fan1_ripple);
517 device_create_file(&new_client->dev, &dev_attr_pwm1);
518 device_create_file(&new_client->dev, &dev_attr_fan2_input);
519 device_create_file(&new_client->dev, &dev_attr_fan2_status);
520 device_create_file(&new_client->dev, &dev_attr_fan2_ripple);
521 device_create_file(&new_client->dev, &dev_attr_pwm2);
522 device_create_file(&new_client->dev, &dev_attr_fan3_input);
523 device_create_file(&new_client->dev, &dev_attr_fan3_status);
524 device_create_file(&new_client->dev, &dev_attr_fan3_ripple);
525
526 return 0;
527
528exit_free:
529 kfree(data);
530exit:
531 return err;
532}
533
534static int fscpos_detach_client(struct i2c_client *client)
535{
536 int err;
537
538 if ((err = i2c_detach_client(client))) {
539 dev_err(&client->dev, "Client deregistration failed, client"
540 " not detached.\n");
541 return err;
542 }
543 kfree(i2c_get_clientdata(client));
544 return 0;
545}
546
547static int fscpos_read_value(struct i2c_client *client, u8 reg)
548{
549 dev_dbg(&client->dev, "Read reg 0x%02x\n", reg);
550 return i2c_smbus_read_byte_data(client, reg);
551}
552
553static int fscpos_write_value(struct i2c_client *client, u8 reg, u8 value)
554{
555 dev_dbg(&client->dev, "Write reg 0x%02x, val 0x%02x\n", reg, value);
556 return i2c_smbus_write_byte_data(client, reg, value);
557}
558
559/* Called when we have found a new FSCPOS chip */
560static void fscpos_init_client(struct i2c_client *client)
561{
562 struct fscpos_data *data = i2c_get_clientdata(client);
563
564 /* read revision from chip */
565 data->revision = fscpos_read_value(client, FSCPOS_REG_REVISION);
566}
567
568static struct fscpos_data *fscpos_update_device(struct device *dev)
569{
570 struct i2c_client *client = to_i2c_client(dev);
571 struct fscpos_data *data = i2c_get_clientdata(client);
572
573 down(&data->update_lock);
574
575 if ((jiffies - data->last_updated > 2 * HZ) ||
576 (jiffies < data->last_updated) || !data->valid) {
577 int i;
578
579 dev_dbg(&client->dev, "Starting fscpos update\n");
580
581 for (i = 0; i < 3; i++) {
582 data->temp_act[i] = fscpos_read_value(client,
583 FSCPOS_REG_TEMP_ACT[i]);
584 data->temp_status[i] = fscpos_read_value(client,
585 FSCPOS_REG_TEMP_STATE[i]);
586 data->fan_act[i] = fscpos_read_value(client,
587 FSCPOS_REG_FAN_ACT[i]);
588 data->fan_status[i] = fscpos_read_value(client,
589 FSCPOS_REG_FAN_STATE[i]);
590 data->fan_ripple[i] = fscpos_read_value(client,
591 FSCPOS_REG_FAN_RIPPLE[i]);
592 if (i < 2) {
593 /* fan2_min is not supported by the chip */
594 data->pwm[i] = fscpos_read_value(client,
595 FSCPOS_REG_PWM[i]);
596 }
597 /* reset fan status if speed is back to > 0 */
598 if (data->fan_status[i] != 0 && data->fan_act[i] > 0) {
599 reset_fan_alarm(client, i);
600 }
601 }
602
603 data->volt[0] = fscpos_read_value(client, FSCPOS_REG_VOLT_12);
604 data->volt[1] = fscpos_read_value(client, FSCPOS_REG_VOLT_5);
605 data->volt[2] = fscpos_read_value(client, FSCPOS_REG_VOLT_BATT);
606
607 data->wdog_preset = fscpos_read_value(client,
608 FSCPOS_REG_WDOG_PRESET);
609 data->wdog_state = fscpos_read_value(client,
610 FSCPOS_REG_WDOG_STATE);
611 data->wdog_control = fscpos_read_value(client,
612 FSCPOS_REG_WDOG_CONTROL);
613
614 data->global_event = fscpos_read_value(client,
615 FSCPOS_REG_EVENT_STATE);
616
617 data->last_updated = jiffies;
618 data->valid = 1;
619 }
620 up(&data->update_lock);
621 return data;
622}
623
624static int __init sm_fscpos_init(void)
625{
626 return i2c_add_driver(&fscpos_driver);
627}
628
629static void __exit sm_fscpos_exit(void)
630{
631 i2c_del_driver(&fscpos_driver);
632}
633
634MODULE_AUTHOR("Stefan Ott <stefan@desire.ch> based on work from Hermann Jung "
635 "<hej@odn.de>, Frodo Looijaard <frodol@dds.nl>"
636 " and Philip Edelbrock <phil@netroedge.com>");
637MODULE_DESCRIPTION("fujitsu siemens poseidon chip driver");
638MODULE_LICENSE("GPL");
639
640module_init(sm_fscpos_init);
641module_exit(sm_fscpos_exit);
diff --git a/drivers/hwmon/gl518sm.c b/drivers/hwmon/gl518sm.c
new file mode 100644
index 000000000000..6bedf729dcf5
--- /dev/null
+++ b/drivers/hwmon/gl518sm.c
@@ -0,0 +1,604 @@
1/*
2 * gl518sm.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
5 * Kyosti Malkki <kmalkki@cc.hut.fi>
6 * Copyright (C) 2004 Hong-Gunn Chew <hglinux@gunnet.org> and
7 * 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 *
23 * Ported to Linux 2.6 by Hong-Gunn Chew with the help of Jean Delvare
24 * and advice of Greg Kroah-Hartman.
25 *
26 * Notes about the port:
27 * Release 0x00 of the GL518SM chipset doesn't support reading of in0,
28 * in1 nor in2. The original driver had an ugly workaround to get them
29 * anyway (changing limits and watching alarms trigger and wear off).
30 * We did not keep that part of the original driver in the Linux 2.6
31 * version, since it was making the driver significantly more complex
32 * with no real benefit.
33 *
34 * History:
35 * 2004-01-28 Original port. (Hong-Gunn Chew)
36 * 2004-01-31 Code review and approval. (Jean Delvare)
37 */
38
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/slab.h>
42#include <linux/jiffies.h>
43#include <linux/i2c.h>
44#include <linux/i2c-sensor.h>
45
46/* Addresses to scan */
47static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
48static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
49
50/* Insmod parameters */
51SENSORS_INSMOD_2(gl518sm_r00, gl518sm_r80);
52
53/* Many GL518 constants specified below */
54
55/* The GL518 registers */
56#define GL518_REG_CHIP_ID 0x00
57#define GL518_REG_REVISION 0x01
58#define GL518_REG_VENDOR_ID 0x02
59#define GL518_REG_CONF 0x03
60#define GL518_REG_TEMP_IN 0x04
61#define GL518_REG_TEMP_MAX 0x05
62#define GL518_REG_TEMP_HYST 0x06
63#define GL518_REG_FAN_COUNT 0x07
64#define GL518_REG_FAN_LIMIT 0x08
65#define GL518_REG_VIN1_LIMIT 0x09
66#define GL518_REG_VIN2_LIMIT 0x0a
67#define GL518_REG_VIN3_LIMIT 0x0b
68#define GL518_REG_VDD_LIMIT 0x0c
69#define GL518_REG_VIN3 0x0d
70#define GL518_REG_MISC 0x0f
71#define GL518_REG_ALARM 0x10
72#define GL518_REG_MASK 0x11
73#define GL518_REG_INT 0x12
74#define GL518_REG_VIN2 0x13
75#define GL518_REG_VIN1 0x14
76#define GL518_REG_VDD 0x15
77
78
79/*
80 * Conversions. Rounding and limit checking is only done on the TO_REG
81 * variants. Note that you should be a bit careful with which arguments
82 * these macros are called: arguments may be evaluated more than once.
83 * Fixing this is just not worth it.
84 */
85
86#define RAW_FROM_REG(val) val
87
88#define BOOL_FROM_REG(val) ((val)?0:1)
89#define BOOL_TO_REG(val) ((val)?0:1)
90
91#define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0? \
92 (val)-500:(val)+500)/1000)+119),0,255))
93#define TEMP_FROM_REG(val) (((val) - 119) * 1000)
94
95static inline u8 FAN_TO_REG(long rpm, int div)
96{
97 long rpmdiv;
98 if (rpm == 0)
99 return 0;
100 rpmdiv = SENSORS_LIMIT(rpm, 1, 1920000) * div;
101 return SENSORS_LIMIT((960000 + rpmdiv / 2) / rpmdiv, 1, 255);
102}
103#define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (960000/((val)*(div))))
104
105#define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
106#define IN_FROM_REG(val) ((val)*19)
107
108#define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
109#define VDD_FROM_REG(val) (((val)*95+2)/4)
110
111#define DIV_TO_REG(val) ((val)==4?2:(val)==2?1:(val)==1?0:3)
112#define DIV_FROM_REG(val) (1 << (val))
113
114#define BEEP_MASK_TO_REG(val) ((val) & 0x7f & data->alarm_mask)
115#define BEEP_MASK_FROM_REG(val) ((val) & 0x7f)
116
117/* Each client has this additional data */
118struct gl518_data {
119 struct i2c_client client;
120 enum chips type;
121
122 struct semaphore update_lock;
123 char valid; /* !=0 if following fields are valid */
124 unsigned long last_updated; /* In jiffies */
125
126 u8 voltage_in[4]; /* Register values; [0] = VDD */
127 u8 voltage_min[4]; /* Register values; [0] = VDD */
128 u8 voltage_max[4]; /* Register values; [0] = VDD */
129 u8 iter_voltage_in[4]; /* Register values; [0] = VDD */
130 u8 fan_in[2];
131 u8 fan_min[2];
132 u8 fan_div[2]; /* Register encoding, shifted right */
133 u8 fan_auto1; /* Boolean */
134 u8 temp_in; /* Register values */
135 u8 temp_max; /* Register values */
136 u8 temp_hyst; /* Register values */
137 u8 alarms; /* Register value */
138 u8 alarm_mask; /* Register value */
139 u8 beep_mask; /* Register value */
140 u8 beep_enable; /* Boolean */
141};
142
143static int gl518_attach_adapter(struct i2c_adapter *adapter);
144static int gl518_detect(struct i2c_adapter *adapter, int address, int kind);
145static void gl518_init_client(struct i2c_client *client);
146static int gl518_detach_client(struct i2c_client *client);
147static int gl518_read_value(struct i2c_client *client, u8 reg);
148static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value);
149static struct gl518_data *gl518_update_device(struct device *dev);
150
151/* This is the driver that will be inserted */
152static struct i2c_driver gl518_driver = {
153 .owner = THIS_MODULE,
154 .name = "gl518sm",
155 .id = I2C_DRIVERID_GL518,
156 .flags = I2C_DF_NOTIFY,
157 .attach_adapter = gl518_attach_adapter,
158 .detach_client = gl518_detach_client,
159};
160
161/*
162 * Sysfs stuff
163 */
164
165#define show(type, suffix, value) \
166static ssize_t show_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
167{ \
168 struct gl518_data *data = gl518_update_device(dev); \
169 return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \
170}
171
172#define show_fan(suffix, value, index) \
173static ssize_t show_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
174{ \
175 struct gl518_data *data = gl518_update_device(dev); \
176 return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[index], \
177 DIV_FROM_REG(data->fan_div[index]))); \
178}
179
180show(TEMP, temp_input1, temp_in);
181show(TEMP, temp_max1, temp_max);
182show(TEMP, temp_hyst1, temp_hyst);
183show(BOOL, fan_auto1, fan_auto1);
184show_fan(fan_input1, fan_in, 0);
185show_fan(fan_input2, fan_in, 1);
186show_fan(fan_min1, fan_min, 0);
187show_fan(fan_min2, fan_min, 1);
188show(DIV, fan_div1, fan_div[0]);
189show(DIV, fan_div2, fan_div[1]);
190show(VDD, in_input0, voltage_in[0]);
191show(IN, in_input1, voltage_in[1]);
192show(IN, in_input2, voltage_in[2]);
193show(IN, in_input3, voltage_in[3]);
194show(VDD, in_min0, voltage_min[0]);
195show(IN, in_min1, voltage_min[1]);
196show(IN, in_min2, voltage_min[2]);
197show(IN, in_min3, voltage_min[3]);
198show(VDD, in_max0, voltage_max[0]);
199show(IN, in_max1, voltage_max[1]);
200show(IN, in_max2, voltage_max[2]);
201show(IN, in_max3, voltage_max[3]);
202show(RAW, alarms, alarms);
203show(BOOL, beep_enable, beep_enable);
204show(BEEP_MASK, beep_mask, beep_mask);
205
206#define set(type, suffix, value, reg) \
207static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
208 size_t count) \
209{ \
210 struct i2c_client *client = to_i2c_client(dev); \
211 struct gl518_data *data = i2c_get_clientdata(client); \
212 long val = simple_strtol(buf, NULL, 10); \
213 \
214 down(&data->update_lock); \
215 data->value = type##_TO_REG(val); \
216 gl518_write_value(client, reg, data->value); \
217 up(&data->update_lock); \
218 return count; \
219}
220
221#define set_bits(type, suffix, value, reg, mask, shift) \
222static ssize_t set_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
223 size_t count) \
224{ \
225 struct i2c_client *client = to_i2c_client(dev); \
226 struct gl518_data *data = i2c_get_clientdata(client); \
227 int regvalue; \
228 unsigned long val = simple_strtoul(buf, NULL, 10); \
229 \
230 down(&data->update_lock); \
231 regvalue = gl518_read_value(client, reg); \
232 data->value = type##_TO_REG(val); \
233 regvalue = (regvalue & ~mask) | (data->value << shift); \
234 gl518_write_value(client, reg, regvalue); \
235 up(&data->update_lock); \
236 return count; \
237}
238
239#define set_low(type, suffix, value, reg) \
240 set_bits(type, suffix, value, reg, 0x00ff, 0)
241#define set_high(type, suffix, value, reg) \
242 set_bits(type, suffix, value, reg, 0xff00, 8)
243
244set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX);
245set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST);
246set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3);
247set_bits(DIV, fan_div1, fan_div[0], GL518_REG_MISC, 0xc0, 6);
248set_bits(DIV, fan_div2, fan_div[1], GL518_REG_MISC, 0x30, 4);
249set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT);
250set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT);
251set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT);
252set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT);
253set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT);
254set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT);
255set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT);
256set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT);
257set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2);
258set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM);
259
260static ssize_t set_fan_min1(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
261{
262 struct i2c_client *client = to_i2c_client(dev);
263 struct gl518_data *data = i2c_get_clientdata(client);
264 int regvalue;
265 unsigned long val = simple_strtoul(buf, NULL, 10);
266
267 down(&data->update_lock);
268 regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
269 data->fan_min[0] = FAN_TO_REG(val,
270 DIV_FROM_REG(data->fan_div[0]));
271 regvalue = (regvalue & 0x00ff) | (data->fan_min[0] << 8);
272 gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
273
274 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
275 if (data->fan_min[0] == 0)
276 data->alarm_mask &= ~0x20;
277 else
278 data->alarm_mask |= 0x20;
279 data->beep_mask &= data->alarm_mask;
280 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
281
282 up(&data->update_lock);
283 return count;
284}
285
286static ssize_t set_fan_min2(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
287{
288 struct i2c_client *client = to_i2c_client(dev);
289 struct gl518_data *data = i2c_get_clientdata(client);
290 int regvalue;
291 unsigned long val = simple_strtoul(buf, NULL, 10);
292
293 down(&data->update_lock);
294 regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
295 data->fan_min[1] = FAN_TO_REG(val,
296 DIV_FROM_REG(data->fan_div[1]));
297 regvalue = (regvalue & 0xff00) | data->fan_min[1];
298 gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
299
300 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
301 if (data->fan_min[1] == 0)
302 data->alarm_mask &= ~0x40;
303 else
304 data->alarm_mask |= 0x40;
305 data->beep_mask &= data->alarm_mask;
306 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
307
308 up(&data->update_lock);
309 return count;
310}
311
312static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
313static DEVICE_ATTR(temp1_max, S_IWUSR|S_IRUGO, show_temp_max1, set_temp_max1);
314static DEVICE_ATTR(temp1_max_hyst, S_IWUSR|S_IRUGO,
315 show_temp_hyst1, set_temp_hyst1);
316static DEVICE_ATTR(fan1_auto, S_IWUSR|S_IRUGO, show_fan_auto1, set_fan_auto1);
317static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input1, NULL);
318static DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input2, NULL);
319static DEVICE_ATTR(fan1_min, S_IWUSR|S_IRUGO, show_fan_min1, set_fan_min1);
320static DEVICE_ATTR(fan2_min, S_IWUSR|S_IRUGO, show_fan_min2, set_fan_min2);
321static DEVICE_ATTR(fan1_div, S_IWUSR|S_IRUGO, show_fan_div1, set_fan_div1);
322static DEVICE_ATTR(fan2_div, S_IWUSR|S_IRUGO, show_fan_div2, set_fan_div2);
323static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
324static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
325static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
326static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
327static DEVICE_ATTR(in0_min, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0);
328static DEVICE_ATTR(in1_min, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1);
329static DEVICE_ATTR(in2_min, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2);
330static DEVICE_ATTR(in3_min, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3);
331static DEVICE_ATTR(in0_max, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0);
332static DEVICE_ATTR(in1_max, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1);
333static DEVICE_ATTR(in2_max, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2);
334static DEVICE_ATTR(in3_max, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3);
335static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
336static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO,
337 show_beep_enable, set_beep_enable);
338static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO,
339 show_beep_mask, set_beep_mask);
340
341/*
342 * Real code
343 */
344
345static int gl518_attach_adapter(struct i2c_adapter *adapter)
346{
347 if (!(adapter->class & I2C_CLASS_HWMON))
348 return 0;
349 return i2c_detect(adapter, &addr_data, gl518_detect);
350}
351
352static int gl518_detect(struct i2c_adapter *adapter, int address, int kind)
353{
354 int i;
355 struct i2c_client *new_client;
356 struct gl518_data *data;
357 int err = 0;
358
359 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
360 I2C_FUNC_SMBUS_WORD_DATA))
361 goto exit;
362
363 /* OK. For now, we presume we have a valid client. We now create the
364 client structure, even though we cannot fill it completely yet.
365 But it allows us to access gl518_{read,write}_value. */
366
367 if (!(data = kmalloc(sizeof(struct gl518_data), GFP_KERNEL))) {
368 err = -ENOMEM;
369 goto exit;
370 }
371 memset(data, 0, sizeof(struct gl518_data));
372
373 new_client = &data->client;
374 i2c_set_clientdata(new_client, data);
375
376 new_client->addr = address;
377 new_client->adapter = adapter;
378 new_client->driver = &gl518_driver;
379 new_client->flags = 0;
380
381 /* Now, we do the remaining detection. */
382
383 if (kind < 0) {
384 if ((gl518_read_value(new_client, GL518_REG_CHIP_ID) != 0x80)
385 || (gl518_read_value(new_client, GL518_REG_CONF) & 0x80))
386 goto exit_free;
387 }
388
389 /* Determine the chip type. */
390 if (kind <= 0) {
391 i = gl518_read_value(new_client, GL518_REG_REVISION);
392 if (i == 0x00) {
393 kind = gl518sm_r00;
394 } else if (i == 0x80) {
395 kind = gl518sm_r80;
396 } else {
397 if (kind <= 0)
398 dev_info(&adapter->dev,
399 "Ignoring 'force' parameter for unknown "
400 "chip at adapter %d, address 0x%02x\n",
401 i2c_adapter_id(adapter), address);
402 goto exit_free;
403 }
404 }
405
406 /* Fill in the remaining client fields */
407 strlcpy(new_client->name, "gl518sm", I2C_NAME_SIZE);
408 data->type = kind;
409 data->valid = 0;
410 init_MUTEX(&data->update_lock);
411
412 /* Tell the I2C layer a new client has arrived */
413 if ((err = i2c_attach_client(new_client)))
414 goto exit_free;
415
416 /* Initialize the GL518SM chip */
417 data->alarm_mask = 0xff;
418 data->voltage_in[0]=data->voltage_in[1]=data->voltage_in[2]=0;
419 gl518_init_client((struct i2c_client *) new_client);
420
421 /* Register sysfs hooks */
422 device_create_file(&new_client->dev, &dev_attr_in0_input);
423 device_create_file(&new_client->dev, &dev_attr_in1_input);
424 device_create_file(&new_client->dev, &dev_attr_in2_input);
425 device_create_file(&new_client->dev, &dev_attr_in3_input);
426 device_create_file(&new_client->dev, &dev_attr_in0_min);
427 device_create_file(&new_client->dev, &dev_attr_in1_min);
428 device_create_file(&new_client->dev, &dev_attr_in2_min);
429 device_create_file(&new_client->dev, &dev_attr_in3_min);
430 device_create_file(&new_client->dev, &dev_attr_in0_max);
431 device_create_file(&new_client->dev, &dev_attr_in1_max);
432 device_create_file(&new_client->dev, &dev_attr_in2_max);
433 device_create_file(&new_client->dev, &dev_attr_in3_max);
434 device_create_file(&new_client->dev, &dev_attr_fan1_auto);
435 device_create_file(&new_client->dev, &dev_attr_fan1_input);
436 device_create_file(&new_client->dev, &dev_attr_fan2_input);
437 device_create_file(&new_client->dev, &dev_attr_fan1_min);
438 device_create_file(&new_client->dev, &dev_attr_fan2_min);
439 device_create_file(&new_client->dev, &dev_attr_fan1_div);
440 device_create_file(&new_client->dev, &dev_attr_fan2_div);
441 device_create_file(&new_client->dev, &dev_attr_temp1_input);
442 device_create_file(&new_client->dev, &dev_attr_temp1_max);
443 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
444 device_create_file(&new_client->dev, &dev_attr_alarms);
445 device_create_file(&new_client->dev, &dev_attr_beep_enable);
446 device_create_file(&new_client->dev, &dev_attr_beep_mask);
447
448 return 0;
449
450/* OK, this is not exactly good programming practice, usually. But it is
451 very code-efficient in this case. */
452
453exit_free:
454 kfree(data);
455exit:
456 return err;
457}
458
459
460/* Called when we have found a new GL518SM.
461 Note that we preserve D4:NoFan2 and D2:beep_enable. */
462static void gl518_init_client(struct i2c_client *client)
463{
464 /* Make sure we leave D7:Reset untouched */
465 u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f;
466
467 /* Comparator mode (D3=0), standby mode (D6=0) */
468 gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
469
470 /* Never interrupts */
471 gl518_write_value(client, GL518_REG_MASK, 0x00);
472
473 /* Clear status register (D5=1), start (D6=1) */
474 gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue);
475 gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
476}
477
478static int gl518_detach_client(struct i2c_client *client)
479{
480 int err;
481
482 if ((err = i2c_detach_client(client))) {
483 dev_err(&client->dev, "Client deregistration failed, "
484 "client not detached.\n");
485 return err;
486 }
487
488 kfree(i2c_get_clientdata(client));
489
490 return 0;
491}
492
493/* Registers 0x07 to 0x0c are word-sized, others are byte-sized
494 GL518 uses a high-byte first convention, which is exactly opposite to
495 the usual practice. */
496static int gl518_read_value(struct i2c_client *client, u8 reg)
497{
498 if ((reg >= 0x07) && (reg <= 0x0c))
499 return swab16(i2c_smbus_read_word_data(client, reg));
500 else
501 return i2c_smbus_read_byte_data(client, reg);
502}
503
504/* Registers 0x07 to 0x0c are word-sized, others are byte-sized
505 GL518 uses a high-byte first convention, which is exactly opposite to
506 the usual practice. */
507static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
508{
509 if ((reg >= 0x07) && (reg <= 0x0c))
510 return i2c_smbus_write_word_data(client, reg, swab16(value));
511 else
512 return i2c_smbus_write_byte_data(client, reg, value);
513}
514
515static struct gl518_data *gl518_update_device(struct device *dev)
516{
517 struct i2c_client *client = to_i2c_client(dev);
518 struct gl518_data *data = i2c_get_clientdata(client);
519 int val;
520
521 down(&data->update_lock);
522
523 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
524 || !data->valid) {
525 dev_dbg(&client->dev, "Starting gl518 update\n");
526
527 data->alarms = gl518_read_value(client, GL518_REG_INT);
528 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
529
530 val = gl518_read_value(client, GL518_REG_VDD_LIMIT);
531 data->voltage_min[0] = val & 0xff;
532 data->voltage_max[0] = (val >> 8) & 0xff;
533 val = gl518_read_value(client, GL518_REG_VIN1_LIMIT);
534 data->voltage_min[1] = val & 0xff;
535 data->voltage_max[1] = (val >> 8) & 0xff;
536 val = gl518_read_value(client, GL518_REG_VIN2_LIMIT);
537 data->voltage_min[2] = val & 0xff;
538 data->voltage_max[2] = (val >> 8) & 0xff;
539 val = gl518_read_value(client, GL518_REG_VIN3_LIMIT);
540 data->voltage_min[3] = val & 0xff;
541 data->voltage_max[3] = (val >> 8) & 0xff;
542
543 val = gl518_read_value(client, GL518_REG_FAN_COUNT);
544 data->fan_in[0] = (val >> 8) & 0xff;
545 data->fan_in[1] = val & 0xff;
546
547 val = gl518_read_value(client, GL518_REG_FAN_LIMIT);
548 data->fan_min[0] = (val >> 8) & 0xff;
549 data->fan_min[1] = val & 0xff;
550
551 data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN);
552 data->temp_max =
553 gl518_read_value(client, GL518_REG_TEMP_MAX);
554 data->temp_hyst =
555 gl518_read_value(client, GL518_REG_TEMP_HYST);
556
557 val = gl518_read_value(client, GL518_REG_MISC);
558 data->fan_div[0] = (val >> 6) & 0x03;
559 data->fan_div[1] = (val >> 4) & 0x03;
560 data->fan_auto1 = (val >> 3) & 0x01;
561
562 data->alarms &= data->alarm_mask;
563
564 val = gl518_read_value(client, GL518_REG_CONF);
565 data->beep_enable = (val >> 2) & 1;
566
567 if (data->type != gl518sm_r00) {
568 data->voltage_in[0] =
569 gl518_read_value(client, GL518_REG_VDD);
570 data->voltage_in[1] =
571 gl518_read_value(client, GL518_REG_VIN1);
572 data->voltage_in[2] =
573 gl518_read_value(client, GL518_REG_VIN2);
574 }
575 data->voltage_in[3] =
576 gl518_read_value(client, GL518_REG_VIN3);
577
578 data->last_updated = jiffies;
579 data->valid = 1;
580 }
581
582 up(&data->update_lock);
583
584 return data;
585}
586
587static int __init sensors_gl518sm_init(void)
588{
589 return i2c_add_driver(&gl518_driver);
590}
591
592static void __exit sensors_gl518sm_exit(void)
593{
594 i2c_del_driver(&gl518_driver);
595}
596
597MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
598 "Kyosti Malkki <kmalkki@cc.hut.fi> and "
599 "Hong-Gunn Chew <hglinux@gunnet.org>");
600MODULE_DESCRIPTION("GL518SM driver");
601MODULE_LICENSE("GPL");
602
603module_init(sensors_gl518sm_init);
604module_exit(sensors_gl518sm_exit);
diff --git a/drivers/hwmon/gl520sm.c b/drivers/hwmon/gl520sm.c
new file mode 100644
index 000000000000..a13a504f5bfa
--- /dev/null
+++ b/drivers/hwmon/gl520sm.c
@@ -0,0 +1,769 @@
1/*
2 gl520sm.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>,
5 Kyösti Mälkki <kmalkki@cc.hut.fi>
6 Copyright (c) 2005 Maarten Deprez <maartendeprez@users.sourceforge.net>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22*/
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/i2c.h>
28#include <linux/i2c-sensor.h>
29#include <linux/i2c-vid.h>
30
31/* Type of the extra sensor */
32static unsigned short extra_sensor_type;
33module_param(extra_sensor_type, ushort, 0);
34MODULE_PARM_DESC(extra_sensor_type, "Type of extra sensor (0=autodetect, 1=temperature, 2=voltage)");
35
36/* Addresses to scan */
37static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
38static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
39
40/* Insmod parameters */
41SENSORS_INSMOD_1(gl520sm);
42
43/* Many GL520 constants specified below
44One of the inputs can be configured as either temp or voltage.
45That's why _TEMP2 and _IN4 access the same register
46*/
47
48/* The GL520 registers */
49#define GL520_REG_CHIP_ID 0x00
50#define GL520_REG_REVISION 0x01
51#define GL520_REG_CONF 0x03
52#define GL520_REG_MASK 0x11
53
54#define GL520_REG_VID_INPUT 0x02
55
56#define GL520_REG_IN0_INPUT 0x15
57#define GL520_REG_IN0_LIMIT 0x0c
58#define GL520_REG_IN0_MIN GL520_REG_IN0_LIMIT
59#define GL520_REG_IN0_MAX GL520_REG_IN0_LIMIT
60
61#define GL520_REG_IN1_INPUT 0x14
62#define GL520_REG_IN1_LIMIT 0x09
63#define GL520_REG_IN1_MIN GL520_REG_IN1_LIMIT
64#define GL520_REG_IN1_MAX GL520_REG_IN1_LIMIT
65
66#define GL520_REG_IN2_INPUT 0x13
67#define GL520_REG_IN2_LIMIT 0x0a
68#define GL520_REG_IN2_MIN GL520_REG_IN2_LIMIT
69#define GL520_REG_IN2_MAX GL520_REG_IN2_LIMIT
70
71#define GL520_REG_IN3_INPUT 0x0d
72#define GL520_REG_IN3_LIMIT 0x0b
73#define GL520_REG_IN3_MIN GL520_REG_IN3_LIMIT
74#define GL520_REG_IN3_MAX GL520_REG_IN3_LIMIT
75
76#define GL520_REG_IN4_INPUT 0x0e
77#define GL520_REG_IN4_MAX 0x17
78#define GL520_REG_IN4_MIN 0x18
79
80#define GL520_REG_TEMP1_INPUT 0x04
81#define GL520_REG_TEMP1_MAX 0x05
82#define GL520_REG_TEMP1_MAX_HYST 0x06
83
84#define GL520_REG_TEMP2_INPUT 0x0e
85#define GL520_REG_TEMP2_MAX 0x17
86#define GL520_REG_TEMP2_MAX_HYST 0x18
87
88#define GL520_REG_FAN_INPUT 0x07
89#define GL520_REG_FAN_MIN 0x08
90#define GL520_REG_FAN_DIV 0x0f
91#define GL520_REG_FAN_OFF GL520_REG_FAN_DIV
92
93#define GL520_REG_ALARMS 0x12
94#define GL520_REG_BEEP_MASK 0x10
95#define GL520_REG_BEEP_ENABLE GL520_REG_CONF
96
97/*
98 * Function declarations
99 */
100
101static int gl520_attach_adapter(struct i2c_adapter *adapter);
102static int gl520_detect(struct i2c_adapter *adapter, int address, int kind);
103static void gl520_init_client(struct i2c_client *client);
104static int gl520_detach_client(struct i2c_client *client);
105static int gl520_read_value(struct i2c_client *client, u8 reg);
106static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value);
107static struct gl520_data *gl520_update_device(struct device *dev);
108
109/* Driver data */
110static struct i2c_driver gl520_driver = {
111 .owner = THIS_MODULE,
112 .name = "gl520sm",
113 .id = I2C_DRIVERID_GL520,
114 .flags = I2C_DF_NOTIFY,
115 .attach_adapter = gl520_attach_adapter,
116 .detach_client = gl520_detach_client,
117};
118
119/* Client data */
120struct gl520_data {
121 struct i2c_client client;
122 struct semaphore update_lock;
123 char valid; /* zero until the following fields are valid */
124 unsigned long last_updated; /* in jiffies */
125
126 u8 vid;
127 u8 vrm;
128 u8 in_input[5]; /* [0] = VVD */
129 u8 in_min[5]; /* [0] = VDD */
130 u8 in_max[5]; /* [0] = VDD */
131 u8 fan_input[2];
132 u8 fan_min[2];
133 u8 fan_div[2];
134 u8 fan_off;
135 u8 temp_input[2];
136 u8 temp_max[2];
137 u8 temp_max_hyst[2];
138 u8 alarms;
139 u8 beep_enable;
140 u8 beep_mask;
141 u8 alarm_mask;
142 u8 two_temps;
143};
144
145/*
146 * Sysfs stuff
147 */
148
149#define sysfs_r(type, n, item, reg) \
150static ssize_t get_##type##item (struct gl520_data *, char *, int); \
151static ssize_t get_##type##n##item (struct device *, struct device_attribute *attr, char *); \
152static ssize_t get_##type##n##item (struct device *dev, struct device_attribute *attr, char *buf) \
153{ \
154 struct gl520_data *data = gl520_update_device(dev); \
155 return get_##type##item(data, buf, (n)); \
156}
157
158#define sysfs_w(type, n, item, reg) \
159static ssize_t set_##type##item (struct i2c_client *, struct gl520_data *, const char *, size_t, int, int); \
160static ssize_t set_##type##n##item (struct device *, struct device_attribute *attr, const char *, size_t); \
161static ssize_t set_##type##n##item (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
162{ \
163 struct i2c_client *client = to_i2c_client(dev); \
164 struct gl520_data *data = i2c_get_clientdata(client); \
165 return set_##type##item(client, data, buf, count, (n), reg); \
166}
167
168#define sysfs_rw_n(type, n, item, reg) \
169sysfs_r(type, n, item, reg) \
170sysfs_w(type, n, item, reg) \
171static DEVICE_ATTR(type##n##item, S_IRUGO | S_IWUSR, get_##type##n##item, set_##type##n##item);
172
173#define sysfs_ro_n(type, n, item, reg) \
174sysfs_r(type, n, item, reg) \
175static DEVICE_ATTR(type##n##item, S_IRUGO, get_##type##n##item, NULL);
176
177#define sysfs_rw(type, item, reg) \
178sysfs_r(type, 0, item, reg) \
179sysfs_w(type, 0, item, reg) \
180static DEVICE_ATTR(type##item, S_IRUGO | S_IWUSR, get_##type##0##item, set_##type##0##item);
181
182#define sysfs_ro(type, item, reg) \
183sysfs_r(type, 0, item, reg) \
184static DEVICE_ATTR(type##item, S_IRUGO, get_##type##0##item, NULL);
185
186
187#define sysfs_vid(n) \
188sysfs_ro_n(cpu, n, _vid, GL520_REG_VID_INPUT)
189
190#define device_create_file_vid(client, n) \
191device_create_file(&client->dev, &dev_attr_cpu##n##_vid)
192
193#define sysfs_in(n) \
194sysfs_ro_n(in, n, _input, GL520_REG_IN##n##INPUT) \
195sysfs_rw_n(in, n, _min, GL520_REG_IN##n##_MIN) \
196sysfs_rw_n(in, n, _max, GL520_REG_IN##n##_MAX) \
197
198#define device_create_file_in(client, n) \
199({device_create_file(&client->dev, &dev_attr_in##n##_input); \
200device_create_file(&client->dev, &dev_attr_in##n##_min); \
201device_create_file(&client->dev, &dev_attr_in##n##_max);})
202
203#define sysfs_fan(n) \
204sysfs_ro_n(fan, n, _input, GL520_REG_FAN_INPUT) \
205sysfs_rw_n(fan, n, _min, GL520_REG_FAN_MIN) \
206sysfs_rw_n(fan, n, _div, GL520_REG_FAN_DIV)
207
208#define device_create_file_fan(client, n) \
209({device_create_file(&client->dev, &dev_attr_fan##n##_input); \
210device_create_file(&client->dev, &dev_attr_fan##n##_min); \
211device_create_file(&client->dev, &dev_attr_fan##n##_div);})
212
213#define sysfs_fan_off(n) \
214sysfs_rw_n(fan, n, _off, GL520_REG_FAN_OFF) \
215
216#define device_create_file_fan_off(client, n) \
217device_create_file(&client->dev, &dev_attr_fan##n##_off)
218
219#define sysfs_temp(n) \
220sysfs_ro_n(temp, n, _input, GL520_REG_TEMP##n##_INPUT) \
221sysfs_rw_n(temp, n, _max, GL520_REG_TEMP##n##_MAX) \
222sysfs_rw_n(temp, n, _max_hyst, GL520_REG_TEMP##n##_MAX_HYST)
223
224#define device_create_file_temp(client, n) \
225({device_create_file(&client->dev, &dev_attr_temp##n##_input); \
226device_create_file(&client->dev, &dev_attr_temp##n##_max); \
227device_create_file(&client->dev, &dev_attr_temp##n##_max_hyst);})
228
229#define sysfs_alarms() \
230sysfs_ro(alarms, , GL520_REG_ALARMS) \
231sysfs_rw(beep_enable, , GL520_REG_BEEP_ENABLE) \
232sysfs_rw(beep_mask, , GL520_REG_BEEP_MASK)
233
234#define device_create_file_alarms(client) \
235({device_create_file(&client->dev, &dev_attr_alarms); \
236device_create_file(&client->dev, &dev_attr_beep_enable); \
237device_create_file(&client->dev, &dev_attr_beep_mask);})
238
239
240sysfs_vid(0)
241
242sysfs_in(0)
243sysfs_in(1)
244sysfs_in(2)
245sysfs_in(3)
246sysfs_in(4)
247
248sysfs_fan(1)
249sysfs_fan(2)
250sysfs_fan_off(1)
251
252sysfs_temp(1)
253sysfs_temp(2)
254
255sysfs_alarms()
256
257
258static ssize_t get_cpu_vid(struct gl520_data *data, char *buf, int n)
259{
260 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
261}
262
263#define VDD_FROM_REG(val) (((val)*95+2)/4)
264#define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
265
266#define IN_FROM_REG(val) ((val)*19)
267#define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
268
269static ssize_t get_in_input(struct gl520_data *data, char *buf, int n)
270{
271 u8 r = data->in_input[n];
272
273 if (n == 0)
274 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
275 else
276 return sprintf(buf, "%d\n", IN_FROM_REG(r));
277}
278
279static ssize_t get_in_min(struct gl520_data *data, char *buf, int n)
280{
281 u8 r = data->in_min[n];
282
283 if (n == 0)
284 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
285 else
286 return sprintf(buf, "%d\n", IN_FROM_REG(r));
287}
288
289static ssize_t get_in_max(struct gl520_data *data, char *buf, int n)
290{
291 u8 r = data->in_max[n];
292
293 if (n == 0)
294 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
295 else
296 return sprintf(buf, "%d\n", IN_FROM_REG(r));
297}
298
299static ssize_t set_in_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
300{
301 long v = simple_strtol(buf, NULL, 10);
302 u8 r;
303
304 down(&data->update_lock);
305
306 if (n == 0)
307 r = VDD_TO_REG(v);
308 else
309 r = IN_TO_REG(v);
310
311 data->in_min[n] = r;
312
313 if (n < 4)
314 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r);
315 else
316 gl520_write_value(client, reg, r);
317
318 up(&data->update_lock);
319 return count;
320}
321
322static ssize_t set_in_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
323{
324 long v = simple_strtol(buf, NULL, 10);
325 u8 r;
326
327 if (n == 0)
328 r = VDD_TO_REG(v);
329 else
330 r = IN_TO_REG(v);
331
332 down(&data->update_lock);
333
334 data->in_max[n] = r;
335
336 if (n < 4)
337 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8));
338 else
339 gl520_write_value(client, reg, r);
340
341 up(&data->update_lock);
342 return count;
343}
344
345#define DIV_FROM_REG(val) (1 << (val))
346#define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (480000/((val) << (div))))
347#define FAN_TO_REG(val,div) ((val)<=0?0:SENSORS_LIMIT((480000 + ((val) << ((div)-1))) / ((val) << (div)), 1, 255));
348
349static ssize_t get_fan_input(struct gl520_data *data, char *buf, int n)
350{
351 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_input[n - 1], data->fan_div[n - 1]));
352}
353
354static ssize_t get_fan_min(struct gl520_data *data, char *buf, int n)
355{
356 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[n - 1], data->fan_div[n - 1]));
357}
358
359static ssize_t get_fan_div(struct gl520_data *data, char *buf, int n)
360{
361 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[n - 1]));
362}
363
364static ssize_t get_fan_off(struct gl520_data *data, char *buf, int n)
365{
366 return sprintf(buf, "%d\n", data->fan_off);
367}
368
369static ssize_t set_fan_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
370{
371 unsigned long v = simple_strtoul(buf, NULL, 10);
372 u8 r;
373
374 down(&data->update_lock);
375 r = FAN_TO_REG(v, data->fan_div[n - 1]);
376 data->fan_min[n - 1] = r;
377
378 if (n == 1)
379 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8));
380 else
381 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r);
382
383 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
384 if (data->fan_min[n - 1] == 0)
385 data->alarm_mask &= (n == 1) ? ~0x20 : ~0x40;
386 else
387 data->alarm_mask |= (n == 1) ? 0x20 : 0x40;
388 data->beep_mask &= data->alarm_mask;
389 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
390
391 up(&data->update_lock);
392 return count;
393}
394
395static ssize_t set_fan_div(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
396{
397 unsigned long v = simple_strtoul(buf, NULL, 10);
398 u8 r;
399
400 switch (v) {
401 case 1: r = 0; break;
402 case 2: r = 1; break;
403 case 4: r = 2; break;
404 case 8: r = 3; break;
405 default:
406 dev_err(&client->dev, "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", v);
407 return -EINVAL;
408 }
409
410 down(&data->update_lock);
411 data->fan_div[n - 1] = r;
412
413 if (n == 1)
414 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xc0) | (r << 6));
415 else
416 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x30) | (r << 4));
417
418 up(&data->update_lock);
419 return count;
420}
421
422static ssize_t set_fan_off(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
423{
424 u8 r = simple_strtoul(buf, NULL, 10)?1:0;
425
426 down(&data->update_lock);
427 data->fan_off = r;
428 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x0c) | (r << 2));
429 up(&data->update_lock);
430 return count;
431}
432
433#define TEMP_FROM_REG(val) (((val) - 130) * 1000)
434#define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0?(val)-500:(val)+500) / 1000)+130),0,255))
435
436static ssize_t get_temp_input(struct gl520_data *data, char *buf, int n)
437{
438 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_input[n - 1]));
439}
440
441static ssize_t get_temp_max(struct gl520_data *data, char *buf, int n)
442{
443 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[n - 1]));
444}
445
446static ssize_t get_temp_max_hyst(struct gl520_data *data, char *buf, int n)
447{
448 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[n - 1]));
449}
450
451static ssize_t set_temp_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
452{
453 long v = simple_strtol(buf, NULL, 10);
454
455 down(&data->update_lock);
456 data->temp_max[n - 1] = TEMP_TO_REG(v);;
457 gl520_write_value(client, reg, data->temp_max[n - 1]);
458 up(&data->update_lock);
459 return count;
460}
461
462static ssize_t set_temp_max_hyst(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
463{
464 long v = simple_strtol(buf, NULL, 10);
465
466 down(&data->update_lock);
467 data->temp_max_hyst[n - 1] = TEMP_TO_REG(v);
468 gl520_write_value(client, reg, data->temp_max_hyst[n - 1]);
469 up(&data->update_lock);
470 return count;
471}
472
473static ssize_t get_alarms(struct gl520_data *data, char *buf, int n)
474{
475 return sprintf(buf, "%d\n", data->alarms);
476}
477
478static ssize_t get_beep_enable(struct gl520_data *data, char *buf, int n)
479{
480 return sprintf(buf, "%d\n", data->beep_enable);
481}
482
483static ssize_t get_beep_mask(struct gl520_data *data, char *buf, int n)
484{
485 return sprintf(buf, "%d\n", data->beep_mask);
486}
487
488static ssize_t set_beep_enable(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
489{
490 u8 r = simple_strtoul(buf, NULL, 10)?0:1;
491
492 down(&data->update_lock);
493 data->beep_enable = !r;
494 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x04) | (r << 2));
495 up(&data->update_lock);
496 return count;
497}
498
499static ssize_t set_beep_mask(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
500{
501 u8 r = simple_strtoul(buf, NULL, 10);
502
503 down(&data->update_lock);
504 r &= data->alarm_mask;
505 data->beep_mask = r;
506 gl520_write_value(client, reg, r);
507 up(&data->update_lock);
508 return count;
509}
510
511
512/*
513 * Real code
514 */
515
516static int gl520_attach_adapter(struct i2c_adapter *adapter)
517{
518 if (!(adapter->class & I2C_CLASS_HWMON))
519 return 0;
520 return i2c_detect(adapter, &addr_data, gl520_detect);
521}
522
523static int gl520_detect(struct i2c_adapter *adapter, int address, int kind)
524{
525 struct i2c_client *new_client;
526 struct gl520_data *data;
527 int err = 0;
528
529 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
530 I2C_FUNC_SMBUS_WORD_DATA))
531 goto exit;
532
533 /* OK. For now, we presume we have a valid client. We now create the
534 client structure, even though we cannot fill it completely yet.
535 But it allows us to access gl520_{read,write}_value. */
536
537 if (!(data = kmalloc(sizeof(struct gl520_data), GFP_KERNEL))) {
538 err = -ENOMEM;
539 goto exit;
540 }
541 memset(data, 0, sizeof(struct gl520_data));
542
543 new_client = &data->client;
544 i2c_set_clientdata(new_client, data);
545 new_client->addr = address;
546 new_client->adapter = adapter;
547 new_client->driver = &gl520_driver;
548 new_client->flags = 0;
549
550 /* Determine the chip type. */
551 if (kind < 0) {
552 if ((gl520_read_value(new_client, GL520_REG_CHIP_ID) != 0x20) ||
553 ((gl520_read_value(new_client, GL520_REG_REVISION) & 0x7f) != 0x00) ||
554 ((gl520_read_value(new_client, GL520_REG_CONF) & 0x80) != 0x00)) {
555 dev_dbg(&new_client->dev, "Unknown chip type, skipping\n");
556 goto exit_free;
557 }
558 }
559
560 /* Fill in the remaining client fields */
561 strlcpy(new_client->name, "gl520sm", I2C_NAME_SIZE);
562 data->valid = 0;
563 init_MUTEX(&data->update_lock);
564
565 /* Tell the I2C layer a new client has arrived */
566 if ((err = i2c_attach_client(new_client)))
567 goto exit_free;
568
569 /* Initialize the GL520SM chip */
570 gl520_init_client(new_client);
571
572 /* Register sysfs hooks */
573 device_create_file_vid(new_client, 0);
574
575 device_create_file_in(new_client, 0);
576 device_create_file_in(new_client, 1);
577 device_create_file_in(new_client, 2);
578 device_create_file_in(new_client, 3);
579 if (!data->two_temps)
580 device_create_file_in(new_client, 4);
581
582 device_create_file_fan(new_client, 1);
583 device_create_file_fan(new_client, 2);
584 device_create_file_fan_off(new_client, 1);
585
586 device_create_file_temp(new_client, 1);
587 if (data->two_temps)
588 device_create_file_temp(new_client, 2);
589
590 device_create_file_alarms(new_client);
591
592 return 0;
593
594exit_free:
595 kfree(data);
596exit:
597 return err;
598}
599
600
601/* Called when we have found a new GL520SM. */
602static void gl520_init_client(struct i2c_client *client)
603{
604 struct gl520_data *data = i2c_get_clientdata(client);
605 u8 oldconf, conf;
606
607 conf = oldconf = gl520_read_value(client, GL520_REG_CONF);
608
609 data->alarm_mask = 0xff;
610 data->vrm = i2c_which_vrm();
611
612 if (extra_sensor_type == 1)
613 conf &= ~0x10;
614 else if (extra_sensor_type == 2)
615 conf |= 0x10;
616 data->two_temps = !(conf & 0x10);
617
618 /* If IRQ# is disabled, we can safely force comparator mode */
619 if (!(conf & 0x20))
620 conf &= 0xf7;
621
622 /* Enable monitoring if needed */
623 conf |= 0x40;
624
625 if (conf != oldconf)
626 gl520_write_value(client, GL520_REG_CONF, conf);
627
628 gl520_update_device(&(client->dev));
629
630 if (data->fan_min[0] == 0)
631 data->alarm_mask &= ~0x20;
632 if (data->fan_min[1] == 0)
633 data->alarm_mask &= ~0x40;
634
635 data->beep_mask &= data->alarm_mask;
636 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
637}
638
639static int gl520_detach_client(struct i2c_client *client)
640{
641 int err;
642
643 if ((err = i2c_detach_client(client))) {
644 dev_err(&client->dev, "Client deregistration failed, "
645 "client not detached.\n");
646 return err;
647 }
648
649 kfree(i2c_get_clientdata(client));
650 return 0;
651}
652
653
654/* Registers 0x07 to 0x0c are word-sized, others are byte-sized
655 GL520 uses a high-byte first convention */
656static int gl520_read_value(struct i2c_client *client, u8 reg)
657{
658 if ((reg >= 0x07) && (reg <= 0x0c))
659 return swab16(i2c_smbus_read_word_data(client, reg));
660 else
661 return i2c_smbus_read_byte_data(client, reg);
662}
663
664static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value)
665{
666 if ((reg >= 0x07) && (reg <= 0x0c))
667 return i2c_smbus_write_word_data(client, reg, swab16(value));
668 else
669 return i2c_smbus_write_byte_data(client, reg, value);
670}
671
672
673static struct gl520_data *gl520_update_device(struct device *dev)
674{
675 struct i2c_client *client = to_i2c_client(dev);
676 struct gl520_data *data = i2c_get_clientdata(client);
677 int val;
678
679 down(&data->update_lock);
680
681 if ((jiffies - data->last_updated > 2 * HZ) ||
682 (jiffies < data->last_updated) || !data->valid) {
683
684 dev_dbg(&client->dev, "Starting gl520sm update\n");
685
686 data->alarms = gl520_read_value(client, GL520_REG_ALARMS);
687 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
688 data->vid = gl520_read_value(client, GL520_REG_VID_INPUT) & 0x1f;
689
690 val = gl520_read_value(client, GL520_REG_IN0_LIMIT);
691 data->in_min[0] = val & 0xff;
692 data->in_max[0] = (val >> 8) & 0xff;
693 val = gl520_read_value(client, GL520_REG_IN1_LIMIT);
694 data->in_min[1] = val & 0xff;
695 data->in_max[1] = (val >> 8) & 0xff;
696 val = gl520_read_value(client, GL520_REG_IN2_LIMIT);
697 data->in_min[2] = val & 0xff;
698 data->in_max[2] = (val >> 8) & 0xff;
699 val = gl520_read_value(client, GL520_REG_IN3_LIMIT);
700 data->in_min[3] = val & 0xff;
701 data->in_max[3] = (val >> 8) & 0xff;
702
703 val = gl520_read_value(client, GL520_REG_FAN_INPUT);
704 data->fan_input[0] = (val >> 8) & 0xff;
705 data->fan_input[1] = val & 0xff;
706
707 val = gl520_read_value(client, GL520_REG_FAN_MIN);
708 data->fan_min[0] = (val >> 8) & 0xff;
709 data->fan_min[1] = val & 0xff;
710
711 data->temp_input[0] = gl520_read_value(client, GL520_REG_TEMP1_INPUT);
712 data->temp_max[0] = gl520_read_value(client, GL520_REG_TEMP1_MAX);
713 data->temp_max_hyst[0] = gl520_read_value(client, GL520_REG_TEMP1_MAX_HYST);
714
715 val = gl520_read_value(client, GL520_REG_FAN_DIV);
716 data->fan_div[0] = (val >> 6) & 0x03;
717 data->fan_div[1] = (val >> 4) & 0x03;
718 data->fan_off = (val >> 2) & 0x01;
719
720 data->alarms &= data->alarm_mask;
721
722 val = gl520_read_value(client, GL520_REG_CONF);
723 data->beep_enable = !((val >> 2) & 1);
724
725 data->in_input[0] = gl520_read_value(client, GL520_REG_IN0_INPUT);
726 data->in_input[1] = gl520_read_value(client, GL520_REG_IN1_INPUT);
727 data->in_input[2] = gl520_read_value(client, GL520_REG_IN2_INPUT);
728 data->in_input[3] = gl520_read_value(client, GL520_REG_IN3_INPUT);
729
730 /* Temp1 and Vin4 are the same input */
731 if (data->two_temps) {
732 data->temp_input[1] = gl520_read_value(client, GL520_REG_TEMP2_INPUT);
733 data->temp_max[1] = gl520_read_value(client, GL520_REG_TEMP2_MAX);
734 data->temp_max_hyst[1] = gl520_read_value(client, GL520_REG_TEMP2_MAX_HYST);
735 } else {
736 data->in_input[4] = gl520_read_value(client, GL520_REG_IN4_INPUT);
737 data->in_min[4] = gl520_read_value(client, GL520_REG_IN4_MIN);
738 data->in_max[4] = gl520_read_value(client, GL520_REG_IN4_MAX);
739 }
740
741 data->last_updated = jiffies;
742 data->valid = 1;
743 }
744
745 up(&data->update_lock);
746
747 return data;
748}
749
750
751static int __init sensors_gl520sm_init(void)
752{
753 return i2c_add_driver(&gl520_driver);
754}
755
756static void __exit sensors_gl520sm_exit(void)
757{
758 i2c_del_driver(&gl520_driver);
759}
760
761
762MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
763 "Kyösti Mälkki <kmalkki@cc.hut.fi>, "
764 "Maarten Deprez <maartendeprez@users.sourceforge.net>");
765MODULE_DESCRIPTION("GL520SM driver");
766MODULE_LICENSE("GPL");
767
768module_init(sensors_gl520sm_init);
769module_exit(sensors_gl520sm_exit);
diff --git a/drivers/hwmon/it87.c b/drivers/hwmon/it87.c
new file mode 100644
index 000000000000..db20c9e47393
--- /dev/null
+++ b/drivers/hwmon/it87.c
@@ -0,0 +1,1184 @@
1/*
2 it87.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring.
4
5 Supports: IT8705F Super I/O chip w/LPC interface & SMBus
6 IT8712F Super I/O chip w/LPC interface & SMBus
7 Sis950 A clone of the IT8705F
8
9 Copyright (C) 2001 Chris Gauthron <chrisg@0-in.com>
10 Largely inspired by lm78.c of the same package
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25*/
26
27/*
28 djg@pdp8.net David Gesswein 7/18/01
29 Modified to fix bug with not all alarms enabled.
30 Added ability to read battery voltage and select temperature sensor
31 type at module load time.
32*/
33
34#include <linux/module.h>
35#include <linux/init.h>
36#include <linux/slab.h>
37#include <linux/jiffies.h>
38#include <linux/i2c.h>
39#include <linux/i2c-sensor.h>
40#include <linux/i2c-vid.h>
41#include <linux/hwmon-sysfs.h>
42#include <asm/io.h>
43
44
45/* Addresses to scan */
46static unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
47 0x2e, 0x2f, I2C_CLIENT_END };
48static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
49
50/* Insmod parameters */
51SENSORS_INSMOD_2(it87, it8712);
52
53#define REG 0x2e /* The register to read/write */
54#define DEV 0x07 /* Register: Logical device select */
55#define VAL 0x2f /* The value to read/write */
56#define PME 0x04 /* The device with the fan registers in it */
57#define DEVID 0x20 /* Register: Device ID */
58#define DEVREV 0x22 /* Register: Device Revision */
59
60static inline int
61superio_inb(int reg)
62{
63 outb(reg, REG);
64 return inb(VAL);
65}
66
67static int superio_inw(int reg)
68{
69 int val;
70 outb(reg++, REG);
71 val = inb(VAL) << 8;
72 outb(reg, REG);
73 val |= inb(VAL);
74 return val;
75}
76
77static inline void
78superio_select(void)
79{
80 outb(DEV, REG);
81 outb(PME, VAL);
82}
83
84static inline void
85superio_enter(void)
86{
87 outb(0x87, REG);
88 outb(0x01, REG);
89 outb(0x55, REG);
90 outb(0x55, REG);
91}
92
93static inline void
94superio_exit(void)
95{
96 outb(0x02, REG);
97 outb(0x02, VAL);
98}
99
100#define IT8712F_DEVID 0x8712
101#define IT8705F_DEVID 0x8705
102#define IT87_ACT_REG 0x30
103#define IT87_BASE_REG 0x60
104
105/* Update battery voltage after every reading if true */
106static int update_vbat;
107
108/* Not all BIOSes properly configure the PWM registers */
109static int fix_pwm_polarity;
110
111/* Chip Type */
112
113static u16 chip_type;
114
115/* Many IT87 constants specified below */
116
117/* Length of ISA address segment */
118#define IT87_EXTENT 8
119
120/* Where are the ISA address/data registers relative to the base address */
121#define IT87_ADDR_REG_OFFSET 5
122#define IT87_DATA_REG_OFFSET 6
123
124/*----- The IT87 registers -----*/
125
126#define IT87_REG_CONFIG 0x00
127
128#define IT87_REG_ALARM1 0x01
129#define IT87_REG_ALARM2 0x02
130#define IT87_REG_ALARM3 0x03
131
132#define IT87_REG_VID 0x0a
133#define IT87_REG_FAN_DIV 0x0b
134
135/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
136
137#define IT87_REG_FAN(nr) (0x0d + (nr))
138#define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
139#define IT87_REG_FAN_MAIN_CTRL 0x13
140#define IT87_REG_FAN_CTL 0x14
141#define IT87_REG_PWM(nr) (0x15 + (nr))
142
143#define IT87_REG_VIN(nr) (0x20 + (nr))
144#define IT87_REG_TEMP(nr) (0x29 + (nr))
145
146#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
147#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
148#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
149#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
150
151#define IT87_REG_I2C_ADDR 0x48
152
153#define IT87_REG_VIN_ENABLE 0x50
154#define IT87_REG_TEMP_ENABLE 0x51
155
156#define IT87_REG_CHIPID 0x58
157
158#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
159#define IN_FROM_REG(val) ((val) * 16)
160
161static inline u8 FAN_TO_REG(long rpm, int div)
162{
163 if (rpm == 0)
164 return 255;
165 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
166 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
167 254);
168}
169
170#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
171
172#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
173 ((val)+500)/1000),-128,127))
174#define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
175
176#define PWM_TO_REG(val) ((val) >> 1)
177#define PWM_FROM_REG(val) (((val)&0x7f) << 1)
178
179static int DIV_TO_REG(int val)
180{
181 int answer = 0;
182 while ((val >>= 1) != 0)
183 answer++;
184 return answer;
185}
186#define DIV_FROM_REG(val) (1 << (val))
187
188
189/* For each registered IT87, we need to keep some data in memory. That
190 data is pointed to by it87_list[NR]->data. The structure itself is
191 dynamically allocated, at the same time when a new it87 client is
192 allocated. */
193struct it87_data {
194 struct i2c_client client;
195 struct semaphore lock;
196 enum chips type;
197
198 struct semaphore update_lock;
199 char valid; /* !=0 if following fields are valid */
200 unsigned long last_updated; /* In jiffies */
201
202 u8 in[9]; /* Register value */
203 u8 in_max[9]; /* Register value */
204 u8 in_min[9]; /* Register value */
205 u8 fan[3]; /* Register value */
206 u8 fan_min[3]; /* Register value */
207 u8 temp[3]; /* Register value */
208 u8 temp_high[3]; /* Register value */
209 u8 temp_low[3]; /* Register value */
210 u8 sensor; /* Register value */
211 u8 fan_div[3]; /* Register encoding, shifted right */
212 u8 vid; /* Register encoding, combined */
213 int vrm;
214 u32 alarms; /* Register encoding, combined */
215 u8 fan_main_ctrl; /* Register value */
216 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
217};
218
219
220static int it87_attach_adapter(struct i2c_adapter *adapter);
221static int it87_find(int *address);
222static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
223static int it87_detach_client(struct i2c_client *client);
224
225static int it87_read_value(struct i2c_client *client, u8 register);
226static int it87_write_value(struct i2c_client *client, u8 register,
227 u8 value);
228static struct it87_data *it87_update_device(struct device *dev);
229static int it87_check_pwm(struct i2c_client *client);
230static void it87_init_client(struct i2c_client *client, struct it87_data *data);
231
232
233static struct i2c_driver it87_driver = {
234 .owner = THIS_MODULE,
235 .name = "it87",
236 .id = I2C_DRIVERID_IT87,
237 .flags = I2C_DF_NOTIFY,
238 .attach_adapter = it87_attach_adapter,
239 .detach_client = it87_detach_client,
240};
241
242static ssize_t show_in(struct device *dev, struct device_attribute *attr,
243 char *buf)
244{
245 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
246 int nr = sensor_attr->index;
247
248 struct it87_data *data = it87_update_device(dev);
249 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
250}
251
252static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
253 char *buf)
254{
255 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
256 int nr = sensor_attr->index;
257
258 struct it87_data *data = it87_update_device(dev);
259 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
260}
261
262static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
263 char *buf)
264{
265 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
266 int nr = sensor_attr->index;
267
268 struct it87_data *data = it87_update_device(dev);
269 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
270}
271
272static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
273 const char *buf, size_t count)
274{
275 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
276 int nr = sensor_attr->index;
277
278 struct i2c_client *client = to_i2c_client(dev);
279 struct it87_data *data = i2c_get_clientdata(client);
280 unsigned long val = simple_strtoul(buf, NULL, 10);
281
282 down(&data->update_lock);
283 data->in_min[nr] = IN_TO_REG(val);
284 it87_write_value(client, IT87_REG_VIN_MIN(nr),
285 data->in_min[nr]);
286 up(&data->update_lock);
287 return count;
288}
289static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
290 const char *buf, size_t count)
291{
292 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
293 int nr = sensor_attr->index;
294
295 struct i2c_client *client = to_i2c_client(dev);
296 struct it87_data *data = i2c_get_clientdata(client);
297 unsigned long val = simple_strtoul(buf, NULL, 10);
298
299 down(&data->update_lock);
300 data->in_max[nr] = IN_TO_REG(val);
301 it87_write_value(client, IT87_REG_VIN_MAX(nr),
302 data->in_max[nr]);
303 up(&data->update_lock);
304 return count;
305}
306
307#define show_in_offset(offset) \
308static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
309 show_in, NULL, offset);
310
311#define limit_in_offset(offset) \
312static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
313 show_in_min, set_in_min, offset); \
314static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
315 show_in_max, set_in_max, offset);
316
317show_in_offset(0);
318limit_in_offset(0);
319show_in_offset(1);
320limit_in_offset(1);
321show_in_offset(2);
322limit_in_offset(2);
323show_in_offset(3);
324limit_in_offset(3);
325show_in_offset(4);
326limit_in_offset(4);
327show_in_offset(5);
328limit_in_offset(5);
329show_in_offset(6);
330limit_in_offset(6);
331show_in_offset(7);
332limit_in_offset(7);
333show_in_offset(8);
334
335/* 3 temperatures */
336static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
337 char *buf)
338{
339 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
340 int nr = sensor_attr->index;
341
342 struct it87_data *data = it87_update_device(dev);
343 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
344}
345static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
346 char *buf)
347{
348 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
349 int nr = sensor_attr->index;
350
351 struct it87_data *data = it87_update_device(dev);
352 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
353}
354static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
355 char *buf)
356{
357 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
358 int nr = sensor_attr->index;
359
360 struct it87_data *data = it87_update_device(dev);
361 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
362}
363static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
364 const char *buf, size_t count)
365{
366 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
367 int nr = sensor_attr->index;
368
369 struct i2c_client *client = to_i2c_client(dev);
370 struct it87_data *data = i2c_get_clientdata(client);
371 int val = simple_strtol(buf, NULL, 10);
372
373 down(&data->update_lock);
374 data->temp_high[nr] = TEMP_TO_REG(val);
375 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
376 up(&data->update_lock);
377 return count;
378}
379static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
380 const char *buf, size_t count)
381{
382 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
383 int nr = sensor_attr->index;
384
385 struct i2c_client *client = to_i2c_client(dev);
386 struct it87_data *data = i2c_get_clientdata(client);
387 int val = simple_strtol(buf, NULL, 10);
388
389 down(&data->update_lock);
390 data->temp_low[nr] = TEMP_TO_REG(val);
391 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
392 up(&data->update_lock);
393 return count;
394}
395#define show_temp_offset(offset) \
396static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
397 show_temp, NULL, offset - 1); \
398static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
399 show_temp_max, set_temp_max, offset - 1); \
400static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
401 show_temp_min, set_temp_min, offset - 1);
402
403show_temp_offset(1);
404show_temp_offset(2);
405show_temp_offset(3);
406
407static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
408 char *buf)
409{
410 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
411 int nr = sensor_attr->index;
412
413 struct it87_data *data = it87_update_device(dev);
414 u8 reg = data->sensor; /* In case the value is updated while we use it */
415
416 if (reg & (1 << nr))
417 return sprintf(buf, "3\n"); /* thermal diode */
418 if (reg & (8 << nr))
419 return sprintf(buf, "2\n"); /* thermistor */
420 return sprintf(buf, "0\n"); /* disabled */
421}
422static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
423 const char *buf, size_t count)
424{
425 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
426 int nr = sensor_attr->index;
427
428 struct i2c_client *client = to_i2c_client(dev);
429 struct it87_data *data = i2c_get_clientdata(client);
430 int val = simple_strtol(buf, NULL, 10);
431
432 down(&data->update_lock);
433
434 data->sensor &= ~(1 << nr);
435 data->sensor &= ~(8 << nr);
436 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
437 if (val == 3)
438 data->sensor |= 1 << nr;
439 else if (val == 2)
440 data->sensor |= 8 << nr;
441 else if (val != 0) {
442 up(&data->update_lock);
443 return -EINVAL;
444 }
445 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
446 up(&data->update_lock);
447 return count;
448}
449#define show_sensor_offset(offset) \
450static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
451 show_sensor, set_sensor, offset - 1);
452
453show_sensor_offset(1);
454show_sensor_offset(2);
455show_sensor_offset(3);
456
457/* 3 Fans */
458static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
459 char *buf)
460{
461 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
462 int nr = sensor_attr->index;
463
464 struct it87_data *data = it87_update_device(dev);
465 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
466 DIV_FROM_REG(data->fan_div[nr])));
467}
468static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
469 char *buf)
470{
471 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
472 int nr = sensor_attr->index;
473
474 struct it87_data *data = it87_update_device(dev);
475 return sprintf(buf,"%d\n",
476 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
477}
478static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
479 char *buf)
480{
481 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
482 int nr = sensor_attr->index;
483
484 struct it87_data *data = it87_update_device(dev);
485 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
486}
487static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
488 char *buf)
489{
490 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
491 int nr = sensor_attr->index;
492
493 struct it87_data *data = it87_update_device(dev);
494 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
495}
496static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
497 char *buf)
498{
499 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
500 int nr = sensor_attr->index;
501
502 struct it87_data *data = it87_update_device(dev);
503 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
504}
505static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
506 const char *buf, size_t count)
507{
508 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
509 int nr = sensor_attr->index;
510
511 struct i2c_client *client = to_i2c_client(dev);
512 struct it87_data *data = i2c_get_clientdata(client);
513 int val = simple_strtol(buf, NULL, 10);
514
515 down(&data->update_lock);
516 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
517 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
518 up(&data->update_lock);
519 return count;
520}
521static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
522 const char *buf, size_t count)
523{
524 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
525 int nr = sensor_attr->index;
526
527 struct i2c_client *client = to_i2c_client(dev);
528 struct it87_data *data = i2c_get_clientdata(client);
529 int val = simple_strtol(buf, NULL, 10);
530 int i, min[3];
531 u8 old;
532
533 down(&data->update_lock);
534 old = it87_read_value(client, IT87_REG_FAN_DIV);
535
536 for (i = 0; i < 3; i++)
537 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
538
539 switch (nr) {
540 case 0:
541 case 1:
542 data->fan_div[nr] = DIV_TO_REG(val);
543 break;
544 case 2:
545 if (val < 8)
546 data->fan_div[nr] = 1;
547 else
548 data->fan_div[nr] = 3;
549 }
550 val = old & 0x80;
551 val |= (data->fan_div[0] & 0x07);
552 val |= (data->fan_div[1] & 0x07) << 3;
553 if (data->fan_div[2] == 3)
554 val |= 0x1 << 6;
555 it87_write_value(client, IT87_REG_FAN_DIV, val);
556
557 for (i = 0; i < 3; i++) {
558 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
559 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
560 }
561 up(&data->update_lock);
562 return count;
563}
564static ssize_t set_pwm_enable(struct device *dev,
565 struct device_attribute *attr, const char *buf, size_t count)
566{
567 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
568 int nr = sensor_attr->index;
569
570 struct i2c_client *client = to_i2c_client(dev);
571 struct it87_data *data = i2c_get_clientdata(client);
572 int val = simple_strtol(buf, NULL, 10);
573
574 down(&data->update_lock);
575
576 if (val == 0) {
577 int tmp;
578 /* make sure the fan is on when in on/off mode */
579 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
580 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
581 /* set on/off mode */
582 data->fan_main_ctrl &= ~(1 << nr);
583 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
584 } else if (val == 1) {
585 /* set SmartGuardian mode */
586 data->fan_main_ctrl |= (1 << nr);
587 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
588 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
589 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
590 } else {
591 up(&data->update_lock);
592 return -EINVAL;
593 }
594
595 up(&data->update_lock);
596 return count;
597}
598static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
599 const char *buf, size_t count)
600{
601 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
602 int nr = sensor_attr->index;
603
604 struct i2c_client *client = to_i2c_client(dev);
605 struct it87_data *data = i2c_get_clientdata(client);
606 int val = simple_strtol(buf, NULL, 10);
607
608 if (val < 0 || val > 255)
609 return -EINVAL;
610
611 down(&data->update_lock);
612 data->manual_pwm_ctl[nr] = val;
613 if (data->fan_main_ctrl & (1 << nr))
614 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
615 up(&data->update_lock);
616 return count;
617}
618
619#define show_fan_offset(offset) \
620static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
621 show_fan, NULL, offset - 1); \
622static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
623 show_fan_min, set_fan_min, offset - 1); \
624static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
625 show_fan_div, set_fan_div, offset - 1);
626
627show_fan_offset(1);
628show_fan_offset(2);
629show_fan_offset(3);
630
631#define show_pwm_offset(offset) \
632static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
633 show_pwm_enable, set_pwm_enable, offset - 1); \
634static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
635 show_pwm, set_pwm, offset - 1);
636
637show_pwm_offset(1);
638show_pwm_offset(2);
639show_pwm_offset(3);
640
641/* Alarms */
642static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
643{
644 struct it87_data *data = it87_update_device(dev);
645 return sprintf(buf, "%u\n", data->alarms);
646}
647static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
648
649static ssize_t
650show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
651{
652 struct it87_data *data = it87_update_device(dev);
653 return sprintf(buf, "%ld\n", (long) data->vrm);
654}
655static ssize_t
656store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
657{
658 struct i2c_client *client = to_i2c_client(dev);
659 struct it87_data *data = i2c_get_clientdata(client);
660 u32 val;
661
662 val = simple_strtoul(buf, NULL, 10);
663 data->vrm = val;
664
665 return count;
666}
667static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
668#define device_create_file_vrm(client) \
669device_create_file(&client->dev, &dev_attr_vrm)
670
671static ssize_t
672show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
673{
674 struct it87_data *data = it87_update_device(dev);
675 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
676}
677static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
678#define device_create_file_vid(client) \
679device_create_file(&client->dev, &dev_attr_cpu0_vid)
680
681/* This function is called when:
682 * it87_driver is inserted (when this module is loaded), for each
683 available adapter
684 * when a new adapter is inserted (and it87_driver is still present) */
685static int it87_attach_adapter(struct i2c_adapter *adapter)
686{
687 if (!(adapter->class & I2C_CLASS_HWMON))
688 return 0;
689 return i2c_detect(adapter, &addr_data, it87_detect);
690}
691
692/* SuperIO detection - will change normal_isa[0] if a chip is found */
693static int it87_find(int *address)
694{
695 int err = -ENODEV;
696
697 superio_enter();
698 chip_type = superio_inw(DEVID);
699 if (chip_type != IT8712F_DEVID
700 && chip_type != IT8705F_DEVID)
701 goto exit;
702
703 superio_select();
704 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
705 pr_info("it87: Device not activated, skipping\n");
706 goto exit;
707 }
708
709 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
710 if (*address == 0) {
711 pr_info("it87: Base address not set, skipping\n");
712 goto exit;
713 }
714
715 err = 0;
716 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
717 chip_type, *address, superio_inb(DEVREV) & 0x0f);
718
719exit:
720 superio_exit();
721 return err;
722}
723
724/* This function is called by i2c_detect */
725int it87_detect(struct i2c_adapter *adapter, int address, int kind)
726{
727 int i;
728 struct i2c_client *new_client;
729 struct it87_data *data;
730 int err = 0;
731 const char *name = "";
732 int is_isa = i2c_is_isa_adapter(adapter);
733 int enable_pwm_interface;
734
735 if (!is_isa &&
736 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
737 goto ERROR0;
738
739 /* Reserve the ISA region */
740 if (is_isa)
741 if (!request_region(address, IT87_EXTENT, it87_driver.name))
742 goto ERROR0;
743
744 /* Probe whether there is anything available on this address. Already
745 done for SMBus and Super-I/O clients */
746 if (kind < 0) {
747 if (is_isa && !chip_type) {
748#define REALLY_SLOW_IO
749 /* We need the timeouts for at least some IT87-like chips. But only
750 if we read 'undefined' registers. */
751 i = inb_p(address + 1);
752 if (inb_p(address + 2) != i
753 || inb_p(address + 3) != i
754 || inb_p(address + 7) != i) {
755 err = -ENODEV;
756 goto ERROR1;
757 }
758#undef REALLY_SLOW_IO
759
760 /* Let's just hope nothing breaks here */
761 i = inb_p(address + 5) & 0x7f;
762 outb_p(~i & 0x7f, address + 5);
763 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
764 outb_p(i, address + 5);
765 err = -ENODEV;
766 goto ERROR1;
767 }
768 }
769 }
770
771 /* OK. For now, we presume we have a valid client. We now create the
772 client structure, even though we cannot fill it completely yet.
773 But it allows us to access it87_{read,write}_value. */
774
775 if (!(data = kmalloc(sizeof(struct it87_data), GFP_KERNEL))) {
776 err = -ENOMEM;
777 goto ERROR1;
778 }
779 memset(data, 0, sizeof(struct it87_data));
780
781 new_client = &data->client;
782 if (is_isa)
783 init_MUTEX(&data->lock);
784 i2c_set_clientdata(new_client, data);
785 new_client->addr = address;
786 new_client->adapter = adapter;
787 new_client->driver = &it87_driver;
788 new_client->flags = 0;
789
790 /* Now, we do the remaining detection. */
791
792 if (kind < 0) {
793 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
794 || (!is_isa
795 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
796 err = -ENODEV;
797 goto ERROR2;
798 }
799 }
800
801 /* Determine the chip type. */
802 if (kind <= 0) {
803 i = it87_read_value(new_client, IT87_REG_CHIPID);
804 if (i == 0x90) {
805 kind = it87;
806 if ((is_isa) && (chip_type == IT8712F_DEVID))
807 kind = it8712;
808 }
809 else {
810 if (kind == 0)
811 dev_info(&adapter->dev,
812 "Ignoring 'force' parameter for unknown chip at "
813 "adapter %d, address 0x%02x\n",
814 i2c_adapter_id(adapter), address);
815 err = -ENODEV;
816 goto ERROR2;
817 }
818 }
819
820 if (kind == it87) {
821 name = "it87";
822 } else if (kind == it8712) {
823 name = "it8712";
824 }
825
826 /* Fill in the remaining client fields and put it into the global list */
827 strlcpy(new_client->name, name, I2C_NAME_SIZE);
828 data->type = kind;
829 data->valid = 0;
830 init_MUTEX(&data->update_lock);
831
832 /* Tell the I2C layer a new client has arrived */
833 if ((err = i2c_attach_client(new_client)))
834 goto ERROR2;
835
836 /* Check PWM configuration */
837 enable_pwm_interface = it87_check_pwm(new_client);
838
839 /* Initialize the IT87 chip */
840 it87_init_client(new_client, data);
841
842 /* Register sysfs hooks */
843 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
844 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
845 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
846 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
847 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
848 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
849 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
850 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
851 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
852 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
853 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
854 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
855 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
856 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
857 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
858 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
859 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
860 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
861 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
862 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
863 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
864 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
865 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
866 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
867 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
868 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
869 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
870 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
871 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
872 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
873 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
874 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
875 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
876 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
877 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_type.dev_attr);
878 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_type.dev_attr);
879 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_type.dev_attr);
880 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_input.dev_attr);
881 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_input.dev_attr);
882 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_input.dev_attr);
883 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_min.dev_attr);
884 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_min.dev_attr);
885 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_min.dev_attr);
886 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_div.dev_attr);
887 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_div.dev_attr);
888 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_div.dev_attr);
889 device_create_file(&new_client->dev, &dev_attr_alarms);
890 if (enable_pwm_interface) {
891 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1_enable.dev_attr);
892 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2_enable.dev_attr);
893 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3_enable.dev_attr);
894 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
895 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
896 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
897 }
898
899 if (data->type == it8712) {
900 data->vrm = i2c_which_vrm();
901 device_create_file_vrm(new_client);
902 device_create_file_vid(new_client);
903 }
904
905 return 0;
906
907ERROR2:
908 kfree(data);
909ERROR1:
910 if (is_isa)
911 release_region(address, IT87_EXTENT);
912ERROR0:
913 return err;
914}
915
916static int it87_detach_client(struct i2c_client *client)
917{
918 int err;
919
920 if ((err = i2c_detach_client(client))) {
921 dev_err(&client->dev,
922 "Client deregistration failed, client not detached.\n");
923 return err;
924 }
925
926 if(i2c_is_isa_client(client))
927 release_region(client->addr, IT87_EXTENT);
928 kfree(i2c_get_clientdata(client));
929
930 return 0;
931}
932
933/* The SMBus locks itself, but ISA access must be locked explicitly!
934 We don't want to lock the whole ISA bus, so we lock each client
935 separately.
936 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
937 would slow down the IT87 access and should not be necessary. */
938static int it87_read_value(struct i2c_client *client, u8 reg)
939{
940 struct it87_data *data = i2c_get_clientdata(client);
941
942 int res;
943 if (i2c_is_isa_client(client)) {
944 down(&data->lock);
945 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
946 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
947 up(&data->lock);
948 return res;
949 } else
950 return i2c_smbus_read_byte_data(client, reg);
951}
952
953/* The SMBus locks itself, but ISA access muse be locked explicitly!
954 We don't want to lock the whole ISA bus, so we lock each client
955 separately.
956 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
957 would slow down the IT87 access and should not be necessary. */
958static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
959{
960 struct it87_data *data = i2c_get_clientdata(client);
961
962 if (i2c_is_isa_client(client)) {
963 down(&data->lock);
964 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
965 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
966 up(&data->lock);
967 return 0;
968 } else
969 return i2c_smbus_write_byte_data(client, reg, value);
970}
971
972/* Return 1 if and only if the PWM interface is safe to use */
973static int it87_check_pwm(struct i2c_client *client)
974{
975 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
976 * and polarity set to active low is sign that this is the case so we
977 * disable pwm control to protect the user. */
978 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
979 if ((tmp & 0x87) == 0) {
980 if (fix_pwm_polarity) {
981 /* The user asks us to attempt a chip reconfiguration.
982 * This means switching to active high polarity and
983 * inverting all fan speed values. */
984 int i;
985 u8 pwm[3];
986
987 for (i = 0; i < 3; i++)
988 pwm[i] = it87_read_value(client,
989 IT87_REG_PWM(i));
990
991 /* If any fan is in automatic pwm mode, the polarity
992 * might be correct, as suspicious as it seems, so we
993 * better don't change anything (but still disable the
994 * PWM interface). */
995 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
996 dev_info(&client->dev, "Reconfiguring PWM to "
997 "active high polarity\n");
998 it87_write_value(client, IT87_REG_FAN_CTL,
999 tmp | 0x87);
1000 for (i = 0; i < 3; i++)
1001 it87_write_value(client,
1002 IT87_REG_PWM(i),
1003 0x7f & ~pwm[i]);
1004 return 1;
1005 }
1006
1007 dev_info(&client->dev, "PWM configuration is "
1008 "too broken to be fixed\n");
1009 }
1010
1011 dev_info(&client->dev, "Detected broken BIOS "
1012 "defaults, disabling PWM interface\n");
1013 return 0;
1014 } else if (fix_pwm_polarity) {
1015 dev_info(&client->dev, "PWM configuration looks "
1016 "sane, won't touch\n");
1017 }
1018
1019 return 1;
1020}
1021
1022/* Called when we have found a new IT87. */
1023static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1024{
1025 int tmp, i;
1026
1027 /* initialize to sane defaults:
1028 * - if the chip is in manual pwm mode, this will be overwritten with
1029 * the actual settings on the chip (so in this case, initialization
1030 * is not needed)
1031 * - if in automatic or on/off mode, we could switch to manual mode,
1032 * read the registers and set manual_pwm_ctl accordingly, but currently
1033 * this is not implemented, so we initialize to something sane */
1034 for (i = 0; i < 3; i++) {
1035 data->manual_pwm_ctl[i] = 0xff;
1036 }
1037
1038 /* Check if temperature channnels are reset manually or by some reason */
1039 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1040 if ((tmp & 0x3f) == 0) {
1041 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1042 tmp = (tmp & 0xc0) | 0x2a;
1043 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1044 }
1045 data->sensor = tmp;
1046
1047 /* Check if voltage monitors are reset manually or by some reason */
1048 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1049 if ((tmp & 0xff) == 0) {
1050 /* Enable all voltage monitors */
1051 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1052 }
1053
1054 /* Check if tachometers are reset manually or by some reason */
1055 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1056 if ((data->fan_main_ctrl & 0x70) == 0) {
1057 /* Enable all fan tachometers */
1058 data->fan_main_ctrl |= 0x70;
1059 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1060 }
1061
1062 /* Set current fan mode registers and the default settings for the
1063 * other mode registers */
1064 for (i = 0; i < 3; i++) {
1065 if (data->fan_main_ctrl & (1 << i)) {
1066 /* pwm mode */
1067 tmp = it87_read_value(client, IT87_REG_PWM(i));
1068 if (tmp & 0x80) {
1069 /* automatic pwm - not yet implemented, but
1070 * leave the settings made by the BIOS alone
1071 * until a change is requested via the sysfs
1072 * interface */
1073 } else {
1074 /* manual pwm */
1075 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1076 }
1077 }
1078 }
1079
1080 /* Start monitoring */
1081 it87_write_value(client, IT87_REG_CONFIG,
1082 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1083 | (update_vbat ? 0x41 : 0x01));
1084}
1085
1086static struct it87_data *it87_update_device(struct device *dev)
1087{
1088 struct i2c_client *client = to_i2c_client(dev);
1089 struct it87_data *data = i2c_get_clientdata(client);
1090 int i;
1091
1092 down(&data->update_lock);
1093
1094 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1095 || !data->valid) {
1096
1097 if (update_vbat) {
1098 /* Cleared after each update, so reenable. Value
1099 returned by this read will be previous value */
1100 it87_write_value(client, IT87_REG_CONFIG,
1101 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1102 }
1103 for (i = 0; i <= 7; i++) {
1104 data->in[i] =
1105 it87_read_value(client, IT87_REG_VIN(i));
1106 data->in_min[i] =
1107 it87_read_value(client, IT87_REG_VIN_MIN(i));
1108 data->in_max[i] =
1109 it87_read_value(client, IT87_REG_VIN_MAX(i));
1110 }
1111 data->in[8] =
1112 it87_read_value(client, IT87_REG_VIN(8));
1113 /* Temperature sensor doesn't have limit registers, set
1114 to min and max value */
1115 data->in_min[8] = 0;
1116 data->in_max[8] = 255;
1117
1118 for (i = 0; i < 3; i++) {
1119 data->fan[i] =
1120 it87_read_value(client, IT87_REG_FAN(i));
1121 data->fan_min[i] =
1122 it87_read_value(client, IT87_REG_FAN_MIN(i));
1123 }
1124 for (i = 0; i < 3; i++) {
1125 data->temp[i] =
1126 it87_read_value(client, IT87_REG_TEMP(i));
1127 data->temp_high[i] =
1128 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1129 data->temp_low[i] =
1130 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1131 }
1132
1133 i = it87_read_value(client, IT87_REG_FAN_DIV);
1134 data->fan_div[0] = i & 0x07;
1135 data->fan_div[1] = (i >> 3) & 0x07;
1136 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1137
1138 data->alarms =
1139 it87_read_value(client, IT87_REG_ALARM1) |
1140 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1141 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1142 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1143
1144 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1145 /* The 8705 does not have VID capability */
1146 if (data->type == it8712) {
1147 data->vid = it87_read_value(client, IT87_REG_VID);
1148 data->vid &= 0x1f;
1149 }
1150 data->last_updated = jiffies;
1151 data->valid = 1;
1152 }
1153
1154 up(&data->update_lock);
1155
1156 return data;
1157}
1158
1159static int __init sm_it87_init(void)
1160{
1161 int addr;
1162
1163 if (!it87_find(&addr)) {
1164 normal_isa[0] = addr;
1165 }
1166 return i2c_add_driver(&it87_driver);
1167}
1168
1169static void __exit sm_it87_exit(void)
1170{
1171 i2c_del_driver(&it87_driver);
1172}
1173
1174
1175MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1176MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
1177module_param(update_vbat, bool, 0);
1178MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1179module_param(fix_pwm_polarity, bool, 0);
1180MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1181MODULE_LICENSE("GPL");
1182
1183module_init(sm_it87_init);
1184module_exit(sm_it87_exit);
diff --git a/drivers/hwmon/lm63.c b/drivers/hwmon/lm63.c
new file mode 100644
index 000000000000..7c6f9ea5a254
--- /dev/null
+++ b/drivers/hwmon/lm63.c
@@ -0,0 +1,597 @@
1/*
2 * lm63.c - driver for the National Semiconductor LM63 temperature sensor
3 * with integrated fan control
4 * Copyright (C) 2004-2005 Jean Delvare <khali@linux-fr.org>
5 * Based on the lm90 driver.
6 *
7 * The LM63 is a sensor chip made by National Semiconductor. It measures
8 * two temperatures (its own and one external one) and the speed of one
9 * fan, those speed it can additionally control. Complete datasheet can be
10 * obtained from National's website at:
11 * http://www.national.com/pf/LM/LM63.html
12 *
13 * The LM63 is basically an LM86 with fan speed monitoring and control
14 * capabilities added. It misses some of the LM86 features though:
15 * - No low limit for local temperature.
16 * - No critical limit for local temperature.
17 * - Critical limit for remote temperature can be changed only once. We
18 * will consider that the critical limit is read-only.
19 *
20 * The datasheet isn't very clear about what the tachometer reading is.
21 * I had a explanation from National Semiconductor though. The two lower
22 * bits of the read value have to be masked out. The value is still 16 bit
23 * in width.
24 *
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
29 *
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38 */
39
40#include <linux/module.h>
41#include <linux/init.h>
42#include <linux/slab.h>
43#include <linux/jiffies.h>
44#include <linux/i2c.h>
45#include <linux/i2c-sensor.h>
46#include <linux/hwmon-sysfs.h>
47
48/*
49 * Addresses to scan
50 * Address is fully defined internally and cannot be changed.
51 */
52
53static unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END };
54static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
55
56/*
57 * Insmod parameters
58 */
59
60SENSORS_INSMOD_1(lm63);
61
62/*
63 * The LM63 registers
64 */
65
66#define LM63_REG_CONFIG1 0x03
67#define LM63_REG_CONFIG2 0xBF
68#define LM63_REG_CONFIG_FAN 0x4A
69
70#define LM63_REG_TACH_COUNT_MSB 0x47
71#define LM63_REG_TACH_COUNT_LSB 0x46
72#define LM63_REG_TACH_LIMIT_MSB 0x49
73#define LM63_REG_TACH_LIMIT_LSB 0x48
74
75#define LM63_REG_PWM_VALUE 0x4C
76#define LM63_REG_PWM_FREQ 0x4D
77
78#define LM63_REG_LOCAL_TEMP 0x00
79#define LM63_REG_LOCAL_HIGH 0x05
80
81#define LM63_REG_REMOTE_TEMP_MSB 0x01
82#define LM63_REG_REMOTE_TEMP_LSB 0x10
83#define LM63_REG_REMOTE_OFFSET_MSB 0x11
84#define LM63_REG_REMOTE_OFFSET_LSB 0x12
85#define LM63_REG_REMOTE_HIGH_MSB 0x07
86#define LM63_REG_REMOTE_HIGH_LSB 0x13
87#define LM63_REG_REMOTE_LOW_MSB 0x08
88#define LM63_REG_REMOTE_LOW_LSB 0x14
89#define LM63_REG_REMOTE_TCRIT 0x19
90#define LM63_REG_REMOTE_TCRIT_HYST 0x21
91
92#define LM63_REG_ALERT_STATUS 0x02
93#define LM63_REG_ALERT_MASK 0x16
94
95#define LM63_REG_MAN_ID 0xFE
96#define LM63_REG_CHIP_ID 0xFF
97
98/*
99 * Conversions and various macros
100 * For tachometer counts, the LM63 uses 16-bit values.
101 * For local temperature and high limit, remote critical limit and hysteresis
102 * value, it uses signed 8-bit values with LSB = 1 degree Celsius.
103 * For remote temperature, low and high limits, it uses signed 11-bit values
104 * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
105 */
106
107#define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
108 5400000 / (reg))
109#define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
110 (5400000 / (val)) & 0xFFFC)
111#define TEMP8_FROM_REG(reg) ((reg) * 1000)
112#define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \
113 (val) >= 127000 ? 127 : \
114 (val) < 0 ? ((val) - 500) / 1000 : \
115 ((val) + 500) / 1000)
116#define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
117#define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
118 (val) >= 127875 ? 0x7FE0 : \
119 (val) < 0 ? ((val) - 62) / 125 * 32 : \
120 ((val) + 62) / 125 * 32)
121#define HYST_TO_REG(val) ((val) <= 0 ? 0 : \
122 (val) >= 127000 ? 127 : \
123 ((val) + 500) / 1000)
124
125/*
126 * Functions declaration
127 */
128
129static int lm63_attach_adapter(struct i2c_adapter *adapter);
130static int lm63_detach_client(struct i2c_client *client);
131
132static struct lm63_data *lm63_update_device(struct device *dev);
133
134static int lm63_detect(struct i2c_adapter *adapter, int address, int kind);
135static void lm63_init_client(struct i2c_client *client);
136
137/*
138 * Driver data (common to all clients)
139 */
140
141static struct i2c_driver lm63_driver = {
142 .owner = THIS_MODULE,
143 .name = "lm63",
144 .flags = I2C_DF_NOTIFY,
145 .attach_adapter = lm63_attach_adapter,
146 .detach_client = lm63_detach_client,
147};
148
149/*
150 * Client data (each client gets its own)
151 */
152
153struct lm63_data {
154 struct i2c_client client;
155 struct semaphore update_lock;
156 char valid; /* zero until following fields are valid */
157 unsigned long last_updated; /* in jiffies */
158
159 /* registers values */
160 u8 config, config_fan;
161 u16 fan[2]; /* 0: input
162 1: low limit */
163 u8 pwm1_freq;
164 u8 pwm1_value;
165 s8 temp8[3]; /* 0: local input
166 1: local high limit
167 2: remote critical limit */
168 s16 temp11[3]; /* 0: remote input
169 1: remote low limit
170 2: remote high limit */
171 u8 temp2_crit_hyst;
172 u8 alarms;
173};
174
175/*
176 * Sysfs callback functions and files
177 */
178
179static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
180 char *buf)
181{
182 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
183 struct lm63_data *data = lm63_update_device(dev);
184 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
185}
186
187static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
188 const char *buf, size_t count)
189{
190 struct i2c_client *client = to_i2c_client(dev);
191 struct lm63_data *data = i2c_get_clientdata(client);
192 unsigned long val = simple_strtoul(buf, NULL, 10);
193
194 down(&data->update_lock);
195 data->fan[1] = FAN_TO_REG(val);
196 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
197 data->fan[1] & 0xFF);
198 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
199 data->fan[1] >> 8);
200 up(&data->update_lock);
201 return count;
202}
203
204static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy,
205 char *buf)
206{
207 struct lm63_data *data = lm63_update_device(dev);
208 return sprintf(buf, "%d\n", data->pwm1_value >= 2 * data->pwm1_freq ?
209 255 : (data->pwm1_value * 255 + data->pwm1_freq) /
210 (2 * data->pwm1_freq));
211}
212
213static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy,
214 const char *buf, size_t count)
215{
216 struct i2c_client *client = to_i2c_client(dev);
217 struct lm63_data *data = i2c_get_clientdata(client);
218 unsigned long val;
219
220 if (!(data->config_fan & 0x20)) /* register is read-only */
221 return -EPERM;
222
223 val = simple_strtoul(buf, NULL, 10);
224 down(&data->update_lock);
225 data->pwm1_value = val <= 0 ? 0 :
226 val >= 255 ? 2 * data->pwm1_freq :
227 (val * data->pwm1_freq * 2 + 127) / 255;
228 i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value);
229 up(&data->update_lock);
230 return count;
231}
232
233static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dummy,
234 char *buf)
235{
236 struct lm63_data *data = lm63_update_device(dev);
237 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
238}
239
240static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
241 char *buf)
242{
243 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
244 struct lm63_data *data = lm63_update_device(dev);
245 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
246}
247
248static ssize_t set_temp8(struct device *dev, struct device_attribute *dummy,
249 const char *buf, size_t count)
250{
251 struct i2c_client *client = to_i2c_client(dev);
252 struct lm63_data *data = i2c_get_clientdata(client);
253 long val = simple_strtol(buf, NULL, 10);
254
255 down(&data->update_lock);
256 data->temp8[1] = TEMP8_TO_REG(val);
257 i2c_smbus_write_byte_data(client, LM63_REG_LOCAL_HIGH, data->temp8[1]);
258 up(&data->update_lock);
259 return count;
260}
261
262static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
263 char *buf)
264{
265 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
266 struct lm63_data *data = lm63_update_device(dev);
267 return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[attr->index]));
268}
269
270static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
271 const char *buf, size_t count)
272{
273 static const u8 reg[4] = {
274 LM63_REG_REMOTE_LOW_MSB,
275 LM63_REG_REMOTE_LOW_LSB,
276 LM63_REG_REMOTE_HIGH_MSB,
277 LM63_REG_REMOTE_HIGH_LSB,
278 };
279
280 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
281 struct i2c_client *client = to_i2c_client(dev);
282 struct lm63_data *data = i2c_get_clientdata(client);
283 long val = simple_strtol(buf, NULL, 10);
284 int nr = attr->index;
285
286 down(&data->update_lock);
287 data->temp11[nr] = TEMP11_TO_REG(val);
288 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
289 data->temp11[nr] >> 8);
290 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
291 data->temp11[nr] & 0xff);
292 up(&data->update_lock);
293 return count;
294}
295
296/* Hysteresis register holds a relative value, while we want to present
297 an absolute to user-space */
298static ssize_t show_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy,
299 char *buf)
300{
301 struct lm63_data *data = lm63_update_device(dev);
302 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[2])
303 - TEMP8_FROM_REG(data->temp2_crit_hyst));
304}
305
306/* And now the other way around, user-space provides an absolute
307 hysteresis value and we have to store a relative one */
308static ssize_t set_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy,
309 const char *buf, size_t count)
310{
311 struct i2c_client *client = to_i2c_client(dev);
312 struct lm63_data *data = i2c_get_clientdata(client);
313 long val = simple_strtol(buf, NULL, 10);
314 long hyst;
315
316 down(&data->update_lock);
317 hyst = TEMP8_FROM_REG(data->temp8[2]) - val;
318 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
319 HYST_TO_REG(hyst));
320 up(&data->update_lock);
321 return count;
322}
323
324static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
325 char *buf)
326{
327 struct lm63_data *data = lm63_update_device(dev);
328 return sprintf(buf, "%u\n", data->alarms);
329}
330
331static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
332static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
333 set_fan, 1);
334
335static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1);
336static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL);
337
338static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp8, NULL, 0);
339static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
340 set_temp8, 1);
341
342static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
343static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
344 set_temp11, 1);
345static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
346 set_temp11, 2);
347static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp8, NULL, 2);
348static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
349 set_temp2_crit_hyst);
350
351static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
352
353/*
354 * Real code
355 */
356
357static int lm63_attach_adapter(struct i2c_adapter *adapter)
358{
359 if (!(adapter->class & I2C_CLASS_HWMON))
360 return 0;
361 return i2c_detect(adapter, &addr_data, lm63_detect);
362}
363
364/*
365 * The following function does more than just detection. If detection
366 * succeeds, it also registers the new chip.
367 */
368static int lm63_detect(struct i2c_adapter *adapter, int address, int kind)
369{
370 struct i2c_client *new_client;
371 struct lm63_data *data;
372 int err = 0;
373
374 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
375 goto exit;
376
377 if (!(data = kmalloc(sizeof(struct lm63_data), GFP_KERNEL))) {
378 err = -ENOMEM;
379 goto exit;
380 }
381 memset(data, 0, sizeof(struct lm63_data));
382
383 /* The common I2C client data is placed right before the
384 LM63-specific data. */
385 new_client = &data->client;
386 i2c_set_clientdata(new_client, data);
387 new_client->addr = address;
388 new_client->adapter = adapter;
389 new_client->driver = &lm63_driver;
390 new_client->flags = 0;
391
392 /* Default to an LM63 if forced */
393 if (kind == 0)
394 kind = lm63;
395
396 if (kind < 0) { /* must identify */
397 u8 man_id, chip_id, reg_config1, reg_config2;
398 u8 reg_alert_status, reg_alert_mask;
399
400 man_id = i2c_smbus_read_byte_data(new_client,
401 LM63_REG_MAN_ID);
402 chip_id = i2c_smbus_read_byte_data(new_client,
403 LM63_REG_CHIP_ID);
404 reg_config1 = i2c_smbus_read_byte_data(new_client,
405 LM63_REG_CONFIG1);
406 reg_config2 = i2c_smbus_read_byte_data(new_client,
407 LM63_REG_CONFIG2);
408 reg_alert_status = i2c_smbus_read_byte_data(new_client,
409 LM63_REG_ALERT_STATUS);
410 reg_alert_mask = i2c_smbus_read_byte_data(new_client,
411 LM63_REG_ALERT_MASK);
412
413 if (man_id == 0x01 /* National Semiconductor */
414 && chip_id == 0x41 /* LM63 */
415 && (reg_config1 & 0x18) == 0x00
416 && (reg_config2 & 0xF8) == 0x00
417 && (reg_alert_status & 0x20) == 0x00
418 && (reg_alert_mask & 0xA4) == 0xA4) {
419 kind = lm63;
420 } else { /* failed */
421 dev_dbg(&adapter->dev, "Unsupported chip "
422 "(man_id=0x%02X, chip_id=0x%02X).\n",
423 man_id, chip_id);
424 goto exit_free;
425 }
426 }
427
428 strlcpy(new_client->name, "lm63", I2C_NAME_SIZE);
429 data->valid = 0;
430 init_MUTEX(&data->update_lock);
431
432 /* Tell the I2C layer a new client has arrived */
433 if ((err = i2c_attach_client(new_client)))
434 goto exit_free;
435
436 /* Initialize the LM63 chip */
437 lm63_init_client(new_client);
438
439 /* Register sysfs hooks */
440 if (data->config & 0x04) { /* tachometer enabled */
441 device_create_file(&new_client->dev,
442 &sensor_dev_attr_fan1_input.dev_attr);
443 device_create_file(&new_client->dev,
444 &sensor_dev_attr_fan1_min.dev_attr);
445 }
446 device_create_file(&new_client->dev, &dev_attr_pwm1);
447 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
448 device_create_file(&new_client->dev,
449 &sensor_dev_attr_temp1_input.dev_attr);
450 device_create_file(&new_client->dev,
451 &sensor_dev_attr_temp2_input.dev_attr);
452 device_create_file(&new_client->dev,
453 &sensor_dev_attr_temp2_min.dev_attr);
454 device_create_file(&new_client->dev,
455 &sensor_dev_attr_temp1_max.dev_attr);
456 device_create_file(&new_client->dev,
457 &sensor_dev_attr_temp2_max.dev_attr);
458 device_create_file(&new_client->dev,
459 &sensor_dev_attr_temp2_crit.dev_attr);
460 device_create_file(&new_client->dev, &dev_attr_temp2_crit_hyst);
461 device_create_file(&new_client->dev, &dev_attr_alarms);
462
463 return 0;
464
465exit_free:
466 kfree(data);
467exit:
468 return err;
469}
470
471/* Idealy we shouldn't have to initialize anything, since the BIOS
472 should have taken care of everything */
473static void lm63_init_client(struct i2c_client *client)
474{
475 struct lm63_data *data = i2c_get_clientdata(client);
476
477 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
478 data->config_fan = i2c_smbus_read_byte_data(client,
479 LM63_REG_CONFIG_FAN);
480
481 /* Start converting if needed */
482 if (data->config & 0x40) { /* standby */
483 dev_dbg(&client->dev, "Switching to operational mode");
484 data->config &= 0xA7;
485 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
486 data->config);
487 }
488
489 /* We may need pwm1_freq before ever updating the client data */
490 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
491 if (data->pwm1_freq == 0)
492 data->pwm1_freq = 1;
493
494 /* Show some debug info about the LM63 configuration */
495 dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
496 (data->config & 0x04) ? "tachometer input" :
497 "alert output");
498 dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
499 (data->config_fan & 0x08) ? "1.4" : "360",
500 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
501 dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
502 (data->config_fan & 0x10) ? "low" : "high",
503 (data->config_fan & 0x20) ? "manual" : "auto");
504}
505
506static int lm63_detach_client(struct i2c_client *client)
507{
508 int err;
509
510 if ((err = i2c_detach_client(client))) {
511 dev_err(&client->dev, "Client deregistration failed, "
512 "client not detached\n");
513 return err;
514 }
515
516 kfree(i2c_get_clientdata(client));
517 return 0;
518}
519
520static struct lm63_data *lm63_update_device(struct device *dev)
521{
522 struct i2c_client *client = to_i2c_client(dev);
523 struct lm63_data *data = i2c_get_clientdata(client);
524
525 down(&data->update_lock);
526
527 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
528 if (data->config & 0x04) { /* tachometer enabled */
529 /* order matters for fan1_input */
530 data->fan[0] = i2c_smbus_read_byte_data(client,
531 LM63_REG_TACH_COUNT_LSB) & 0xFC;
532 data->fan[0] |= i2c_smbus_read_byte_data(client,
533 LM63_REG_TACH_COUNT_MSB) << 8;
534 data->fan[1] = (i2c_smbus_read_byte_data(client,
535 LM63_REG_TACH_LIMIT_LSB) & 0xFC)
536 | (i2c_smbus_read_byte_data(client,
537 LM63_REG_TACH_LIMIT_MSB) << 8);
538 }
539
540 data->pwm1_freq = i2c_smbus_read_byte_data(client,
541 LM63_REG_PWM_FREQ);
542 if (data->pwm1_freq == 0)
543 data->pwm1_freq = 1;
544 data->pwm1_value = i2c_smbus_read_byte_data(client,
545 LM63_REG_PWM_VALUE);
546
547 data->temp8[0] = i2c_smbus_read_byte_data(client,
548 LM63_REG_LOCAL_TEMP);
549 data->temp8[1] = i2c_smbus_read_byte_data(client,
550 LM63_REG_LOCAL_HIGH);
551
552 /* order matters for temp2_input */
553 data->temp11[0] = i2c_smbus_read_byte_data(client,
554 LM63_REG_REMOTE_TEMP_MSB) << 8;
555 data->temp11[0] |= i2c_smbus_read_byte_data(client,
556 LM63_REG_REMOTE_TEMP_LSB);
557 data->temp11[1] = (i2c_smbus_read_byte_data(client,
558 LM63_REG_REMOTE_LOW_MSB) << 8)
559 | i2c_smbus_read_byte_data(client,
560 LM63_REG_REMOTE_LOW_LSB);
561 data->temp11[2] = (i2c_smbus_read_byte_data(client,
562 LM63_REG_REMOTE_HIGH_MSB) << 8)
563 | i2c_smbus_read_byte_data(client,
564 LM63_REG_REMOTE_HIGH_LSB);
565 data->temp8[2] = i2c_smbus_read_byte_data(client,
566 LM63_REG_REMOTE_TCRIT);
567 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
568 LM63_REG_REMOTE_TCRIT_HYST);
569
570 data->alarms = i2c_smbus_read_byte_data(client,
571 LM63_REG_ALERT_STATUS) & 0x7F;
572
573 data->last_updated = jiffies;
574 data->valid = 1;
575 }
576
577 up(&data->update_lock);
578
579 return data;
580}
581
582static int __init sensors_lm63_init(void)
583{
584 return i2c_add_driver(&lm63_driver);
585}
586
587static void __exit sensors_lm63_exit(void)
588{
589 i2c_del_driver(&lm63_driver);
590}
591
592MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
593MODULE_DESCRIPTION("LM63 driver");
594MODULE_LICENSE("GPL");
595
596module_init(sensors_lm63_init);
597module_exit(sensors_lm63_exit);
diff --git a/drivers/hwmon/lm75.c b/drivers/hwmon/lm75.c
new file mode 100644
index 000000000000..5be164ed278e
--- /dev/null
+++ b/drivers/hwmon/lm75.c
@@ -0,0 +1,296 @@
1/*
2 lm75.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/jiffies.h>
25#include <linux/i2c.h>
26#include <linux/i2c-sensor.h>
27#include "lm75.h"
28
29
30/* Addresses to scan */
31static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
32 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
33static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
34
35/* Insmod parameters */
36SENSORS_INSMOD_1(lm75);
37
38/* Many LM75 constants specified below */
39
40/* The LM75 registers */
41#define LM75_REG_TEMP 0x00
42#define LM75_REG_CONF 0x01
43#define LM75_REG_TEMP_HYST 0x02
44#define LM75_REG_TEMP_OS 0x03
45
46/* Each client has this additional data */
47struct lm75_data {
48 struct i2c_client client;
49 struct semaphore update_lock;
50 char valid; /* !=0 if following fields are valid */
51 unsigned long last_updated; /* In jiffies */
52 u16 temp_input; /* Register values */
53 u16 temp_max;
54 u16 temp_hyst;
55};
56
57static int lm75_attach_adapter(struct i2c_adapter *adapter);
58static int lm75_detect(struct i2c_adapter *adapter, int address, int kind);
59static void lm75_init_client(struct i2c_client *client);
60static int lm75_detach_client(struct i2c_client *client);
61static int lm75_read_value(struct i2c_client *client, u8 reg);
62static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
63static struct lm75_data *lm75_update_device(struct device *dev);
64
65
66/* This is the driver that will be inserted */
67static struct i2c_driver lm75_driver = {
68 .owner = THIS_MODULE,
69 .name = "lm75",
70 .id = I2C_DRIVERID_LM75,
71 .flags = I2C_DF_NOTIFY,
72 .attach_adapter = lm75_attach_adapter,
73 .detach_client = lm75_detach_client,
74};
75
76#define show(value) \
77static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
78{ \
79 struct lm75_data *data = lm75_update_device(dev); \
80 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->value)); \
81}
82show(temp_max);
83show(temp_hyst);
84show(temp_input);
85
86#define set(value, reg) \
87static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
88{ \
89 struct i2c_client *client = to_i2c_client(dev); \
90 struct lm75_data *data = i2c_get_clientdata(client); \
91 int temp = simple_strtoul(buf, NULL, 10); \
92 \
93 down(&data->update_lock); \
94 data->value = LM75_TEMP_TO_REG(temp); \
95 lm75_write_value(client, reg, data->value); \
96 up(&data->update_lock); \
97 return count; \
98}
99set(temp_max, LM75_REG_TEMP_OS);
100set(temp_hyst, LM75_REG_TEMP_HYST);
101
102static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
103static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst);
104static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL);
105
106static int lm75_attach_adapter(struct i2c_adapter *adapter)
107{
108 if (!(adapter->class & I2C_CLASS_HWMON))
109 return 0;
110 return i2c_detect(adapter, &addr_data, lm75_detect);
111}
112
113/* This function is called by i2c_detect */
114static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
115{
116 int i;
117 struct i2c_client *new_client;
118 struct lm75_data *data;
119 int err = 0;
120 const char *name = "";
121
122 /* Make sure we aren't probing the ISA bus!! This is just a safety check
123 at this moment; i2c_detect really won't call us. */
124#ifdef DEBUG
125 if (i2c_is_isa_adapter(adapter)) {
126 dev_dbg(&adapter->dev,
127 "lm75_detect called for an ISA bus adapter?!?\n");
128 goto exit;
129 }
130#endif
131
132 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
133 I2C_FUNC_SMBUS_WORD_DATA))
134 goto exit;
135
136 /* OK. For now, we presume we have a valid client. We now create the
137 client structure, even though we cannot fill it completely yet.
138 But it allows us to access lm75_{read,write}_value. */
139 if (!(data = kmalloc(sizeof(struct lm75_data), GFP_KERNEL))) {
140 err = -ENOMEM;
141 goto exit;
142 }
143 memset(data, 0, sizeof(struct lm75_data));
144
145 new_client = &data->client;
146 i2c_set_clientdata(new_client, data);
147 new_client->addr = address;
148 new_client->adapter = adapter;
149 new_client->driver = &lm75_driver;
150 new_client->flags = 0;
151
152 /* Now, we do the remaining detection. There is no identification-
153 dedicated register so we have to rely on several tricks:
154 unused bits, registers cycling over 8-address boundaries,
155 addresses 0x04-0x07 returning the last read value.
156 The cycling+unused addresses combination is not tested,
157 since it would significantly slow the detection down and would
158 hardly add any value. */
159 if (kind < 0) {
160 int cur, conf, hyst, os;
161
162 /* Unused addresses */
163 cur = i2c_smbus_read_word_data(new_client, 0);
164 conf = i2c_smbus_read_byte_data(new_client, 1);
165 hyst = i2c_smbus_read_word_data(new_client, 2);
166 if (i2c_smbus_read_word_data(new_client, 4) != hyst
167 || i2c_smbus_read_word_data(new_client, 5) != hyst
168 || i2c_smbus_read_word_data(new_client, 6) != hyst
169 || i2c_smbus_read_word_data(new_client, 7) != hyst)
170 goto exit_free;
171 os = i2c_smbus_read_word_data(new_client, 3);
172 if (i2c_smbus_read_word_data(new_client, 4) != os
173 || i2c_smbus_read_word_data(new_client, 5) != os
174 || i2c_smbus_read_word_data(new_client, 6) != os
175 || i2c_smbus_read_word_data(new_client, 7) != os)
176 goto exit_free;
177
178 /* Unused bits */
179 if (conf & 0xe0)
180 goto exit_free;
181
182 /* Addresses cycling */
183 for (i = 8; i < 0xff; i += 8)
184 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
185 || i2c_smbus_read_word_data(new_client, i + 2) != hyst
186 || i2c_smbus_read_word_data(new_client, i + 3) != os)
187 goto exit_free;
188 }
189
190 /* Determine the chip type - only one kind supported! */
191 if (kind <= 0)
192 kind = lm75;
193
194 if (kind == lm75) {
195 name = "lm75";
196 }
197
198 /* Fill in the remaining client fields and put it into the global list */
199 strlcpy(new_client->name, name, I2C_NAME_SIZE);
200 data->valid = 0;
201 init_MUTEX(&data->update_lock);
202
203 /* Tell the I2C layer a new client has arrived */
204 if ((err = i2c_attach_client(new_client)))
205 goto exit_free;
206
207 /* Initialize the LM75 chip */
208 lm75_init_client(new_client);
209
210 /* Register sysfs hooks */
211 device_create_file(&new_client->dev, &dev_attr_temp1_max);
212 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
213 device_create_file(&new_client->dev, &dev_attr_temp1_input);
214
215 return 0;
216
217exit_free:
218 kfree(data);
219exit:
220 return err;
221}
222
223static int lm75_detach_client(struct i2c_client *client)
224{
225 i2c_detach_client(client);
226 kfree(i2c_get_clientdata(client));
227 return 0;
228}
229
230/* All registers are word-sized, except for the configuration register.
231 LM75 uses a high-byte first convention, which is exactly opposite to
232 the usual practice. */
233static int lm75_read_value(struct i2c_client *client, u8 reg)
234{
235 if (reg == LM75_REG_CONF)
236 return i2c_smbus_read_byte_data(client, reg);
237 else
238 return swab16(i2c_smbus_read_word_data(client, reg));
239}
240
241/* All registers are word-sized, except for the configuration register.
242 LM75 uses a high-byte first convention, which is exactly opposite to
243 the usual practice. */
244static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
245{
246 if (reg == LM75_REG_CONF)
247 return i2c_smbus_write_byte_data(client, reg, value);
248 else
249 return i2c_smbus_write_word_data(client, reg, swab16(value));
250}
251
252static void lm75_init_client(struct i2c_client *client)
253{
254 /* Initialize the LM75 chip */
255 lm75_write_value(client, LM75_REG_CONF, 0);
256}
257
258static struct lm75_data *lm75_update_device(struct device *dev)
259{
260 struct i2c_client *client = to_i2c_client(dev);
261 struct lm75_data *data = i2c_get_clientdata(client);
262
263 down(&data->update_lock);
264
265 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
266 || !data->valid) {
267 dev_dbg(&client->dev, "Starting lm75 update\n");
268
269 data->temp_input = lm75_read_value(client, LM75_REG_TEMP);
270 data->temp_max = lm75_read_value(client, LM75_REG_TEMP_OS);
271 data->temp_hyst = lm75_read_value(client, LM75_REG_TEMP_HYST);
272 data->last_updated = jiffies;
273 data->valid = 1;
274 }
275
276 up(&data->update_lock);
277
278 return data;
279}
280
281static int __init sensors_lm75_init(void)
282{
283 return i2c_add_driver(&lm75_driver);
284}
285
286static void __exit sensors_lm75_exit(void)
287{
288 i2c_del_driver(&lm75_driver);
289}
290
291MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
292MODULE_DESCRIPTION("LM75 driver");
293MODULE_LICENSE("GPL");
294
295module_init(sensors_lm75_init);
296module_exit(sensors_lm75_exit);
diff --git a/drivers/hwmon/lm75.h b/drivers/hwmon/lm75.h
new file mode 100644
index 000000000000..63e3f2fb4c21
--- /dev/null
+++ b/drivers/hwmon/lm75.h
@@ -0,0 +1,49 @@
1/*
2 lm75.h - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 2003 Mark M. Hoffman <mhoffman@lightlink.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21/*
22 This file contains common code for encoding/decoding LM75 type
23 temperature readings, which are emulated by many of the chips
24 we support. As the user is unlikely to load more than one driver
25 which contains this code, we don't worry about the wasted space.
26*/
27
28#include <linux/i2c-sensor.h>
29
30/* straight from the datasheet */
31#define LM75_TEMP_MIN (-55000)
32#define LM75_TEMP_MAX 125000
33
34/* TEMP: 0.001C/bit (-55C to +125C)
35 REG: (0.5C/bit, two's complement) << 7 */
36static inline u16 LM75_TEMP_TO_REG(int temp)
37{
38 int ntemp = SENSORS_LIMIT(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
39 ntemp += (ntemp<0 ? -250 : 250);
40 return (u16)((ntemp / 500) << 7);
41}
42
43static inline int LM75_TEMP_FROM_REG(u16 reg)
44{
45 /* use integer division instead of equivalent right shift to
46 guarantee arithmetic shift and preserve the sign */
47 return ((s16)reg / 128) * 500;
48}
49
diff --git a/drivers/hwmon/lm77.c b/drivers/hwmon/lm77.c
new file mode 100644
index 000000000000..b98f44952997
--- /dev/null
+++ b/drivers/hwmon/lm77.c
@@ -0,0 +1,420 @@
1/*
2 lm77.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4
5 Copyright (c) 2004 Andras BALI <drewie@freemail.hu>
6
7 Heavily based on lm75.c by Frodo Looijaard <frodol@dds.nl>. The LM77
8 is a temperature sensor and thermal window comparator with 0.5 deg
9 resolution made by National Semiconductor. Complete datasheet can be
10 obtained at their site:
11 http://www.national.com/pf/LM/LM77.html
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26*/
27
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/slab.h>
31#include <linux/jiffies.h>
32#include <linux/i2c.h>
33#include <linux/i2c-sensor.h>
34
35
36/* Addresses to scan */
37static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, I2C_CLIENT_END };
38static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
39
40/* Insmod parameters */
41SENSORS_INSMOD_1(lm77);
42
43/* The LM77 registers */
44#define LM77_REG_TEMP 0x00
45#define LM77_REG_CONF 0x01
46#define LM77_REG_TEMP_HYST 0x02
47#define LM77_REG_TEMP_CRIT 0x03
48#define LM77_REG_TEMP_MIN 0x04
49#define LM77_REG_TEMP_MAX 0x05
50
51/* Each client has this additional data */
52struct lm77_data {
53 struct i2c_client client;
54 struct semaphore update_lock;
55 char valid;
56 unsigned long last_updated; /* In jiffies */
57 int temp_input; /* Temperatures */
58 int temp_crit;
59 int temp_min;
60 int temp_max;
61 int temp_hyst;
62 u8 alarms;
63};
64
65static int lm77_attach_adapter(struct i2c_adapter *adapter);
66static int lm77_detect(struct i2c_adapter *adapter, int address, int kind);
67static void lm77_init_client(struct i2c_client *client);
68static int lm77_detach_client(struct i2c_client *client);
69static u16 lm77_read_value(struct i2c_client *client, u8 reg);
70static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value);
71
72static struct lm77_data *lm77_update_device(struct device *dev);
73
74
75/* This is the driver that will be inserted */
76static struct i2c_driver lm77_driver = {
77 .owner = THIS_MODULE,
78 .name = "lm77",
79 .flags = I2C_DF_NOTIFY,
80 .attach_adapter = lm77_attach_adapter,
81 .detach_client = lm77_detach_client,
82};
83
84/* straight from the datasheet */
85#define LM77_TEMP_MIN (-55000)
86#define LM77_TEMP_MAX 125000
87
88/* In the temperature registers, the low 3 bits are not part of the
89 temperature values; they are the status bits. */
90static inline u16 LM77_TEMP_TO_REG(int temp)
91{
92 int ntemp = SENSORS_LIMIT(temp, LM77_TEMP_MIN, LM77_TEMP_MAX);
93 return (u16)((ntemp / 500) * 8);
94}
95
96static inline int LM77_TEMP_FROM_REG(u16 reg)
97{
98 return ((int)reg / 8) * 500;
99}
100
101/* sysfs stuff */
102
103/* read routines for temperature limits */
104#define show(value) \
105static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
106{ \
107 struct lm77_data *data = lm77_update_device(dev); \
108 return sprintf(buf, "%d\n", data->value); \
109}
110
111show(temp_input);
112show(temp_crit);
113show(temp_min);
114show(temp_max);
115show(alarms);
116
117/* read routines for hysteresis values */
118static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute *attr, char *buf)
119{
120 struct lm77_data *data = lm77_update_device(dev);
121 return sprintf(buf, "%d\n", data->temp_crit - data->temp_hyst);
122}
123static ssize_t show_temp_min_hyst(struct device *dev, struct device_attribute *attr, char *buf)
124{
125 struct lm77_data *data = lm77_update_device(dev);
126 return sprintf(buf, "%d\n", data->temp_min + data->temp_hyst);
127}
128static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute *attr, char *buf)
129{
130 struct lm77_data *data = lm77_update_device(dev);
131 return sprintf(buf, "%d\n", data->temp_max - data->temp_hyst);
132}
133
134/* write routines */
135#define set(value, reg) \
136static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
137{ \
138 struct i2c_client *client = to_i2c_client(dev); \
139 struct lm77_data *data = i2c_get_clientdata(client); \
140 long val = simple_strtoul(buf, NULL, 10); \
141 \
142 down(&data->update_lock); \
143 data->value = val; \
144 lm77_write_value(client, reg, LM77_TEMP_TO_REG(data->value)); \
145 up(&data->update_lock); \
146 return count; \
147}
148
149set(temp_min, LM77_REG_TEMP_MIN);
150set(temp_max, LM77_REG_TEMP_MAX);
151
152/* hysteresis is stored as a relative value on the chip, so it has to be
153 converted first */
154static ssize_t set_temp_crit_hyst(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
155{
156 struct i2c_client *client = to_i2c_client(dev);
157 struct lm77_data *data = i2c_get_clientdata(client);
158 unsigned long val = simple_strtoul(buf, NULL, 10);
159
160 down(&data->update_lock);
161 data->temp_hyst = data->temp_crit - val;
162 lm77_write_value(client, LM77_REG_TEMP_HYST,
163 LM77_TEMP_TO_REG(data->temp_hyst));
164 up(&data->update_lock);
165 return count;
166}
167
168/* preserve hysteresis when setting T_crit */
169static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
170{
171 struct i2c_client *client = to_i2c_client(dev);
172 struct lm77_data *data = i2c_get_clientdata(client);
173 long val = simple_strtoul(buf, NULL, 10);
174 int oldcrithyst;
175
176 down(&data->update_lock);
177 oldcrithyst = data->temp_crit - data->temp_hyst;
178 data->temp_crit = val;
179 data->temp_hyst = data->temp_crit - oldcrithyst;
180 lm77_write_value(client, LM77_REG_TEMP_CRIT,
181 LM77_TEMP_TO_REG(data->temp_crit));
182 lm77_write_value(client, LM77_REG_TEMP_HYST,
183 LM77_TEMP_TO_REG(data->temp_hyst));
184 up(&data->update_lock);
185 return count;
186}
187
188static DEVICE_ATTR(temp1_input, S_IRUGO,
189 show_temp_input, NULL);
190static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO,
191 show_temp_crit, set_temp_crit);
192static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
193 show_temp_min, set_temp_min);
194static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
195 show_temp_max, set_temp_max);
196
197static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO,
198 show_temp_crit_hyst, set_temp_crit_hyst);
199static DEVICE_ATTR(temp1_min_hyst, S_IRUGO,
200 show_temp_min_hyst, NULL);
201static DEVICE_ATTR(temp1_max_hyst, S_IRUGO,
202 show_temp_max_hyst, NULL);
203
204static DEVICE_ATTR(alarms, S_IRUGO,
205 show_alarms, NULL);
206
207static int lm77_attach_adapter(struct i2c_adapter *adapter)
208{
209 if (!(adapter->class & I2C_CLASS_HWMON))
210 return 0;
211 return i2c_detect(adapter, &addr_data, lm77_detect);
212}
213
214/* This function is called by i2c_detect */
215static int lm77_detect(struct i2c_adapter *adapter, int address, int kind)
216{
217 struct i2c_client *new_client;
218 struct lm77_data *data;
219 int err = 0;
220 const char *name = "";
221
222 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
223 I2C_FUNC_SMBUS_WORD_DATA))
224 goto exit;
225
226 /* OK. For now, we presume we have a valid client. We now create the
227 client structure, even though we cannot fill it completely yet.
228 But it allows us to access lm77_{read,write}_value. */
229 if (!(data = kmalloc(sizeof(struct lm77_data), GFP_KERNEL))) {
230 err = -ENOMEM;
231 goto exit;
232 }
233 memset(data, 0, sizeof(struct lm77_data));
234
235 new_client = &data->client;
236 i2c_set_clientdata(new_client, data);
237 new_client->addr = address;
238 new_client->adapter = adapter;
239 new_client->driver = &lm77_driver;
240 new_client->flags = 0;
241
242 /* Here comes the remaining detection. Since the LM77 has no
243 register dedicated to identification, we have to rely on the
244 following tricks:
245
246 1. the high 4 bits represent the sign and thus they should
247 always be the same
248 2. the high 3 bits are unused in the configuration register
249 3. addresses 0x06 and 0x07 return the last read value
250 4. registers cycling over 8-address boundaries
251
252 Word-sized registers are high-byte first. */
253 if (kind < 0) {
254 int i, cur, conf, hyst, crit, min, max;
255
256 /* addresses cycling */
257 cur = i2c_smbus_read_word_data(new_client, 0);
258 conf = i2c_smbus_read_byte_data(new_client, 1);
259 hyst = i2c_smbus_read_word_data(new_client, 2);
260 crit = i2c_smbus_read_word_data(new_client, 3);
261 min = i2c_smbus_read_word_data(new_client, 4);
262 max = i2c_smbus_read_word_data(new_client, 5);
263 for (i = 8; i <= 0xff; i += 8)
264 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
265 || i2c_smbus_read_word_data(new_client, i + 2) != hyst
266 || i2c_smbus_read_word_data(new_client, i + 3) != crit
267 || i2c_smbus_read_word_data(new_client, i + 4) != min
268 || i2c_smbus_read_word_data(new_client, i + 5) != max)
269 goto exit_free;
270
271 /* sign bits */
272 if (((cur & 0x00f0) != 0xf0 && (cur & 0x00f0) != 0x0)
273 || ((hyst & 0x00f0) != 0xf0 && (hyst & 0x00f0) != 0x0)
274 || ((crit & 0x00f0) != 0xf0 && (crit & 0x00f0) != 0x0)
275 || ((min & 0x00f0) != 0xf0 && (min & 0x00f0) != 0x0)
276 || ((max & 0x00f0) != 0xf0 && (max & 0x00f0) != 0x0))
277 goto exit_free;
278
279 /* unused bits */
280 if (conf & 0xe0)
281 goto exit_free;
282
283 /* 0x06 and 0x07 return the last read value */
284 cur = i2c_smbus_read_word_data(new_client, 0);
285 if (i2c_smbus_read_word_data(new_client, 6) != cur
286 || i2c_smbus_read_word_data(new_client, 7) != cur)
287 goto exit_free;
288 hyst = i2c_smbus_read_word_data(new_client, 2);
289 if (i2c_smbus_read_word_data(new_client, 6) != hyst
290 || i2c_smbus_read_word_data(new_client, 7) != hyst)
291 goto exit_free;
292 min = i2c_smbus_read_word_data(new_client, 4);
293 if (i2c_smbus_read_word_data(new_client, 6) != min
294 || i2c_smbus_read_word_data(new_client, 7) != min)
295 goto exit_free;
296
297 }
298
299 /* Determine the chip type - only one kind supported! */
300 if (kind <= 0)
301 kind = lm77;
302
303 if (kind == lm77) {
304 name = "lm77";
305 }
306
307 /* Fill in the remaining client fields and put it into the global list */
308 strlcpy(new_client->name, name, I2C_NAME_SIZE);
309 data->valid = 0;
310 init_MUTEX(&data->update_lock);
311
312 /* Tell the I2C layer a new client has arrived */
313 if ((err = i2c_attach_client(new_client)))
314 goto exit_free;
315
316 /* Initialize the LM77 chip */
317 lm77_init_client(new_client);
318
319 /* Register sysfs hooks */
320 device_create_file(&new_client->dev, &dev_attr_temp1_input);
321 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
322 device_create_file(&new_client->dev, &dev_attr_temp1_min);
323 device_create_file(&new_client->dev, &dev_attr_temp1_max);
324 device_create_file(&new_client->dev, &dev_attr_temp1_crit_hyst);
325 device_create_file(&new_client->dev, &dev_attr_temp1_min_hyst);
326 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
327 device_create_file(&new_client->dev, &dev_attr_alarms);
328 return 0;
329
330exit_free:
331 kfree(data);
332exit:
333 return err;
334}
335
336static int lm77_detach_client(struct i2c_client *client)
337{
338 i2c_detach_client(client);
339 kfree(i2c_get_clientdata(client));
340 return 0;
341}
342
343/* All registers are word-sized, except for the configuration register.
344 The LM77 uses the high-byte first convention. */
345static u16 lm77_read_value(struct i2c_client *client, u8 reg)
346{
347 if (reg == LM77_REG_CONF)
348 return i2c_smbus_read_byte_data(client, reg);
349 else
350 return swab16(i2c_smbus_read_word_data(client, reg));
351}
352
353static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value)
354{
355 if (reg == LM77_REG_CONF)
356 return i2c_smbus_write_byte_data(client, reg, value);
357 else
358 return i2c_smbus_write_word_data(client, reg, swab16(value));
359}
360
361static void lm77_init_client(struct i2c_client *client)
362{
363 /* Initialize the LM77 chip - turn off shutdown mode */
364 int conf = lm77_read_value(client, LM77_REG_CONF);
365 if (conf & 1)
366 lm77_write_value(client, LM77_REG_CONF, conf & 0xfe);
367}
368
369static struct lm77_data *lm77_update_device(struct device *dev)
370{
371 struct i2c_client *client = to_i2c_client(dev);
372 struct lm77_data *data = i2c_get_clientdata(client);
373
374 down(&data->update_lock);
375
376 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
377 || !data->valid) {
378 dev_dbg(&client->dev, "Starting lm77 update\n");
379 data->temp_input =
380 LM77_TEMP_FROM_REG(lm77_read_value(client,
381 LM77_REG_TEMP));
382 data->temp_hyst =
383 LM77_TEMP_FROM_REG(lm77_read_value(client,
384 LM77_REG_TEMP_HYST));
385 data->temp_crit =
386 LM77_TEMP_FROM_REG(lm77_read_value(client,
387 LM77_REG_TEMP_CRIT));
388 data->temp_min =
389 LM77_TEMP_FROM_REG(lm77_read_value(client,
390 LM77_REG_TEMP_MIN));
391 data->temp_max =
392 LM77_TEMP_FROM_REG(lm77_read_value(client,
393 LM77_REG_TEMP_MAX));
394 data->alarms =
395 lm77_read_value(client, LM77_REG_TEMP) & 0x0007;
396 data->last_updated = jiffies;
397 data->valid = 1;
398 }
399
400 up(&data->update_lock);
401
402 return data;
403}
404
405static int __init sensors_lm77_init(void)
406{
407 return i2c_add_driver(&lm77_driver);
408}
409
410static void __exit sensors_lm77_exit(void)
411{
412 i2c_del_driver(&lm77_driver);
413}
414
415MODULE_AUTHOR("Andras BALI <drewie@freemail.hu>");
416MODULE_DESCRIPTION("LM77 driver");
417MODULE_LICENSE("GPL");
418
419module_init(sensors_lm77_init);
420module_exit(sensors_lm77_exit);
diff --git a/drivers/hwmon/lm78.c b/drivers/hwmon/lm78.c
new file mode 100644
index 000000000000..29241469dcba
--- /dev/null
+++ b/drivers/hwmon/lm78.c
@@ -0,0 +1,795 @@
1/*
2 lm78.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/jiffies.h>
25#include <linux/i2c.h>
26#include <linux/i2c-sensor.h>
27#include <asm/io.h>
28
29/* Addresses to scan */
30static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24,
31 0x25, 0x26, 0x27, 0x28, 0x29,
32 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
33 0x2f, I2C_CLIENT_END };
34static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
35
36/* Insmod parameters */
37SENSORS_INSMOD_3(lm78, lm78j, lm79);
38
39/* Many LM78 constants specified below */
40
41/* Length of ISA address segment */
42#define LM78_EXTENT 8
43
44/* Where are the ISA address/data registers relative to the base address */
45#define LM78_ADDR_REG_OFFSET 5
46#define LM78_DATA_REG_OFFSET 6
47
48/* The LM78 registers */
49#define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2)
50#define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2)
51#define LM78_REG_IN(nr) (0x20 + (nr))
52
53#define LM78_REG_FAN_MIN(nr) (0x3b + (nr))
54#define LM78_REG_FAN(nr) (0x28 + (nr))
55
56#define LM78_REG_TEMP 0x27
57#define LM78_REG_TEMP_OVER 0x39
58#define LM78_REG_TEMP_HYST 0x3a
59
60#define LM78_REG_ALARM1 0x41
61#define LM78_REG_ALARM2 0x42
62
63#define LM78_REG_VID_FANDIV 0x47
64
65#define LM78_REG_CONFIG 0x40
66#define LM78_REG_CHIPID 0x49
67#define LM78_REG_I2C_ADDR 0x48
68
69
70/* Conversions. Rounding and limit checking is only done on the TO_REG
71 variants. */
72
73/* IN: mV, (0V to 4.08V)
74 REG: 16mV/bit */
75static inline u8 IN_TO_REG(unsigned long val)
76{
77 unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
78 return (nval + 8) / 16;
79}
80#define IN_FROM_REG(val) ((val) * 16)
81
82static inline u8 FAN_TO_REG(long rpm, int div)
83{
84 if (rpm <= 0)
85 return 255;
86 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
87}
88
89static inline int FAN_FROM_REG(u8 val, int div)
90{
91 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
92}
93
94/* TEMP: mC (-128C to +127C)
95 REG: 1C/bit, two's complement */
96static inline s8 TEMP_TO_REG(int val)
97{
98 int nval = SENSORS_LIMIT(val, -128000, 127000) ;
99 return nval<0 ? (nval-500)/1000 : (nval+500)/1000;
100}
101
102static inline int TEMP_FROM_REG(s8 val)
103{
104 return val * 1000;
105}
106
107/* VID: mV
108 REG: (see doc/vid) */
109static inline int VID_FROM_REG(u8 val)
110{
111 return val==0x1f ? 0 : val>=0x10 ? 5100-val*100 : 2050-val*50;
112}
113
114#define DIV_FROM_REG(val) (1 << (val))
115
116/* There are some complications in a module like this. First off, LM78 chips
117 may be both present on the SMBus and the ISA bus, and we have to handle
118 those cases separately at some places. Second, there might be several
119 LM78 chips available (well, actually, that is probably never done; but
120 it is a clean illustration of how to handle a case like that). Finally,
121 a specific chip may be attached to *both* ISA and SMBus, and we would
122 not like to detect it double. Fortunately, in the case of the LM78 at
123 least, a register tells us what SMBus address we are on, so that helps
124 a bit - except if there could be more than one SMBus. Groan. No solution
125 for this yet. */
126
127/* This module may seem overly long and complicated. In fact, it is not so
128 bad. Quite a lot of bookkeeping is done. A real driver can often cut
129 some corners. */
130
131/* For each registered LM78, we need to keep some data in memory. That
132 data is pointed to by lm78_list[NR]->data. The structure itself is
133 dynamically allocated, at the same time when a new lm78 client is
134 allocated. */
135struct lm78_data {
136 struct i2c_client client;
137 struct semaphore lock;
138 enum chips type;
139
140 struct semaphore update_lock;
141 char valid; /* !=0 if following fields are valid */
142 unsigned long last_updated; /* In jiffies */
143
144 u8 in[7]; /* Register value */
145 u8 in_max[7]; /* Register value */
146 u8 in_min[7]; /* Register value */
147 u8 fan[3]; /* Register value */
148 u8 fan_min[3]; /* Register value */
149 s8 temp; /* Register value */
150 s8 temp_over; /* Register value */
151 s8 temp_hyst; /* Register value */
152 u8 fan_div[3]; /* Register encoding, shifted right */
153 u8 vid; /* Register encoding, combined */
154 u16 alarms; /* Register encoding, combined */
155};
156
157
158static int lm78_attach_adapter(struct i2c_adapter *adapter);
159static int lm78_detect(struct i2c_adapter *adapter, int address, int kind);
160static int lm78_detach_client(struct i2c_client *client);
161
162static int lm78_read_value(struct i2c_client *client, u8 register);
163static int lm78_write_value(struct i2c_client *client, u8 register, u8 value);
164static struct lm78_data *lm78_update_device(struct device *dev);
165static void lm78_init_client(struct i2c_client *client);
166
167
168static struct i2c_driver lm78_driver = {
169 .owner = THIS_MODULE,
170 .name = "lm78",
171 .id = I2C_DRIVERID_LM78,
172 .flags = I2C_DF_NOTIFY,
173 .attach_adapter = lm78_attach_adapter,
174 .detach_client = lm78_detach_client,
175};
176
177/* 7 Voltages */
178static ssize_t show_in(struct device *dev, char *buf, int nr)
179{
180 struct lm78_data *data = lm78_update_device(dev);
181 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
182}
183
184static ssize_t show_in_min(struct device *dev, char *buf, int nr)
185{
186 struct lm78_data *data = lm78_update_device(dev);
187 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
188}
189
190static ssize_t show_in_max(struct device *dev, char *buf, int nr)
191{
192 struct lm78_data *data = lm78_update_device(dev);
193 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
194}
195
196static ssize_t set_in_min(struct device *dev, const char *buf,
197 size_t count, int nr)
198{
199 struct i2c_client *client = to_i2c_client(dev);
200 struct lm78_data *data = i2c_get_clientdata(client);
201 unsigned long val = simple_strtoul(buf, NULL, 10);
202
203 down(&data->update_lock);
204 data->in_min[nr] = IN_TO_REG(val);
205 lm78_write_value(client, LM78_REG_IN_MIN(nr), data->in_min[nr]);
206 up(&data->update_lock);
207 return count;
208}
209
210static ssize_t set_in_max(struct device *dev, const char *buf,
211 size_t count, int nr)
212{
213 struct i2c_client *client = to_i2c_client(dev);
214 struct lm78_data *data = i2c_get_clientdata(client);
215 unsigned long val = simple_strtoul(buf, NULL, 10);
216
217 down(&data->update_lock);
218 data->in_max[nr] = IN_TO_REG(val);
219 lm78_write_value(client, LM78_REG_IN_MAX(nr), data->in_max[nr]);
220 up(&data->update_lock);
221 return count;
222}
223
224#define show_in_offset(offset) \
225static ssize_t \
226 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
227{ \
228 return show_in(dev, buf, offset); \
229} \
230static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
231 show_in##offset, NULL); \
232static ssize_t \
233 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
234{ \
235 return show_in_min(dev, buf, offset); \
236} \
237static ssize_t \
238 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
239{ \
240 return show_in_max(dev, buf, offset); \
241} \
242static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
243 const char *buf, size_t count) \
244{ \
245 return set_in_min(dev, buf, count, offset); \
246} \
247static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
248 const char *buf, size_t count) \
249{ \
250 return set_in_max(dev, buf, count, offset); \
251} \
252static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
253 show_in##offset##_min, set_in##offset##_min); \
254static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
255 show_in##offset##_max, set_in##offset##_max);
256
257show_in_offset(0);
258show_in_offset(1);
259show_in_offset(2);
260show_in_offset(3);
261show_in_offset(4);
262show_in_offset(5);
263show_in_offset(6);
264
265/* Temperature */
266static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
267{
268 struct lm78_data *data = lm78_update_device(dev);
269 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
270}
271
272static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr, char *buf)
273{
274 struct lm78_data *data = lm78_update_device(dev);
275 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
276}
277
278static ssize_t set_temp_over(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
279{
280 struct i2c_client *client = to_i2c_client(dev);
281 struct lm78_data *data = i2c_get_clientdata(client);
282 long val = simple_strtol(buf, NULL, 10);
283
284 down(&data->update_lock);
285 data->temp_over = TEMP_TO_REG(val);
286 lm78_write_value(client, LM78_REG_TEMP_OVER, data->temp_over);
287 up(&data->update_lock);
288 return count;
289}
290
291static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
292{
293 struct lm78_data *data = lm78_update_device(dev);
294 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
295}
296
297static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
298{
299 struct i2c_client *client = to_i2c_client(dev);
300 struct lm78_data *data = i2c_get_clientdata(client);
301 long val = simple_strtol(buf, NULL, 10);
302
303 down(&data->update_lock);
304 data->temp_hyst = TEMP_TO_REG(val);
305 lm78_write_value(client, LM78_REG_TEMP_HYST, data->temp_hyst);
306 up(&data->update_lock);
307 return count;
308}
309
310static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
311static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
312 show_temp_over, set_temp_over);
313static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
314 show_temp_hyst, set_temp_hyst);
315
316/* 3 Fans */
317static ssize_t show_fan(struct device *dev, char *buf, int nr)
318{
319 struct lm78_data *data = lm78_update_device(dev);
320 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
321 DIV_FROM_REG(data->fan_div[nr])) );
322}
323
324static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
325{
326 struct lm78_data *data = lm78_update_device(dev);
327 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
328 DIV_FROM_REG(data->fan_div[nr])) );
329}
330
331static ssize_t set_fan_min(struct device *dev, const char *buf,
332 size_t count, int nr)
333{
334 struct i2c_client *client = to_i2c_client(dev);
335 struct lm78_data *data = i2c_get_clientdata(client);
336 unsigned long val = simple_strtoul(buf, NULL, 10);
337
338 down(&data->update_lock);
339 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
340 lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
341 up(&data->update_lock);
342 return count;
343}
344
345static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
346{
347 struct lm78_data *data = lm78_update_device(dev);
348 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
349}
350
351/* Note: we save and restore the fan minimum here, because its value is
352 determined in part by the fan divisor. This follows the principle of
353 least suprise; the user doesn't expect the fan minimum to change just
354 because the divisor changed. */
355static ssize_t set_fan_div(struct device *dev, const char *buf,
356 size_t count, int nr)
357{
358 struct i2c_client *client = to_i2c_client(dev);
359 struct lm78_data *data = i2c_get_clientdata(client);
360 unsigned long val = simple_strtoul(buf, NULL, 10);
361 unsigned long min;
362 u8 reg;
363
364 down(&data->update_lock);
365 min = FAN_FROM_REG(data->fan_min[nr],
366 DIV_FROM_REG(data->fan_div[nr]));
367
368 switch (val) {
369 case 1: data->fan_div[nr] = 0; break;
370 case 2: data->fan_div[nr] = 1; break;
371 case 4: data->fan_div[nr] = 2; break;
372 case 8: data->fan_div[nr] = 3; break;
373 default:
374 dev_err(&client->dev, "fan_div value %ld not "
375 "supported. Choose one of 1, 2, 4 or 8!\n", val);
376 up(&data->update_lock);
377 return -EINVAL;
378 }
379
380 reg = lm78_read_value(client, LM78_REG_VID_FANDIV);
381 switch (nr) {
382 case 0:
383 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
384 break;
385 case 1:
386 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
387 break;
388 }
389 lm78_write_value(client, LM78_REG_VID_FANDIV, reg);
390
391 data->fan_min[nr] =
392 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
393 lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
394 up(&data->update_lock);
395
396 return count;
397}
398
399#define show_fan_offset(offset) \
400static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
401{ \
402 return show_fan(dev, buf, offset - 1); \
403} \
404static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
405{ \
406 return show_fan_min(dev, buf, offset - 1); \
407} \
408static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
409{ \
410 return show_fan_div(dev, buf, offset - 1); \
411} \
412static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
413 const char *buf, size_t count) \
414{ \
415 return set_fan_min(dev, buf, count, offset - 1); \
416} \
417static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
418static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
419 show_fan_##offset##_min, set_fan_##offset##_min);
420
421static ssize_t set_fan_1_div(struct device *dev, struct device_attribute *attr, const char *buf,
422 size_t count)
423{
424 return set_fan_div(dev, buf, count, 0) ;
425}
426
427static ssize_t set_fan_2_div(struct device *dev, struct device_attribute *attr, const char *buf,
428 size_t count)
429{
430 return set_fan_div(dev, buf, count, 1) ;
431}
432
433show_fan_offset(1);
434show_fan_offset(2);
435show_fan_offset(3);
436
437/* Fan 3 divisor is locked in H/W */
438static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
439 show_fan_1_div, set_fan_1_div);
440static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
441 show_fan_2_div, set_fan_2_div);
442static DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_3_div, NULL);
443
444/* VID */
445static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
446{
447 struct lm78_data *data = lm78_update_device(dev);
448 return sprintf(buf, "%d\n", VID_FROM_REG(data->vid));
449}
450static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
451
452/* Alarms */
453static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
454{
455 struct lm78_data *data = lm78_update_device(dev);
456 return sprintf(buf, "%u\n", data->alarms);
457}
458static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
459
460/* This function is called when:
461 * lm78_driver is inserted (when this module is loaded), for each
462 available adapter
463 * when a new adapter is inserted (and lm78_driver is still present) */
464static int lm78_attach_adapter(struct i2c_adapter *adapter)
465{
466 if (!(adapter->class & I2C_CLASS_HWMON))
467 return 0;
468 return i2c_detect(adapter, &addr_data, lm78_detect);
469}
470
471/* This function is called by i2c_detect */
472int lm78_detect(struct i2c_adapter *adapter, int address, int kind)
473{
474 int i, err;
475 struct i2c_client *new_client;
476 struct lm78_data *data;
477 const char *client_name = "";
478 int is_isa = i2c_is_isa_adapter(adapter);
479
480 if (!is_isa &&
481 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
482 err = -ENODEV;
483 goto ERROR0;
484 }
485
486 /* Reserve the ISA region */
487 if (is_isa)
488 if (!request_region(address, LM78_EXTENT, lm78_driver.name)) {
489 err = -EBUSY;
490 goto ERROR0;
491 }
492
493 /* Probe whether there is anything available on this address. Already
494 done for SMBus clients */
495 if (kind < 0) {
496 if (is_isa) {
497
498#define REALLY_SLOW_IO
499 /* We need the timeouts for at least some LM78-like
500 chips. But only if we read 'undefined' registers. */
501 i = inb_p(address + 1);
502 if (inb_p(address + 2) != i) {
503 err = -ENODEV;
504 goto ERROR1;
505 }
506 if (inb_p(address + 3) != i) {
507 err = -ENODEV;
508 goto ERROR1;
509 }
510 if (inb_p(address + 7) != i) {
511 err = -ENODEV;
512 goto ERROR1;
513 }
514#undef REALLY_SLOW_IO
515
516 /* Let's just hope nothing breaks here */
517 i = inb_p(address + 5) & 0x7f;
518 outb_p(~i & 0x7f, address + 5);
519 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
520 outb_p(i, address + 5);
521 err = -ENODEV;
522 goto ERROR1;
523 }
524 }
525 }
526
527 /* OK. For now, we presume we have a valid client. We now create the
528 client structure, even though we cannot fill it completely yet.
529 But it allows us to access lm78_{read,write}_value. */
530
531 if (!(data = kmalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
532 err = -ENOMEM;
533 goto ERROR1;
534 }
535 memset(data, 0, sizeof(struct lm78_data));
536
537 new_client = &data->client;
538 if (is_isa)
539 init_MUTEX(&data->lock);
540 i2c_set_clientdata(new_client, data);
541 new_client->addr = address;
542 new_client->adapter = adapter;
543 new_client->driver = &lm78_driver;
544 new_client->flags = 0;
545
546 /* Now, we do the remaining detection. */
547 if (kind < 0) {
548 if (lm78_read_value(new_client, LM78_REG_CONFIG) & 0x80) {
549 err = -ENODEV;
550 goto ERROR2;
551 }
552 if (!is_isa && (lm78_read_value(
553 new_client, LM78_REG_I2C_ADDR) != address)) {
554 err = -ENODEV;
555 goto ERROR2;
556 }
557 }
558
559 /* Determine the chip type. */
560 if (kind <= 0) {
561 i = lm78_read_value(new_client, LM78_REG_CHIPID);
562 if (i == 0x00 || i == 0x20)
563 kind = lm78;
564 else if (i == 0x40)
565 kind = lm78j;
566 else if ((i & 0xfe) == 0xc0)
567 kind = lm79;
568 else {
569 if (kind == 0)
570 dev_warn(&adapter->dev, "Ignoring 'force' "
571 "parameter for unknown chip at "
572 "adapter %d, address 0x%02x\n",
573 i2c_adapter_id(adapter), address);
574 err = -ENODEV;
575 goto ERROR2;
576 }
577 }
578
579 if (kind == lm78) {
580 client_name = "lm78";
581 } else if (kind == lm78j) {
582 client_name = "lm78-j";
583 } else if (kind == lm79) {
584 client_name = "lm79";
585 }
586
587 /* Fill in the remaining client fields and put into the global list */
588 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
589 data->type = kind;
590
591 data->valid = 0;
592 init_MUTEX(&data->update_lock);
593
594 /* Tell the I2C layer a new client has arrived */
595 if ((err = i2c_attach_client(new_client)))
596 goto ERROR2;
597
598 /* Initialize the LM78 chip */
599 lm78_init_client(new_client);
600
601 /* A few vars need to be filled upon startup */
602 for (i = 0; i < 3; i++) {
603 data->fan_min[i] = lm78_read_value(new_client,
604 LM78_REG_FAN_MIN(i));
605 }
606
607 /* Register sysfs hooks */
608 device_create_file(&new_client->dev, &dev_attr_in0_input);
609 device_create_file(&new_client->dev, &dev_attr_in0_min);
610 device_create_file(&new_client->dev, &dev_attr_in0_max);
611 device_create_file(&new_client->dev, &dev_attr_in1_input);
612 device_create_file(&new_client->dev, &dev_attr_in1_min);
613 device_create_file(&new_client->dev, &dev_attr_in1_max);
614 device_create_file(&new_client->dev, &dev_attr_in2_input);
615 device_create_file(&new_client->dev, &dev_attr_in2_min);
616 device_create_file(&new_client->dev, &dev_attr_in2_max);
617 device_create_file(&new_client->dev, &dev_attr_in3_input);
618 device_create_file(&new_client->dev, &dev_attr_in3_min);
619 device_create_file(&new_client->dev, &dev_attr_in3_max);
620 device_create_file(&new_client->dev, &dev_attr_in4_input);
621 device_create_file(&new_client->dev, &dev_attr_in4_min);
622 device_create_file(&new_client->dev, &dev_attr_in4_max);
623 device_create_file(&new_client->dev, &dev_attr_in5_input);
624 device_create_file(&new_client->dev, &dev_attr_in5_min);
625 device_create_file(&new_client->dev, &dev_attr_in5_max);
626 device_create_file(&new_client->dev, &dev_attr_in6_input);
627 device_create_file(&new_client->dev, &dev_attr_in6_min);
628 device_create_file(&new_client->dev, &dev_attr_in6_max);
629 device_create_file(&new_client->dev, &dev_attr_temp1_input);
630 device_create_file(&new_client->dev, &dev_attr_temp1_max);
631 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
632 device_create_file(&new_client->dev, &dev_attr_fan1_input);
633 device_create_file(&new_client->dev, &dev_attr_fan1_min);
634 device_create_file(&new_client->dev, &dev_attr_fan1_div);
635 device_create_file(&new_client->dev, &dev_attr_fan2_input);
636 device_create_file(&new_client->dev, &dev_attr_fan2_min);
637 device_create_file(&new_client->dev, &dev_attr_fan2_div);
638 device_create_file(&new_client->dev, &dev_attr_fan3_input);
639 device_create_file(&new_client->dev, &dev_attr_fan3_min);
640 device_create_file(&new_client->dev, &dev_attr_fan3_div);
641 device_create_file(&new_client->dev, &dev_attr_alarms);
642 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
643
644 return 0;
645
646ERROR2:
647 kfree(data);
648ERROR1:
649 if (is_isa)
650 release_region(address, LM78_EXTENT);
651ERROR0:
652 return err;
653}
654
655static int lm78_detach_client(struct i2c_client *client)
656{
657 int err;
658
659 if ((err = i2c_detach_client(client))) {
660 dev_err(&client->dev,
661 "Client deregistration failed, client not detached.\n");
662 return err;
663 }
664
665 if(i2c_is_isa_client(client))
666 release_region(client->addr, LM78_EXTENT);
667
668 kfree(i2c_get_clientdata(client));
669
670 return 0;
671}
672
673/* The SMBus locks itself, but ISA access must be locked explicitly!
674 We don't want to lock the whole ISA bus, so we lock each client
675 separately.
676 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
677 would slow down the LM78 access and should not be necessary. */
678static int lm78_read_value(struct i2c_client *client, u8 reg)
679{
680 int res;
681 if (i2c_is_isa_client(client)) {
682 struct lm78_data *data = i2c_get_clientdata(client);
683 down(&data->lock);
684 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
685 res = inb_p(client->addr + LM78_DATA_REG_OFFSET);
686 up(&data->lock);
687 return res;
688 } else
689 return i2c_smbus_read_byte_data(client, reg);
690}
691
692/* The SMBus locks itself, but ISA access muse be locked explicitly!
693 We don't want to lock the whole ISA bus, so we lock each client
694 separately.
695 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
696 would slow down the LM78 access and should not be necessary.
697 There are some ugly typecasts here, but the good new is - they should
698 nowhere else be necessary! */
699static int lm78_write_value(struct i2c_client *client, u8 reg, u8 value)
700{
701 if (i2c_is_isa_client(client)) {
702 struct lm78_data *data = i2c_get_clientdata(client);
703 down(&data->lock);
704 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
705 outb_p(value, client->addr + LM78_DATA_REG_OFFSET);
706 up(&data->lock);
707 return 0;
708 } else
709 return i2c_smbus_write_byte_data(client, reg, value);
710}
711
712/* Called when we have found a new LM78. It should set limits, etc. */
713static void lm78_init_client(struct i2c_client *client)
714{
715 u8 config = lm78_read_value(client, LM78_REG_CONFIG);
716
717 /* Start monitoring */
718 if (!(config & 0x01))
719 lm78_write_value(client, LM78_REG_CONFIG,
720 (config & 0xf7) | 0x01);
721}
722
723static struct lm78_data *lm78_update_device(struct device *dev)
724{
725 struct i2c_client *client = to_i2c_client(dev);
726 struct lm78_data *data = i2c_get_clientdata(client);
727 int i;
728
729 down(&data->update_lock);
730
731 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
732 || !data->valid) {
733
734 dev_dbg(&client->dev, "Starting lm78 update\n");
735
736 for (i = 0; i <= 6; i++) {
737 data->in[i] =
738 lm78_read_value(client, LM78_REG_IN(i));
739 data->in_min[i] =
740 lm78_read_value(client, LM78_REG_IN_MIN(i));
741 data->in_max[i] =
742 lm78_read_value(client, LM78_REG_IN_MAX(i));
743 }
744 for (i = 0; i < 3; i++) {
745 data->fan[i] =
746 lm78_read_value(client, LM78_REG_FAN(i));
747 data->fan_min[i] =
748 lm78_read_value(client, LM78_REG_FAN_MIN(i));
749 }
750 data->temp = lm78_read_value(client, LM78_REG_TEMP);
751 data->temp_over =
752 lm78_read_value(client, LM78_REG_TEMP_OVER);
753 data->temp_hyst =
754 lm78_read_value(client, LM78_REG_TEMP_HYST);
755 i = lm78_read_value(client, LM78_REG_VID_FANDIV);
756 data->vid = i & 0x0f;
757 if (data->type == lm79)
758 data->vid |=
759 (lm78_read_value(client, LM78_REG_CHIPID) &
760 0x01) << 4;
761 else
762 data->vid |= 0x10;
763 data->fan_div[0] = (i >> 4) & 0x03;
764 data->fan_div[1] = i >> 6;
765 data->alarms = lm78_read_value(client, LM78_REG_ALARM1) +
766 (lm78_read_value(client, LM78_REG_ALARM2) << 8);
767 data->last_updated = jiffies;
768 data->valid = 1;
769
770 data->fan_div[2] = 1;
771 }
772
773 up(&data->update_lock);
774
775 return data;
776}
777
778static int __init sm_lm78_init(void)
779{
780 return i2c_add_driver(&lm78_driver);
781}
782
783static void __exit sm_lm78_exit(void)
784{
785 i2c_del_driver(&lm78_driver);
786}
787
788
789
790MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
791MODULE_DESCRIPTION("LM78, LM78-J and LM79 driver");
792MODULE_LICENSE("GPL");
793
794module_init(sm_lm78_init);
795module_exit(sm_lm78_exit);
diff --git a/drivers/hwmon/lm80.c b/drivers/hwmon/lm80.c
new file mode 100644
index 000000000000..8100595feb44
--- /dev/null
+++ b/drivers/hwmon/lm80.c
@@ -0,0 +1,601 @@
1/*
2 * lm80.c - From lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5 * and Philip Edelbrock <phil@netroedge.com>
6 *
7 * Ported to Linux 2.6 by Tiago Sousa <mirage@kaotik.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 */
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/jiffies.h>
28#include <linux/i2c.h>
29#include <linux/i2c-sensor.h>
30
31/* Addresses to scan */
32static unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c,
33 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
34static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
35
36/* Insmod parameters */
37SENSORS_INSMOD_1(lm80);
38
39/* Many LM80 constants specified below */
40
41/* The LM80 registers */
42#define LM80_REG_IN_MAX(nr) (0x2a + (nr) * 2)
43#define LM80_REG_IN_MIN(nr) (0x2b + (nr) * 2)
44#define LM80_REG_IN(nr) (0x20 + (nr))
45
46#define LM80_REG_FAN1 0x28
47#define LM80_REG_FAN2 0x29
48#define LM80_REG_FAN_MIN(nr) (0x3b + (nr))
49
50#define LM80_REG_TEMP 0x27
51#define LM80_REG_TEMP_HOT_MAX 0x38
52#define LM80_REG_TEMP_HOT_HYST 0x39
53#define LM80_REG_TEMP_OS_MAX 0x3a
54#define LM80_REG_TEMP_OS_HYST 0x3b
55
56#define LM80_REG_CONFIG 0x00
57#define LM80_REG_ALARM1 0x01
58#define LM80_REG_ALARM2 0x02
59#define LM80_REG_MASK1 0x03
60#define LM80_REG_MASK2 0x04
61#define LM80_REG_FANDIV 0x05
62#define LM80_REG_RES 0x06
63
64
65/* Conversions. Rounding and limit checking is only done on the TO_REG
66 variants. Note that you should be a bit careful with which arguments
67 these macros are called: arguments may be evaluated more than once.
68 Fixing this is just not worth it. */
69
70#define IN_TO_REG(val) (SENSORS_LIMIT(((val)+5)/10,0,255))
71#define IN_FROM_REG(val) ((val)*10)
72
73static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div)
74{
75 if (rpm == 0)
76 return 255;
77 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
78 return SENSORS_LIMIT((1350000 + rpm*div / 2) / (rpm*div), 1, 254);
79}
80
81#define FAN_FROM_REG(val,div) ((val)==0?-1:\
82 (val)==255?0:1350000/((div)*(val)))
83
84static inline long TEMP_FROM_REG(u16 temp)
85{
86 long res;
87
88 temp >>= 4;
89 if (temp < 0x0800)
90 res = 625 * (long) temp;
91 else
92 res = ((long) temp - 0x01000) * 625;
93
94 return res / 10;
95}
96
97#define TEMP_LIMIT_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
98
99#define TEMP_LIMIT_TO_REG(val) SENSORS_LIMIT((val)<0?\
100 ((val)-500)/1000:((val)+500)/1000,0,255)
101
102#define DIV_FROM_REG(val) (1 << (val))
103
104/*
105 * Client data (each client gets its own)
106 */
107
108struct lm80_data {
109 struct i2c_client client;
110 struct semaphore update_lock;
111 char valid; /* !=0 if following fields are valid */
112 unsigned long last_updated; /* In jiffies */
113
114 u8 in[7]; /* Register value */
115 u8 in_max[7]; /* Register value */
116 u8 in_min[7]; /* Register value */
117 u8 fan[2]; /* Register value */
118 u8 fan_min[2]; /* Register value */
119 u8 fan_div[2]; /* Register encoding, shifted right */
120 u16 temp; /* Register values, shifted right */
121 u8 temp_hot_max; /* Register value */
122 u8 temp_hot_hyst; /* Register value */
123 u8 temp_os_max; /* Register value */
124 u8 temp_os_hyst; /* Register value */
125 u16 alarms; /* Register encoding, combined */
126};
127
128/*
129 * Functions declaration
130 */
131
132static int lm80_attach_adapter(struct i2c_adapter *adapter);
133static int lm80_detect(struct i2c_adapter *adapter, int address, int kind);
134static void lm80_init_client(struct i2c_client *client);
135static int lm80_detach_client(struct i2c_client *client);
136static struct lm80_data *lm80_update_device(struct device *dev);
137static int lm80_read_value(struct i2c_client *client, u8 reg);
138static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value);
139
140/*
141 * Driver data (common to all clients)
142 */
143
144static struct i2c_driver lm80_driver = {
145 .owner = THIS_MODULE,
146 .name = "lm80",
147 .id = I2C_DRIVERID_LM80,
148 .flags = I2C_DF_NOTIFY,
149 .attach_adapter = lm80_attach_adapter,
150 .detach_client = lm80_detach_client,
151};
152
153/*
154 * Sysfs stuff
155 */
156
157#define show_in(suffix, value) \
158static ssize_t show_in_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
159{ \
160 struct lm80_data *data = lm80_update_device(dev); \
161 return sprintf(buf, "%d\n", IN_FROM_REG(data->value)); \
162}
163show_in(min0, in_min[0]);
164show_in(min1, in_min[1]);
165show_in(min2, in_min[2]);
166show_in(min3, in_min[3]);
167show_in(min4, in_min[4]);
168show_in(min5, in_min[5]);
169show_in(min6, in_min[6]);
170show_in(max0, in_max[0]);
171show_in(max1, in_max[1]);
172show_in(max2, in_max[2]);
173show_in(max3, in_max[3]);
174show_in(max4, in_max[4]);
175show_in(max5, in_max[5]);
176show_in(max6, in_max[6]);
177show_in(input0, in[0]);
178show_in(input1, in[1]);
179show_in(input2, in[2]);
180show_in(input3, in[3]);
181show_in(input4, in[4]);
182show_in(input5, in[5]);
183show_in(input6, in[6]);
184
185#define set_in(suffix, value, reg) \
186static ssize_t set_in_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
187 size_t count) \
188{ \
189 struct i2c_client *client = to_i2c_client(dev); \
190 struct lm80_data *data = i2c_get_clientdata(client); \
191 long val = simple_strtol(buf, NULL, 10); \
192 \
193 down(&data->update_lock);\
194 data->value = IN_TO_REG(val); \
195 lm80_write_value(client, reg, data->value); \
196 up(&data->update_lock);\
197 return count; \
198}
199set_in(min0, in_min[0], LM80_REG_IN_MIN(0));
200set_in(min1, in_min[1], LM80_REG_IN_MIN(1));
201set_in(min2, in_min[2], LM80_REG_IN_MIN(2));
202set_in(min3, in_min[3], LM80_REG_IN_MIN(3));
203set_in(min4, in_min[4], LM80_REG_IN_MIN(4));
204set_in(min5, in_min[5], LM80_REG_IN_MIN(5));
205set_in(min6, in_min[6], LM80_REG_IN_MIN(6));
206set_in(max0, in_max[0], LM80_REG_IN_MAX(0));
207set_in(max1, in_max[1], LM80_REG_IN_MAX(1));
208set_in(max2, in_max[2], LM80_REG_IN_MAX(2));
209set_in(max3, in_max[3], LM80_REG_IN_MAX(3));
210set_in(max4, in_max[4], LM80_REG_IN_MAX(4));
211set_in(max5, in_max[5], LM80_REG_IN_MAX(5));
212set_in(max6, in_max[6], LM80_REG_IN_MAX(6));
213
214#define show_fan(suffix, value, div) \
215static ssize_t show_fan_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
216{ \
217 struct lm80_data *data = lm80_update_device(dev); \
218 return sprintf(buf, "%d\n", FAN_FROM_REG(data->value, \
219 DIV_FROM_REG(data->div))); \
220}
221show_fan(min1, fan_min[0], fan_div[0]);
222show_fan(min2, fan_min[1], fan_div[1]);
223show_fan(input1, fan[0], fan_div[0]);
224show_fan(input2, fan[1], fan_div[1]);
225
226#define show_fan_div(suffix, value) \
227static ssize_t show_fan_div##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
228{ \
229 struct lm80_data *data = lm80_update_device(dev); \
230 return sprintf(buf, "%d\n", DIV_FROM_REG(data->value)); \
231}
232show_fan_div(1, fan_div[0]);
233show_fan_div(2, fan_div[1]);
234
235#define set_fan(suffix, value, reg, div) \
236static ssize_t set_fan_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
237 size_t count) \
238{ \
239 struct i2c_client *client = to_i2c_client(dev); \
240 struct lm80_data *data = i2c_get_clientdata(client); \
241 long val = simple_strtoul(buf, NULL, 10); \
242 \
243 down(&data->update_lock);\
244 data->value = FAN_TO_REG(val, DIV_FROM_REG(data->div)); \
245 lm80_write_value(client, reg, data->value); \
246 up(&data->update_lock);\
247 return count; \
248}
249set_fan(min1, fan_min[0], LM80_REG_FAN_MIN(1), fan_div[0]);
250set_fan(min2, fan_min[1], LM80_REG_FAN_MIN(2), fan_div[1]);
251
252/* Note: we save and restore the fan minimum here, because its value is
253 determined in part by the fan divisor. This follows the principle of
254 least suprise; the user doesn't expect the fan minimum to change just
255 because the divisor changed. */
256static ssize_t set_fan_div(struct device *dev, const char *buf,
257 size_t count, int nr)
258{
259 struct i2c_client *client = to_i2c_client(dev);
260 struct lm80_data *data = i2c_get_clientdata(client);
261 unsigned long min, val = simple_strtoul(buf, NULL, 10);
262 u8 reg;
263
264 /* Save fan_min */
265 down(&data->update_lock);
266 min = FAN_FROM_REG(data->fan_min[nr],
267 DIV_FROM_REG(data->fan_div[nr]));
268
269 switch (val) {
270 case 1: data->fan_div[nr] = 0; break;
271 case 2: data->fan_div[nr] = 1; break;
272 case 4: data->fan_div[nr] = 2; break;
273 case 8: data->fan_div[nr] = 3; break;
274 default:
275 dev_err(&client->dev, "fan_div value %ld not "
276 "supported. Choose one of 1, 2, 4 or 8!\n", val);
277 up(&data->update_lock);
278 return -EINVAL;
279 }
280
281 reg = (lm80_read_value(client, LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1))))
282 | (data->fan_div[nr] << (2 * (nr + 1)));
283 lm80_write_value(client, LM80_REG_FANDIV, reg);
284
285 /* Restore fan_min */
286 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
287 lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
288 up(&data->update_lock);
289
290 return count;
291}
292
293#define set_fan_div(number) \
294static ssize_t set_fan_div##number(struct device *dev, struct device_attribute *attr, const char *buf, \
295 size_t count) \
296{ \
297 return set_fan_div(dev, buf, count, number - 1); \
298}
299set_fan_div(1);
300set_fan_div(2);
301
302static ssize_t show_temp_input1(struct device *dev, struct device_attribute *attr, char *buf)
303{
304 struct lm80_data *data = lm80_update_device(dev);
305 return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp));
306}
307
308#define show_temp(suffix, value) \
309static ssize_t show_temp_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
310{ \
311 struct lm80_data *data = lm80_update_device(dev); \
312 return sprintf(buf, "%d\n", TEMP_LIMIT_FROM_REG(data->value)); \
313}
314show_temp(hot_max, temp_hot_max);
315show_temp(hot_hyst, temp_hot_hyst);
316show_temp(os_max, temp_os_max);
317show_temp(os_hyst, temp_os_hyst);
318
319#define set_temp(suffix, value, reg) \
320static ssize_t set_temp_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
321 size_t count) \
322{ \
323 struct i2c_client *client = to_i2c_client(dev); \
324 struct lm80_data *data = i2c_get_clientdata(client); \
325 long val = simple_strtoul(buf, NULL, 10); \
326 \
327 down(&data->update_lock); \
328 data->value = TEMP_LIMIT_TO_REG(val); \
329 lm80_write_value(client, reg, data->value); \
330 up(&data->update_lock); \
331 return count; \
332}
333set_temp(hot_max, temp_hot_max, LM80_REG_TEMP_HOT_MAX);
334set_temp(hot_hyst, temp_hot_hyst, LM80_REG_TEMP_HOT_HYST);
335set_temp(os_max, temp_os_max, LM80_REG_TEMP_OS_MAX);
336set_temp(os_hyst, temp_os_hyst, LM80_REG_TEMP_OS_HYST);
337
338static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
339{
340 struct lm80_data *data = lm80_update_device(dev);
341 return sprintf(buf, "%u\n", data->alarms);
342}
343
344static DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min0, set_in_min0);
345static DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min1, set_in_min1);
346static DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min2, set_in_min2);
347static DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min3, set_in_min3);
348static DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min4, set_in_min4);
349static DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min5, set_in_min5);
350static DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min6, set_in_min6);
351static DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max0, set_in_max0);
352static DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max1, set_in_max1);
353static DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max2, set_in_max2);
354static DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max3, set_in_max3);
355static DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max4, set_in_max4);
356static DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max5, set_in_max5);
357static DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max6, set_in_max6);
358static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
359static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
360static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
361static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
362static DEVICE_ATTR(in4_input, S_IRUGO, show_in_input4, NULL);
363static DEVICE_ATTR(in5_input, S_IRUGO, show_in_input5, NULL);
364static DEVICE_ATTR(in6_input, S_IRUGO, show_in_input6, NULL);
365static DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min1,
366 set_fan_min1);
367static DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min2,
368 set_fan_min2);
369static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input1, NULL);
370static DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input2, NULL);
371static DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, show_fan_div1, set_fan_div1);
372static DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO, show_fan_div2, set_fan_div2);
373static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
374static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_hot_max,
375 set_temp_hot_max);
376static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hot_hyst,
377 set_temp_hot_hyst);
378static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_os_max,
379 set_temp_os_max);
380static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_os_hyst,
381 set_temp_os_hyst);
382static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
383
384/*
385 * Real code
386 */
387
388static int lm80_attach_adapter(struct i2c_adapter *adapter)
389{
390 if (!(adapter->class & I2C_CLASS_HWMON))
391 return 0;
392 return i2c_detect(adapter, &addr_data, lm80_detect);
393}
394
395int lm80_detect(struct i2c_adapter *adapter, int address, int kind)
396{
397 int i, cur;
398 struct i2c_client *new_client;
399 struct lm80_data *data;
400 int err = 0;
401 const char *name;
402
403 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
404 goto exit;
405
406 /* OK. For now, we presume we have a valid client. We now create the
407 client structure, even though we cannot fill it completely yet.
408 But it allows us to access lm80_{read,write}_value. */
409 if (!(data = kmalloc(sizeof(struct lm80_data), GFP_KERNEL))) {
410 err = -ENOMEM;
411 goto exit;
412 }
413 memset(data, 0, sizeof(struct lm80_data));
414
415 new_client = &data->client;
416 i2c_set_clientdata(new_client, data);
417 new_client->addr = address;
418 new_client->adapter = adapter;
419 new_client->driver = &lm80_driver;
420 new_client->flags = 0;
421
422 /* Now, we do the remaining detection. It is lousy. */
423 if (lm80_read_value(new_client, LM80_REG_ALARM2) & 0xc0)
424 goto error_free;
425 for (i = 0x2a; i <= 0x3d; i++) {
426 cur = i2c_smbus_read_byte_data(new_client, i);
427 if ((i2c_smbus_read_byte_data(new_client, i + 0x40) != cur)
428 || (i2c_smbus_read_byte_data(new_client, i + 0x80) != cur)
429 || (i2c_smbus_read_byte_data(new_client, i + 0xc0) != cur))
430 goto error_free;
431 }
432
433 /* Determine the chip type - only one kind supported! */
434 kind = lm80;
435 name = "lm80";
436
437 /* Fill in the remaining client fields and put it into the global list */
438 strlcpy(new_client->name, name, I2C_NAME_SIZE);
439 data->valid = 0;
440 init_MUTEX(&data->update_lock);
441
442 /* Tell the I2C layer a new client has arrived */
443 if ((err = i2c_attach_client(new_client)))
444 goto error_free;
445
446 /* Initialize the LM80 chip */
447 lm80_init_client(new_client);
448
449 /* A few vars need to be filled upon startup */
450 data->fan_min[0] = lm80_read_value(new_client, LM80_REG_FAN_MIN(1));
451 data->fan_min[1] = lm80_read_value(new_client, LM80_REG_FAN_MIN(2));
452
453 /* Register sysfs hooks */
454 device_create_file(&new_client->dev, &dev_attr_in0_min);
455 device_create_file(&new_client->dev, &dev_attr_in1_min);
456 device_create_file(&new_client->dev, &dev_attr_in2_min);
457 device_create_file(&new_client->dev, &dev_attr_in3_min);
458 device_create_file(&new_client->dev, &dev_attr_in4_min);
459 device_create_file(&new_client->dev, &dev_attr_in5_min);
460 device_create_file(&new_client->dev, &dev_attr_in6_min);
461 device_create_file(&new_client->dev, &dev_attr_in0_max);
462 device_create_file(&new_client->dev, &dev_attr_in1_max);
463 device_create_file(&new_client->dev, &dev_attr_in2_max);
464 device_create_file(&new_client->dev, &dev_attr_in3_max);
465 device_create_file(&new_client->dev, &dev_attr_in4_max);
466 device_create_file(&new_client->dev, &dev_attr_in5_max);
467 device_create_file(&new_client->dev, &dev_attr_in6_max);
468 device_create_file(&new_client->dev, &dev_attr_in0_input);
469 device_create_file(&new_client->dev, &dev_attr_in1_input);
470 device_create_file(&new_client->dev, &dev_attr_in2_input);
471 device_create_file(&new_client->dev, &dev_attr_in3_input);
472 device_create_file(&new_client->dev, &dev_attr_in4_input);
473 device_create_file(&new_client->dev, &dev_attr_in5_input);
474 device_create_file(&new_client->dev, &dev_attr_in6_input);
475 device_create_file(&new_client->dev, &dev_attr_fan1_min);
476 device_create_file(&new_client->dev, &dev_attr_fan2_min);
477 device_create_file(&new_client->dev, &dev_attr_fan1_input);
478 device_create_file(&new_client->dev, &dev_attr_fan2_input);
479 device_create_file(&new_client->dev, &dev_attr_fan1_div);
480 device_create_file(&new_client->dev, &dev_attr_fan2_div);
481 device_create_file(&new_client->dev, &dev_attr_temp1_input);
482 device_create_file(&new_client->dev, &dev_attr_temp1_max);
483 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
484 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
485 device_create_file(&new_client->dev, &dev_attr_temp1_crit_hyst);
486 device_create_file(&new_client->dev, &dev_attr_alarms);
487
488 return 0;
489
490error_free:
491 kfree(data);
492exit:
493 return err;
494}
495
496static int lm80_detach_client(struct i2c_client *client)
497{
498 int err;
499
500 if ((err = i2c_detach_client(client))) {
501 dev_err(&client->dev, "Client deregistration failed, "
502 "client not detached.\n");
503 return err;
504 }
505
506 kfree(i2c_get_clientdata(client));
507 return 0;
508}
509
510static int lm80_read_value(struct i2c_client *client, u8 reg)
511{
512 return i2c_smbus_read_byte_data(client, reg);
513}
514
515static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value)
516{
517 return i2c_smbus_write_byte_data(client, reg, value);
518}
519
520/* Called when we have found a new LM80. */
521static void lm80_init_client(struct i2c_client *client)
522{
523 /* Reset all except Watchdog values and last conversion values
524 This sets fan-divs to 2, among others. This makes most other
525 initializations unnecessary */
526 lm80_write_value(client, LM80_REG_CONFIG, 0x80);
527 /* Set 11-bit temperature resolution */
528 lm80_write_value(client, LM80_REG_RES, 0x08);
529
530 /* Start monitoring */
531 lm80_write_value(client, LM80_REG_CONFIG, 0x01);
532}
533
534static struct lm80_data *lm80_update_device(struct device *dev)
535{
536 struct i2c_client *client = to_i2c_client(dev);
537 struct lm80_data *data = i2c_get_clientdata(client);
538 int i;
539
540 down(&data->update_lock);
541
542 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
543 dev_dbg(&client->dev, "Starting lm80 update\n");
544 for (i = 0; i <= 6; i++) {
545 data->in[i] =
546 lm80_read_value(client, LM80_REG_IN(i));
547 data->in_min[i] =
548 lm80_read_value(client, LM80_REG_IN_MIN(i));
549 data->in_max[i] =
550 lm80_read_value(client, LM80_REG_IN_MAX(i));
551 }
552 data->fan[0] = lm80_read_value(client, LM80_REG_FAN1);
553 data->fan_min[0] =
554 lm80_read_value(client, LM80_REG_FAN_MIN(1));
555 data->fan[1] = lm80_read_value(client, LM80_REG_FAN2);
556 data->fan_min[1] =
557 lm80_read_value(client, LM80_REG_FAN_MIN(2));
558
559 data->temp =
560 (lm80_read_value(client, LM80_REG_TEMP) << 8) |
561 (lm80_read_value(client, LM80_REG_RES) & 0xf0);
562 data->temp_os_max =
563 lm80_read_value(client, LM80_REG_TEMP_OS_MAX);
564 data->temp_os_hyst =
565 lm80_read_value(client, LM80_REG_TEMP_OS_HYST);
566 data->temp_hot_max =
567 lm80_read_value(client, LM80_REG_TEMP_HOT_MAX);
568 data->temp_hot_hyst =
569 lm80_read_value(client, LM80_REG_TEMP_HOT_HYST);
570
571 i = lm80_read_value(client, LM80_REG_FANDIV);
572 data->fan_div[0] = (i >> 2) & 0x03;
573 data->fan_div[1] = (i >> 4) & 0x03;
574 data->alarms = lm80_read_value(client, LM80_REG_ALARM1) +
575 (lm80_read_value(client, LM80_REG_ALARM2) << 8);
576 data->last_updated = jiffies;
577 data->valid = 1;
578 }
579
580 up(&data->update_lock);
581
582 return data;
583}
584
585static int __init sensors_lm80_init(void)
586{
587 return i2c_add_driver(&lm80_driver);
588}
589
590static void __exit sensors_lm80_exit(void)
591{
592 i2c_del_driver(&lm80_driver);
593}
594
595MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
596 "Philip Edelbrock <phil@netroedge.com>");
597MODULE_DESCRIPTION("LM80 driver");
598MODULE_LICENSE("GPL");
599
600module_init(sensors_lm80_init);
601module_exit(sensors_lm80_exit);
diff --git a/drivers/hwmon/lm83.c b/drivers/hwmon/lm83.c
new file mode 100644
index 000000000000..a49008b444c8
--- /dev/null
+++ b/drivers/hwmon/lm83.c
@@ -0,0 +1,408 @@
1/*
2 * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 2003-2005 Jean Delvare <khali@linux-fr.org>
5 *
6 * Heavily inspired from the lm78, lm75 and adm1021 drivers. The LM83 is
7 * a sensor chip made by National Semiconductor. It reports up to four
8 * temperatures (its own plus up to three external ones) with a 1 deg
9 * resolution and a 3-4 deg accuracy. Complete datasheet can be obtained
10 * from National's website at:
11 * http://www.national.com/pf/LM/LM83.html
12 * Since the datasheet omits to give the chip stepping code, I give it
13 * here: 0x03 (at register 0xff).
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 */
29
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/slab.h>
33#include <linux/jiffies.h>
34#include <linux/i2c.h>
35#include <linux/i2c-sensor.h>
36#include <linux/hwmon-sysfs.h>
37
38/*
39 * Addresses to scan
40 * Address is selected using 2 three-level pins, resulting in 9 possible
41 * addresses.
42 */
43
44static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
45 0x29, 0x2a, 0x2b,
46 0x4c, 0x4d, 0x4e,
47 I2C_CLIENT_END };
48static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
49
50/*
51 * Insmod parameters
52 */
53
54SENSORS_INSMOD_1(lm83);
55
56/*
57 * The LM83 registers
58 * Manufacturer ID is 0x01 for National Semiconductor.
59 */
60
61#define LM83_REG_R_MAN_ID 0xFE
62#define LM83_REG_R_CHIP_ID 0xFF
63#define LM83_REG_R_CONFIG 0x03
64#define LM83_REG_W_CONFIG 0x09
65#define LM83_REG_R_STATUS1 0x02
66#define LM83_REG_R_STATUS2 0x35
67#define LM83_REG_R_LOCAL_TEMP 0x00
68#define LM83_REG_R_LOCAL_HIGH 0x05
69#define LM83_REG_W_LOCAL_HIGH 0x0B
70#define LM83_REG_R_REMOTE1_TEMP 0x30
71#define LM83_REG_R_REMOTE1_HIGH 0x38
72#define LM83_REG_W_REMOTE1_HIGH 0x50
73#define LM83_REG_R_REMOTE2_TEMP 0x01
74#define LM83_REG_R_REMOTE2_HIGH 0x07
75#define LM83_REG_W_REMOTE2_HIGH 0x0D
76#define LM83_REG_R_REMOTE3_TEMP 0x31
77#define LM83_REG_R_REMOTE3_HIGH 0x3A
78#define LM83_REG_W_REMOTE3_HIGH 0x52
79#define LM83_REG_R_TCRIT 0x42
80#define LM83_REG_W_TCRIT 0x5A
81
82/*
83 * Conversions and various macros
84 * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
85 */
86
87#define TEMP_FROM_REG(val) ((val) * 1000)
88#define TEMP_TO_REG(val) ((val) <= -128000 ? -128 : \
89 (val) >= 127000 ? 127 : \
90 (val) < 0 ? ((val) - 500) / 1000 : \
91 ((val) + 500) / 1000)
92
93static const u8 LM83_REG_R_TEMP[] = {
94 LM83_REG_R_LOCAL_TEMP,
95 LM83_REG_R_REMOTE1_TEMP,
96 LM83_REG_R_REMOTE2_TEMP,
97 LM83_REG_R_REMOTE3_TEMP,
98 LM83_REG_R_LOCAL_HIGH,
99 LM83_REG_R_REMOTE1_HIGH,
100 LM83_REG_R_REMOTE2_HIGH,
101 LM83_REG_R_REMOTE3_HIGH,
102 LM83_REG_R_TCRIT,
103};
104
105static const u8 LM83_REG_W_HIGH[] = {
106 LM83_REG_W_LOCAL_HIGH,
107 LM83_REG_W_REMOTE1_HIGH,
108 LM83_REG_W_REMOTE2_HIGH,
109 LM83_REG_W_REMOTE3_HIGH,
110 LM83_REG_W_TCRIT,
111};
112
113/*
114 * Functions declaration
115 */
116
117static int lm83_attach_adapter(struct i2c_adapter *adapter);
118static int lm83_detect(struct i2c_adapter *adapter, int address, int kind);
119static int lm83_detach_client(struct i2c_client *client);
120static struct lm83_data *lm83_update_device(struct device *dev);
121
122/*
123 * Driver data (common to all clients)
124 */
125
126static struct i2c_driver lm83_driver = {
127 .owner = THIS_MODULE,
128 .name = "lm83",
129 .id = I2C_DRIVERID_LM83,
130 .flags = I2C_DF_NOTIFY,
131 .attach_adapter = lm83_attach_adapter,
132 .detach_client = lm83_detach_client,
133};
134
135/*
136 * Client data (each client gets its own)
137 */
138
139struct lm83_data {
140 struct i2c_client client;
141 struct semaphore update_lock;
142 char valid; /* zero until following fields are valid */
143 unsigned long last_updated; /* in jiffies */
144
145 /* registers values */
146 s8 temp[9]; /* 0..3: input 1-4,
147 4..7: high limit 1-4,
148 8 : critical limit */
149 u16 alarms; /* bitvector, combined */
150};
151
152/*
153 * Sysfs stuff
154 */
155
156static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
157 char *buf)
158{
159 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
160 struct lm83_data *data = lm83_update_device(dev);
161 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
162}
163
164static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
165 const char *buf, size_t count)
166{
167 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
168 struct i2c_client *client = to_i2c_client(dev);
169 struct lm83_data *data = i2c_get_clientdata(client);
170 long val = simple_strtol(buf, NULL, 10);
171 int nr = attr->index;
172
173 down(&data->update_lock);
174 data->temp[nr] = TEMP_TO_REG(val);
175 i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
176 data->temp[nr]);
177 up(&data->update_lock);
178 return count;
179}
180
181static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
182 char *buf)
183{
184 struct lm83_data *data = lm83_update_device(dev);
185 return sprintf(buf, "%d\n", data->alarms);
186}
187
188static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
189static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
190static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
191static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
192static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
193 set_temp, 4);
194static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp,
195 set_temp, 5);
196static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp,
197 set_temp, 6);
198static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp,
199 set_temp, 7);
200static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8);
201static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8);
202static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
203 set_temp, 8);
204static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8);
205static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
206
207/*
208 * Real code
209 */
210
211static int lm83_attach_adapter(struct i2c_adapter *adapter)
212{
213 if (!(adapter->class & I2C_CLASS_HWMON))
214 return 0;
215 return i2c_detect(adapter, &addr_data, lm83_detect);
216}
217
218/*
219 * The following function does more than just detection. If detection
220 * succeeds, it also registers the new chip.
221 */
222static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
223{
224 struct i2c_client *new_client;
225 struct lm83_data *data;
226 int err = 0;
227 const char *name = "";
228
229 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
230 goto exit;
231
232 if (!(data = kmalloc(sizeof(struct lm83_data), GFP_KERNEL))) {
233 err = -ENOMEM;
234 goto exit;
235 }
236 memset(data, 0, sizeof(struct lm83_data));
237
238 /* The common I2C client data is placed right after the
239 * LM83-specific data. */
240 new_client = &data->client;
241 i2c_set_clientdata(new_client, data);
242 new_client->addr = address;
243 new_client->adapter = adapter;
244 new_client->driver = &lm83_driver;
245 new_client->flags = 0;
246
247 /* Now we do the detection and identification. A negative kind
248 * means that the driver was loaded with no force parameter
249 * (default), so we must both detect and identify the chip
250 * (actually there is only one possible kind of chip for now, LM83).
251 * A zero kind means that the driver was loaded with the force
252 * parameter, the detection step shall be skipped. A positive kind
253 * means that the driver was loaded with the force parameter and a
254 * given kind of chip is requested, so both the detection and the
255 * identification steps are skipped. */
256
257 /* Default to an LM83 if forced */
258 if (kind == 0)
259 kind = lm83;
260
261 if (kind < 0) { /* detection */
262 if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1)
263 & 0xA8) != 0x00) ||
264 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2)
265 & 0x48) != 0x00) ||
266 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
267 & 0x41) != 0x00)) {
268 dev_dbg(&adapter->dev,
269 "LM83 detection failed at 0x%02x.\n", address);
270 goto exit_free;
271 }
272 }
273
274 if (kind <= 0) { /* identification */
275 u8 man_id, chip_id;
276
277 man_id = i2c_smbus_read_byte_data(new_client,
278 LM83_REG_R_MAN_ID);
279 chip_id = i2c_smbus_read_byte_data(new_client,
280 LM83_REG_R_CHIP_ID);
281
282 if (man_id == 0x01) { /* National Semiconductor */
283 if (chip_id == 0x03) {
284 kind = lm83;
285 }
286 }
287
288 if (kind <= 0) { /* identification failed */
289 dev_info(&adapter->dev,
290 "Unsupported chip (man_id=0x%02X, "
291 "chip_id=0x%02X).\n", man_id, chip_id);
292 goto exit_free;
293 }
294 }
295
296 if (kind == lm83) {
297 name = "lm83";
298 }
299
300 /* We can fill in the remaining client fields */
301 strlcpy(new_client->name, name, I2C_NAME_SIZE);
302 data->valid = 0;
303 init_MUTEX(&data->update_lock);
304
305 /* Tell the I2C layer a new client has arrived */
306 if ((err = i2c_attach_client(new_client)))
307 goto exit_free;
308
309 /*
310 * Initialize the LM83 chip
311 * (Nothing to do for this one.)
312 */
313
314 /* Register sysfs hooks */
315 device_create_file(&new_client->dev,
316 &sensor_dev_attr_temp1_input.dev_attr);
317 device_create_file(&new_client->dev,
318 &sensor_dev_attr_temp2_input.dev_attr);
319 device_create_file(&new_client->dev,
320 &sensor_dev_attr_temp3_input.dev_attr);
321 device_create_file(&new_client->dev,
322 &sensor_dev_attr_temp4_input.dev_attr);
323 device_create_file(&new_client->dev,
324 &sensor_dev_attr_temp1_max.dev_attr);
325 device_create_file(&new_client->dev,
326 &sensor_dev_attr_temp2_max.dev_attr);
327 device_create_file(&new_client->dev,
328 &sensor_dev_attr_temp3_max.dev_attr);
329 device_create_file(&new_client->dev,
330 &sensor_dev_attr_temp4_max.dev_attr);
331 device_create_file(&new_client->dev,
332 &sensor_dev_attr_temp1_crit.dev_attr);
333 device_create_file(&new_client->dev,
334 &sensor_dev_attr_temp2_crit.dev_attr);
335 device_create_file(&new_client->dev,
336 &sensor_dev_attr_temp3_crit.dev_attr);
337 device_create_file(&new_client->dev,
338 &sensor_dev_attr_temp4_crit.dev_attr);
339 device_create_file(&new_client->dev, &dev_attr_alarms);
340
341 return 0;
342
343exit_free:
344 kfree(data);
345exit:
346 return err;
347}
348
349static int lm83_detach_client(struct i2c_client *client)
350{
351 int err;
352
353 if ((err = i2c_detach_client(client))) {
354 dev_err(&client->dev,
355 "Client deregistration failed, client not detached.\n");
356 return err;
357 }
358
359 kfree(i2c_get_clientdata(client));
360 return 0;
361}
362
363static struct lm83_data *lm83_update_device(struct device *dev)
364{
365 struct i2c_client *client = to_i2c_client(dev);
366 struct lm83_data *data = i2c_get_clientdata(client);
367
368 down(&data->update_lock);
369
370 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
371 int nr;
372
373 dev_dbg(&client->dev, "Updating lm83 data.\n");
374 for (nr = 0; nr < 9; nr++) {
375 data->temp[nr] =
376 i2c_smbus_read_byte_data(client,
377 LM83_REG_R_TEMP[nr]);
378 }
379 data->alarms =
380 i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
381 + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
382 << 8);
383
384 data->last_updated = jiffies;
385 data->valid = 1;
386 }
387
388 up(&data->update_lock);
389
390 return data;
391}
392
393static int __init sensors_lm83_init(void)
394{
395 return i2c_add_driver(&lm83_driver);
396}
397
398static void __exit sensors_lm83_exit(void)
399{
400 i2c_del_driver(&lm83_driver);
401}
402
403MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
404MODULE_DESCRIPTION("LM83 driver");
405MODULE_LICENSE("GPL");
406
407module_init(sensors_lm83_init);
408module_exit(sensors_lm83_exit);
diff --git a/drivers/hwmon/lm85.c b/drivers/hwmon/lm85.c
new file mode 100644
index 000000000000..b4d7fd418264
--- /dev/null
+++ b/drivers/hwmon/lm85.c
@@ -0,0 +1,1575 @@
1/*
2 lm85.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5 Copyright (c) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com>
6 Copyright (c) 2003 Margit Schubert-While <margitsw@t-online.de>
7 Copyright (c) 2004 Justin Thiessen <jthiessen@penguincomputing.com>
8
9 Chip details at <http://www.national.com/ds/LM/LM85.pdf>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24*/
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/slab.h>
29#include <linux/jiffies.h>
30#include <linux/i2c.h>
31#include <linux/i2c-sensor.h>
32#include <linux/i2c-vid.h>
33
34/* Addresses to scan */
35static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
36static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
37
38/* Insmod parameters */
39SENSORS_INSMOD_6(lm85b, lm85c, adm1027, adt7463, emc6d100, emc6d102);
40
41/* The LM85 registers */
42
43#define LM85_REG_IN(nr) (0x20 + (nr))
44#define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2)
45#define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2)
46
47#define LM85_REG_TEMP(nr) (0x25 + (nr))
48#define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2)
49#define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2)
50
51/* Fan speeds are LSB, MSB (2 bytes) */
52#define LM85_REG_FAN(nr) (0x28 + (nr) *2)
53#define LM85_REG_FAN_MIN(nr) (0x54 + (nr) *2)
54
55#define LM85_REG_PWM(nr) (0x30 + (nr))
56
57#define ADT7463_REG_OPPOINT(nr) (0x33 + (nr))
58
59#define ADT7463_REG_TMIN_CTL1 0x36
60#define ADT7463_REG_TMIN_CTL2 0x37
61
62#define LM85_REG_DEVICE 0x3d
63#define LM85_REG_COMPANY 0x3e
64#define LM85_REG_VERSTEP 0x3f
65/* These are the recognized values for the above regs */
66#define LM85_DEVICE_ADX 0x27
67#define LM85_COMPANY_NATIONAL 0x01
68#define LM85_COMPANY_ANALOG_DEV 0x41
69#define LM85_COMPANY_SMSC 0x5c
70#define LM85_VERSTEP_VMASK 0xf0
71#define LM85_VERSTEP_GENERIC 0x60
72#define LM85_VERSTEP_LM85C 0x60
73#define LM85_VERSTEP_LM85B 0x62
74#define LM85_VERSTEP_ADM1027 0x60
75#define LM85_VERSTEP_ADT7463 0x62
76#define LM85_VERSTEP_ADT7463C 0x6A
77#define LM85_VERSTEP_EMC6D100_A0 0x60
78#define LM85_VERSTEP_EMC6D100_A1 0x61
79#define LM85_VERSTEP_EMC6D102 0x65
80
81#define LM85_REG_CONFIG 0x40
82
83#define LM85_REG_ALARM1 0x41
84#define LM85_REG_ALARM2 0x42
85
86#define LM85_REG_VID 0x43
87
88/* Automated FAN control */
89#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
90#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
91#define LM85_REG_AFAN_SPIKE1 0x62
92#define LM85_REG_AFAN_SPIKE2 0x63
93#define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
94#define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
95#define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
96#define LM85_REG_AFAN_HYST1 0x6d
97#define LM85_REG_AFAN_HYST2 0x6e
98
99#define LM85_REG_TACH_MODE 0x74
100#define LM85_REG_SPINUP_CTL 0x75
101
102#define ADM1027_REG_TEMP_OFFSET(nr) (0x70 + (nr))
103#define ADM1027_REG_CONFIG2 0x73
104#define ADM1027_REG_INTMASK1 0x74
105#define ADM1027_REG_INTMASK2 0x75
106#define ADM1027_REG_EXTEND_ADC1 0x76
107#define ADM1027_REG_EXTEND_ADC2 0x77
108#define ADM1027_REG_CONFIG3 0x78
109#define ADM1027_REG_FAN_PPR 0x7b
110
111#define ADT7463_REG_THERM 0x79
112#define ADT7463_REG_THERM_LIMIT 0x7A
113
114#define EMC6D100_REG_ALARM3 0x7d
115/* IN5, IN6 and IN7 */
116#define EMC6D100_REG_IN(nr) (0x70 + ((nr)-5))
117#define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr)-5) * 2)
118#define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr)-5) * 2)
119#define EMC6D102_REG_EXTEND_ADC1 0x85
120#define EMC6D102_REG_EXTEND_ADC2 0x86
121#define EMC6D102_REG_EXTEND_ADC3 0x87
122#define EMC6D102_REG_EXTEND_ADC4 0x88
123
124#define LM85_ALARM_IN0 0x0001
125#define LM85_ALARM_IN1 0x0002
126#define LM85_ALARM_IN2 0x0004
127#define LM85_ALARM_IN3 0x0008
128#define LM85_ALARM_TEMP1 0x0010
129#define LM85_ALARM_TEMP2 0x0020
130#define LM85_ALARM_TEMP3 0x0040
131#define LM85_ALARM_ALARM2 0x0080
132#define LM85_ALARM_IN4 0x0100
133#define LM85_ALARM_RESERVED 0x0200
134#define LM85_ALARM_FAN1 0x0400
135#define LM85_ALARM_FAN2 0x0800
136#define LM85_ALARM_FAN3 0x1000
137#define LM85_ALARM_FAN4 0x2000
138#define LM85_ALARM_TEMP1_FAULT 0x4000
139#define LM85_ALARM_TEMP3_FAULT 0x8000
140
141
142/* Conversions. Rounding and limit checking is only done on the TO_REG
143 variants. Note that you should be a bit careful with which arguments
144 these macros are called: arguments may be evaluated more than once.
145 */
146
147/* IN are scaled acording to built-in resistors */
148static int lm85_scaling[] = { /* .001 Volts */
149 2500, 2250, 3300, 5000, 12000,
150 3300, 1500, 1800 /*EMC6D100*/
151 };
152#define SCALE(val,from,to) (((val)*(to) + ((from)/2))/(from))
153
154#define INS_TO_REG(n,val) \
155 SENSORS_LIMIT(SCALE(val,lm85_scaling[n],192),0,255)
156
157#define INSEXT_FROM_REG(n,val,ext,scale) \
158 SCALE((val)*(scale) + (ext),192*(scale),lm85_scaling[n])
159
160#define INS_FROM_REG(n,val) INSEXT_FROM_REG(n,val,0,1)
161
162/* FAN speed is measured using 90kHz clock */
163#define FAN_TO_REG(val) (SENSORS_LIMIT( (val)<=0?0: 5400000/(val),0,65534))
164#define FAN_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:5400000/(val))
165
166/* Temperature is reported in .001 degC increments */
167#define TEMP_TO_REG(val) \
168 SENSORS_LIMIT(SCALE(val,1000,1),-127,127)
169#define TEMPEXT_FROM_REG(val,ext,scale) \
170 SCALE((val)*scale + (ext),scale,1000)
171#define TEMP_FROM_REG(val) \
172 TEMPEXT_FROM_REG(val,0,1)
173
174#define PWM_TO_REG(val) (SENSORS_LIMIT(val,0,255))
175#define PWM_FROM_REG(val) (val)
176
177
178/* ZONEs have the following parameters:
179 * Limit (low) temp, 1. degC
180 * Hysteresis (below limit), 1. degC (0-15)
181 * Range of speed control, .1 degC (2-80)
182 * Critical (high) temp, 1. degC
183 *
184 * FAN PWMs have the following parameters:
185 * Reference Zone, 1, 2, 3, etc.
186 * Spinup time, .05 sec
187 * PWM value at limit/low temp, 1 count
188 * PWM Frequency, 1. Hz
189 * PWM is Min or OFF below limit, flag
190 * Invert PWM output, flag
191 *
192 * Some chips filter the temp, others the fan.
193 * Filter constant (or disabled) .1 seconds
194 */
195
196/* These are the zone temperature range encodings in .001 degree C */
197static int lm85_range_map[] = {
198 2000, 2500, 3300, 4000, 5000, 6600,
199 8000, 10000, 13300, 16000, 20000, 26600,
200 32000, 40000, 53300, 80000
201 };
202static int RANGE_TO_REG( int range )
203{
204 int i;
205
206 if ( range < lm85_range_map[0] ) {
207 return 0 ;
208 } else if ( range > lm85_range_map[15] ) {
209 return 15 ;
210 } else { /* find closest match */
211 for ( i = 14 ; i >= 0 ; --i ) {
212 if ( range > lm85_range_map[i] ) { /* range bracketed */
213 if ((lm85_range_map[i+1] - range) <
214 (range - lm85_range_map[i])) {
215 i++;
216 break;
217 }
218 break;
219 }
220 }
221 }
222 return( i & 0x0f );
223}
224#define RANGE_FROM_REG(val) (lm85_range_map[(val)&0x0f])
225
226/* These are the Acoustic Enhancement, or Temperature smoothing encodings
227 * NOTE: The enable/disable bit is INCLUDED in these encodings as the
228 * MSB (bit 3, value 8). If the enable bit is 0, the encoded value
229 * is ignored, or set to 0.
230 */
231/* These are the PWM frequency encodings */
232static int lm85_freq_map[] = { /* .1 Hz */
233 100, 150, 230, 300, 380, 470, 620, 940
234 };
235static int FREQ_TO_REG( int freq )
236{
237 int i;
238
239 if( freq >= lm85_freq_map[7] ) { return 7 ; }
240 for( i = 0 ; i < 7 ; ++i )
241 if( freq <= lm85_freq_map[i] )
242 break ;
243 return( i & 0x07 );
244}
245#define FREQ_FROM_REG(val) (lm85_freq_map[(val)&0x07])
246
247/* Since we can't use strings, I'm abusing these numbers
248 * to stand in for the following meanings:
249 * 1 -- PWM responds to Zone 1
250 * 2 -- PWM responds to Zone 2
251 * 3 -- PWM responds to Zone 3
252 * 23 -- PWM responds to the higher temp of Zone 2 or 3
253 * 123 -- PWM responds to highest of Zone 1, 2, or 3
254 * 0 -- PWM is always at 0% (ie, off)
255 * -1 -- PWM is always at 100%
256 * -2 -- PWM responds to manual control
257 */
258
259static int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
260#define ZONE_FROM_REG(val) (lm85_zone_map[((val)>>5)&0x07])
261
262static int ZONE_TO_REG( int zone )
263{
264 int i;
265
266 for( i = 0 ; i <= 7 ; ++i )
267 if( zone == lm85_zone_map[i] )
268 break ;
269 if( i > 7 ) /* Not found. */
270 i = 3; /* Always 100% */
271 return( (i & 0x07)<<5 );
272}
273
274#define HYST_TO_REG(val) (SENSORS_LIMIT(((val)+500)/1000,0,15))
275#define HYST_FROM_REG(val) ((val)*1000)
276
277#define OFFSET_TO_REG(val) (SENSORS_LIMIT((val)/25,-127,127))
278#define OFFSET_FROM_REG(val) ((val)*25)
279
280#define PPR_MASK(fan) (0x03<<(fan *2))
281#define PPR_TO_REG(val,fan) (SENSORS_LIMIT((val)-1,0,3)<<(fan *2))
282#define PPR_FROM_REG(val,fan) ((((val)>>(fan * 2))&0x03)+1)
283
284/* i2c-vid.h defines vid_from_reg() */
285#define VID_FROM_REG(val,vrm) (vid_from_reg((val),(vrm)))
286
287/* Unlike some other drivers we DO NOT set initial limits. Use
288 * the config file to set limits. Some users have reported
289 * motherboards shutting down when we set limits in a previous
290 * version of the driver.
291 */
292
293/* Chip sampling rates
294 *
295 * Some sensors are not updated more frequently than once per second
296 * so it doesn't make sense to read them more often than that.
297 * We cache the results and return the saved data if the driver
298 * is called again before a second has elapsed.
299 *
300 * Also, there is significant configuration data for this chip
301 * given the automatic PWM fan control that is possible. There
302 * are about 47 bytes of config data to only 22 bytes of actual
303 * readings. So, we keep the config data up to date in the cache
304 * when it is written and only sample it once every 1 *minute*
305 */
306#define LM85_DATA_INTERVAL (HZ + HZ / 2)
307#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
308
309/* For each registered LM85, we need to keep some data in memory. That
310 data is pointed to by lm85_list[NR]->data. The structure itself is
311 dynamically allocated, at the same time when a new lm85 client is
312 allocated. */
313
314/* LM85 can automatically adjust fan speeds based on temperature
315 * This structure encapsulates an entire Zone config. There are
316 * three zones (one for each temperature input) on the lm85
317 */
318struct lm85_zone {
319 s8 limit; /* Low temp limit */
320 u8 hyst; /* Low limit hysteresis. (0-15) */
321 u8 range; /* Temp range, encoded */
322 s8 critical; /* "All fans ON" temp limit */
323 u8 off_desired; /* Actual "off" temperature specified. Preserved
324 * to prevent "drift" as other autofan control
325 * values change.
326 */
327 u8 max_desired; /* Actual "max" temperature specified. Preserved
328 * to prevent "drift" as other autofan control
329 * values change.
330 */
331};
332
333struct lm85_autofan {
334 u8 config; /* Register value */
335 u8 freq; /* PWM frequency, encoded */
336 u8 min_pwm; /* Minimum PWM value, encoded */
337 u8 min_off; /* Min PWM or OFF below "limit", flag */
338};
339
340struct lm85_data {
341 struct i2c_client client;
342 struct semaphore lock;
343 enum chips type;
344
345 struct semaphore update_lock;
346 int valid; /* !=0 if following fields are valid */
347 unsigned long last_reading; /* In jiffies */
348 unsigned long last_config; /* In jiffies */
349
350 u8 in[8]; /* Register value */
351 u8 in_max[8]; /* Register value */
352 u8 in_min[8]; /* Register value */
353 s8 temp[3]; /* Register value */
354 s8 temp_min[3]; /* Register value */
355 s8 temp_max[3]; /* Register value */
356 s8 temp_offset[3]; /* Register value */
357 u16 fan[4]; /* Register value */
358 u16 fan_min[4]; /* Register value */
359 u8 pwm[3]; /* Register value */
360 u8 spinup_ctl; /* Register encoding, combined */
361 u8 tach_mode; /* Register encoding, combined */
362 u8 temp_ext[3]; /* Decoded values */
363 u8 in_ext[8]; /* Decoded values */
364 u8 adc_scale; /* ADC Extended bits scaling factor */
365 u8 fan_ppr; /* Register value */
366 u8 smooth[3]; /* Register encoding */
367 u8 vid; /* Register value */
368 u8 vrm; /* VRM version */
369 u8 syncpwm3; /* Saved PWM3 for TACH 2,3,4 config */
370 u8 oppoint[3]; /* Register value */
371 u16 tmin_ctl; /* Register value */
372 unsigned long therm_total; /* Cummulative therm count */
373 u8 therm_limit; /* Register value */
374 u32 alarms; /* Register encoding, combined */
375 struct lm85_autofan autofan[3];
376 struct lm85_zone zone[3];
377};
378
379static int lm85_attach_adapter(struct i2c_adapter *adapter);
380static int lm85_detect(struct i2c_adapter *adapter, int address,
381 int kind);
382static int lm85_detach_client(struct i2c_client *client);
383
384static int lm85_read_value(struct i2c_client *client, u8 register);
385static int lm85_write_value(struct i2c_client *client, u8 register, int value);
386static struct lm85_data *lm85_update_device(struct device *dev);
387static void lm85_init_client(struct i2c_client *client);
388
389
390static struct i2c_driver lm85_driver = {
391 .owner = THIS_MODULE,
392 .name = "lm85",
393 .id = I2C_DRIVERID_LM85,
394 .flags = I2C_DF_NOTIFY,
395 .attach_adapter = lm85_attach_adapter,
396 .detach_client = lm85_detach_client,
397};
398
399
400/* 4 Fans */
401static ssize_t show_fan(struct device *dev, char *buf, int nr)
402{
403 struct lm85_data *data = lm85_update_device(dev);
404 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr]) );
405}
406static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
407{
408 struct lm85_data *data = lm85_update_device(dev);
409 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr]) );
410}
411static ssize_t set_fan_min(struct device *dev, const char *buf,
412 size_t count, int nr)
413{
414 struct i2c_client *client = to_i2c_client(dev);
415 struct lm85_data *data = i2c_get_clientdata(client);
416 long val = simple_strtol(buf, NULL, 10);
417
418 down(&data->update_lock);
419 data->fan_min[nr] = FAN_TO_REG(val);
420 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
421 up(&data->update_lock);
422 return count;
423}
424
425#define show_fan_offset(offset) \
426static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
427{ \
428 return show_fan(dev, buf, offset - 1); \
429} \
430static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
431{ \
432 return show_fan_min(dev, buf, offset - 1); \
433} \
434static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
435 const char *buf, size_t count) \
436{ \
437 return set_fan_min(dev, buf, count, offset - 1); \
438} \
439static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, \
440 NULL); \
441static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
442 show_fan_##offset##_min, set_fan_##offset##_min);
443
444show_fan_offset(1);
445show_fan_offset(2);
446show_fan_offset(3);
447show_fan_offset(4);
448
449/* vid, vrm, alarms */
450
451static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
452{
453 struct lm85_data *data = lm85_update_device(dev);
454 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
455}
456
457static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
458
459static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
460{
461 struct lm85_data *data = lm85_update_device(dev);
462 return sprintf(buf, "%ld\n", (long) data->vrm);
463}
464
465static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
466{
467 struct i2c_client *client = to_i2c_client(dev);
468 struct lm85_data *data = i2c_get_clientdata(client);
469 u32 val;
470
471 val = simple_strtoul(buf, NULL, 10);
472 data->vrm = val;
473 return count;
474}
475
476static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
477
478static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
479{
480 struct lm85_data *data = lm85_update_device(dev);
481 return sprintf(buf, "%u\n", data->alarms);
482}
483
484static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
485
486/* pwm */
487
488static ssize_t show_pwm(struct device *dev, char *buf, int nr)
489{
490 struct lm85_data *data = lm85_update_device(dev);
491 return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm[nr]) );
492}
493static ssize_t set_pwm(struct device *dev, const char *buf,
494 size_t count, int nr)
495{
496 struct i2c_client *client = to_i2c_client(dev);
497 struct lm85_data *data = i2c_get_clientdata(client);
498 long val = simple_strtol(buf, NULL, 10);
499
500 down(&data->update_lock);
501 data->pwm[nr] = PWM_TO_REG(val);
502 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
503 up(&data->update_lock);
504 return count;
505}
506static ssize_t show_pwm_enable(struct device *dev, char *buf, int nr)
507{
508 struct lm85_data *data = lm85_update_device(dev);
509 int pwm_zone;
510
511 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
512 return sprintf(buf,"%d\n", (pwm_zone != 0 && pwm_zone != -1) );
513}
514
515#define show_pwm_reg(offset) \
516static ssize_t show_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
517{ \
518 return show_pwm(dev, buf, offset - 1); \
519} \
520static ssize_t set_pwm_##offset (struct device *dev, struct device_attribute *attr, \
521 const char *buf, size_t count) \
522{ \
523 return set_pwm(dev, buf, count, offset - 1); \
524} \
525static ssize_t show_pwm_enable##offset (struct device *dev, struct device_attribute *attr, char *buf) \
526{ \
527 return show_pwm_enable(dev, buf, offset - 1); \
528} \
529static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
530 show_pwm_##offset, set_pwm_##offset); \
531static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO, \
532 show_pwm_enable##offset, NULL);
533
534show_pwm_reg(1);
535show_pwm_reg(2);
536show_pwm_reg(3);
537
538/* Voltages */
539
540static ssize_t show_in(struct device *dev, char *buf, int nr)
541{
542 struct lm85_data *data = lm85_update_device(dev);
543 return sprintf( buf, "%d\n", INSEXT_FROM_REG(nr,
544 data->in[nr],
545 data->in_ext[nr],
546 data->adc_scale) );
547}
548static ssize_t show_in_min(struct device *dev, char *buf, int nr)
549{
550 struct lm85_data *data = lm85_update_device(dev);
551 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]) );
552}
553static ssize_t set_in_min(struct device *dev, const char *buf,
554 size_t count, int nr)
555{
556 struct i2c_client *client = to_i2c_client(dev);
557 struct lm85_data *data = i2c_get_clientdata(client);
558 long val = simple_strtol(buf, NULL, 10);
559
560 down(&data->update_lock);
561 data->in_min[nr] = INS_TO_REG(nr, val);
562 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
563 up(&data->update_lock);
564 return count;
565}
566static ssize_t show_in_max(struct device *dev, char *buf, int nr)
567{
568 struct lm85_data *data = lm85_update_device(dev);
569 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]) );
570}
571static ssize_t set_in_max(struct device *dev, const char *buf,
572 size_t count, int nr)
573{
574 struct i2c_client *client = to_i2c_client(dev);
575 struct lm85_data *data = i2c_get_clientdata(client);
576 long val = simple_strtol(buf, NULL, 10);
577
578 down(&data->update_lock);
579 data->in_max[nr] = INS_TO_REG(nr, val);
580 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
581 up(&data->update_lock);
582 return count;
583}
584#define show_in_reg(offset) \
585static ssize_t show_in_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
586{ \
587 return show_in(dev, buf, offset); \
588} \
589static ssize_t show_in_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
590{ \
591 return show_in_min(dev, buf, offset); \
592} \
593static ssize_t show_in_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
594{ \
595 return show_in_max(dev, buf, offset); \
596} \
597static ssize_t set_in_##offset##_min (struct device *dev, struct device_attribute *attr, \
598 const char *buf, size_t count) \
599{ \
600 return set_in_min(dev, buf, count, offset); \
601} \
602static ssize_t set_in_##offset##_max (struct device *dev, struct device_attribute *attr, \
603 const char *buf, size_t count) \
604{ \
605 return set_in_max(dev, buf, count, offset); \
606} \
607static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in_##offset, \
608 NULL); \
609static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
610 show_in_##offset##_min, set_in_##offset##_min); \
611static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
612 show_in_##offset##_max, set_in_##offset##_max);
613
614show_in_reg(0);
615show_in_reg(1);
616show_in_reg(2);
617show_in_reg(3);
618show_in_reg(4);
619
620/* Temps */
621
622static ssize_t show_temp(struct device *dev, char *buf, int nr)
623{
624 struct lm85_data *data = lm85_update_device(dev);
625 return sprintf(buf,"%d\n", TEMPEXT_FROM_REG(data->temp[nr],
626 data->temp_ext[nr],
627 data->adc_scale) );
628}
629static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
630{
631 struct lm85_data *data = lm85_update_device(dev);
632 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]) );
633}
634static ssize_t set_temp_min(struct device *dev, const char *buf,
635 size_t count, int nr)
636{
637 struct i2c_client *client = to_i2c_client(dev);
638 struct lm85_data *data = i2c_get_clientdata(client);
639 long val = simple_strtol(buf, NULL, 10);
640
641 down(&data->update_lock);
642 data->temp_min[nr] = TEMP_TO_REG(val);
643 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
644 up(&data->update_lock);
645 return count;
646}
647static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
648{
649 struct lm85_data *data = lm85_update_device(dev);
650 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]) );
651}
652static ssize_t set_temp_max(struct device *dev, const char *buf,
653 size_t count, int nr)
654{
655 struct i2c_client *client = to_i2c_client(dev);
656 struct lm85_data *data = i2c_get_clientdata(client);
657 long val = simple_strtol(buf, NULL, 10);
658
659 down(&data->update_lock);
660 data->temp_max[nr] = TEMP_TO_REG(val);
661 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
662 up(&data->update_lock);
663 return count;
664}
665#define show_temp_reg(offset) \
666static ssize_t show_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
667{ \
668 return show_temp(dev, buf, offset - 1); \
669} \
670static ssize_t show_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
671{ \
672 return show_temp_min(dev, buf, offset - 1); \
673} \
674static ssize_t show_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
675{ \
676 return show_temp_max(dev, buf, offset - 1); \
677} \
678static ssize_t set_temp_##offset##_min (struct device *dev, struct device_attribute *attr, \
679 const char *buf, size_t count) \
680{ \
681 return set_temp_min(dev, buf, count, offset - 1); \
682} \
683static ssize_t set_temp_##offset##_max (struct device *dev, struct device_attribute *attr, \
684 const char *buf, size_t count) \
685{ \
686 return set_temp_max(dev, buf, count, offset - 1); \
687} \
688static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, \
689 NULL); \
690static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
691 show_temp_##offset##_min, set_temp_##offset##_min); \
692static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
693 show_temp_##offset##_max, set_temp_##offset##_max);
694
695show_temp_reg(1);
696show_temp_reg(2);
697show_temp_reg(3);
698
699
700/* Automatic PWM control */
701
702static ssize_t show_pwm_auto_channels(struct device *dev, char *buf, int nr)
703{
704 struct lm85_data *data = lm85_update_device(dev);
705 return sprintf(buf,"%d\n", ZONE_FROM_REG(data->autofan[nr].config));
706}
707static ssize_t set_pwm_auto_channels(struct device *dev, const char *buf,
708 size_t count, int nr)
709{
710 struct i2c_client *client = to_i2c_client(dev);
711 struct lm85_data *data = i2c_get_clientdata(client);
712 long val = simple_strtol(buf, NULL, 10);
713
714 down(&data->update_lock);
715 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
716 | ZONE_TO_REG(val) ;
717 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
718 data->autofan[nr].config);
719 up(&data->update_lock);
720 return count;
721}
722static ssize_t show_pwm_auto_pwm_min(struct device *dev, char *buf, int nr)
723{
724 struct lm85_data *data = lm85_update_device(dev);
725 return sprintf(buf,"%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
726}
727static ssize_t set_pwm_auto_pwm_min(struct device *dev, const char *buf,
728 size_t count, int nr)
729{
730 struct i2c_client *client = to_i2c_client(dev);
731 struct lm85_data *data = i2c_get_clientdata(client);
732 long val = simple_strtol(buf, NULL, 10);
733
734 down(&data->update_lock);
735 data->autofan[nr].min_pwm = PWM_TO_REG(val);
736 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
737 data->autofan[nr].min_pwm);
738 up(&data->update_lock);
739 return count;
740}
741static ssize_t show_pwm_auto_pwm_minctl(struct device *dev, char *buf, int nr)
742{
743 struct lm85_data *data = lm85_update_device(dev);
744 return sprintf(buf,"%d\n", data->autofan[nr].min_off);
745}
746static ssize_t set_pwm_auto_pwm_minctl(struct device *dev, const char *buf,
747 size_t count, int nr)
748{
749 struct i2c_client *client = to_i2c_client(dev);
750 struct lm85_data *data = i2c_get_clientdata(client);
751 long val = simple_strtol(buf, NULL, 10);
752
753 down(&data->update_lock);
754 data->autofan[nr].min_off = val;
755 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, data->smooth[0]
756 | data->syncpwm3
757 | (data->autofan[0].min_off ? 0x20 : 0)
758 | (data->autofan[1].min_off ? 0x40 : 0)
759 | (data->autofan[2].min_off ? 0x80 : 0)
760 );
761 up(&data->update_lock);
762 return count;
763}
764static ssize_t show_pwm_auto_pwm_freq(struct device *dev, char *buf, int nr)
765{
766 struct lm85_data *data = lm85_update_device(dev);
767 return sprintf(buf,"%d\n", FREQ_FROM_REG(data->autofan[nr].freq));
768}
769static ssize_t set_pwm_auto_pwm_freq(struct device *dev, const char *buf,
770 size_t count, int nr)
771{
772 struct i2c_client *client = to_i2c_client(dev);
773 struct lm85_data *data = i2c_get_clientdata(client);
774 long val = simple_strtol(buf, NULL, 10);
775
776 down(&data->update_lock);
777 data->autofan[nr].freq = FREQ_TO_REG(val);
778 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
779 (data->zone[nr].range << 4)
780 | data->autofan[nr].freq
781 );
782 up(&data->update_lock);
783 return count;
784}
785#define pwm_auto(offset) \
786static ssize_t show_pwm##offset##_auto_channels (struct device *dev, struct device_attribute *attr, \
787 char *buf) \
788{ \
789 return show_pwm_auto_channels(dev, buf, offset - 1); \
790} \
791static ssize_t set_pwm##offset##_auto_channels (struct device *dev, struct device_attribute *attr, \
792 const char *buf, size_t count) \
793{ \
794 return set_pwm_auto_channels(dev, buf, count, offset - 1); \
795} \
796static ssize_t show_pwm##offset##_auto_pwm_min (struct device *dev, struct device_attribute *attr, \
797 char *buf) \
798{ \
799 return show_pwm_auto_pwm_min(dev, buf, offset - 1); \
800} \
801static ssize_t set_pwm##offset##_auto_pwm_min (struct device *dev, struct device_attribute *attr, \
802 const char *buf, size_t count) \
803{ \
804 return set_pwm_auto_pwm_min(dev, buf, count, offset - 1); \
805} \
806static ssize_t show_pwm##offset##_auto_pwm_minctl (struct device *dev, struct device_attribute *attr, \
807 char *buf) \
808{ \
809 return show_pwm_auto_pwm_minctl(dev, buf, offset - 1); \
810} \
811static ssize_t set_pwm##offset##_auto_pwm_minctl (struct device *dev, struct device_attribute *attr, \
812 const char *buf, size_t count) \
813{ \
814 return set_pwm_auto_pwm_minctl(dev, buf, count, offset - 1); \
815} \
816static ssize_t show_pwm##offset##_auto_pwm_freq (struct device *dev, struct device_attribute *attr, \
817 char *buf) \
818{ \
819 return show_pwm_auto_pwm_freq(dev, buf, offset - 1); \
820} \
821static ssize_t set_pwm##offset##_auto_pwm_freq(struct device *dev, struct device_attribute *attr, \
822 const char *buf, size_t count) \
823{ \
824 return set_pwm_auto_pwm_freq(dev, buf, count, offset - 1); \
825} \
826static DEVICE_ATTR(pwm##offset##_auto_channels, S_IRUGO | S_IWUSR, \
827 show_pwm##offset##_auto_channels, \
828 set_pwm##offset##_auto_channels); \
829static DEVICE_ATTR(pwm##offset##_auto_pwm_min, S_IRUGO | S_IWUSR, \
830 show_pwm##offset##_auto_pwm_min, \
831 set_pwm##offset##_auto_pwm_min); \
832static DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, S_IRUGO | S_IWUSR, \
833 show_pwm##offset##_auto_pwm_minctl, \
834 set_pwm##offset##_auto_pwm_minctl); \
835static DEVICE_ATTR(pwm##offset##_auto_pwm_freq, S_IRUGO | S_IWUSR, \
836 show_pwm##offset##_auto_pwm_freq, \
837 set_pwm##offset##_auto_pwm_freq);
838pwm_auto(1);
839pwm_auto(2);
840pwm_auto(3);
841
842/* Temperature settings for automatic PWM control */
843
844static ssize_t show_temp_auto_temp_off(struct device *dev, char *buf, int nr)
845{
846 struct lm85_data *data = lm85_update_device(dev);
847 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
848 HYST_FROM_REG(data->zone[nr].hyst));
849}
850static ssize_t set_temp_auto_temp_off(struct device *dev, const char *buf,
851 size_t count, int nr)
852{
853 struct i2c_client *client = to_i2c_client(dev);
854 struct lm85_data *data = i2c_get_clientdata(client);
855 int min;
856 long val = simple_strtol(buf, NULL, 10);
857
858 down(&data->update_lock);
859 min = TEMP_FROM_REG(data->zone[nr].limit);
860 data->zone[nr].off_desired = TEMP_TO_REG(val);
861 data->zone[nr].hyst = HYST_TO_REG(min - val);
862 if ( nr == 0 || nr == 1 ) {
863 lm85_write_value(client, LM85_REG_AFAN_HYST1,
864 (data->zone[0].hyst << 4)
865 | data->zone[1].hyst
866 );
867 } else {
868 lm85_write_value(client, LM85_REG_AFAN_HYST2,
869 (data->zone[2].hyst << 4)
870 );
871 }
872 up(&data->update_lock);
873 return count;
874}
875static ssize_t show_temp_auto_temp_min(struct device *dev, char *buf, int nr)
876{
877 struct lm85_data *data = lm85_update_device(dev);
878 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) );
879}
880static ssize_t set_temp_auto_temp_min(struct device *dev, const char *buf,
881 size_t count, int nr)
882{
883 struct i2c_client *client = to_i2c_client(dev);
884 struct lm85_data *data = i2c_get_clientdata(client);
885 long val = simple_strtol(buf, NULL, 10);
886
887 down(&data->update_lock);
888 data->zone[nr].limit = TEMP_TO_REG(val);
889 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
890 data->zone[nr].limit);
891
892/* Update temp_auto_max and temp_auto_range */
893 data->zone[nr].range = RANGE_TO_REG(
894 TEMP_FROM_REG(data->zone[nr].max_desired) -
895 TEMP_FROM_REG(data->zone[nr].limit));
896 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
897 ((data->zone[nr].range & 0x0f) << 4)
898 | (data->autofan[nr].freq & 0x07));
899
900/* Update temp_auto_hyst and temp_auto_off */
901 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
902 data->zone[nr].limit) - TEMP_FROM_REG(
903 data->zone[nr].off_desired));
904 if ( nr == 0 || nr == 1 ) {
905 lm85_write_value(client, LM85_REG_AFAN_HYST1,
906 (data->zone[0].hyst << 4)
907 | data->zone[1].hyst
908 );
909 } else {
910 lm85_write_value(client, LM85_REG_AFAN_HYST2,
911 (data->zone[2].hyst << 4)
912 );
913 }
914 up(&data->update_lock);
915 return count;
916}
917static ssize_t show_temp_auto_temp_max(struct device *dev, char *buf, int nr)
918{
919 struct lm85_data *data = lm85_update_device(dev);
920 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
921 RANGE_FROM_REG(data->zone[nr].range));
922}
923static ssize_t set_temp_auto_temp_max(struct device *dev, const char *buf,
924 size_t count, int nr)
925{
926 struct i2c_client *client = to_i2c_client(dev);
927 struct lm85_data *data = i2c_get_clientdata(client);
928 int min;
929 long val = simple_strtol(buf, NULL, 10);
930
931 down(&data->update_lock);
932 min = TEMP_FROM_REG(data->zone[nr].limit);
933 data->zone[nr].max_desired = TEMP_TO_REG(val);
934 data->zone[nr].range = RANGE_TO_REG(
935 val - min);
936 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
937 ((data->zone[nr].range & 0x0f) << 4)
938 | (data->autofan[nr].freq & 0x07));
939 up(&data->update_lock);
940 return count;
941}
942static ssize_t show_temp_auto_temp_crit(struct device *dev, char *buf, int nr)
943{
944 struct lm85_data *data = lm85_update_device(dev);
945 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].critical));
946}
947static ssize_t set_temp_auto_temp_crit(struct device *dev, const char *buf,
948 size_t count, int nr)
949{
950 struct i2c_client *client = to_i2c_client(dev);
951 struct lm85_data *data = i2c_get_clientdata(client);
952 long val = simple_strtol(buf, NULL, 10);
953
954 down(&data->update_lock);
955 data->zone[nr].critical = TEMP_TO_REG(val);
956 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
957 data->zone[nr].critical);
958 up(&data->update_lock);
959 return count;
960}
961#define temp_auto(offset) \
962static ssize_t show_temp##offset##_auto_temp_off (struct device *dev, struct device_attribute *attr, \
963 char *buf) \
964{ \
965 return show_temp_auto_temp_off(dev, buf, offset - 1); \
966} \
967static ssize_t set_temp##offset##_auto_temp_off (struct device *dev, struct device_attribute *attr, \
968 const char *buf, size_t count) \
969{ \
970 return set_temp_auto_temp_off(dev, buf, count, offset - 1); \
971} \
972static ssize_t show_temp##offset##_auto_temp_min (struct device *dev, struct device_attribute *attr, \
973 char *buf) \
974{ \
975 return show_temp_auto_temp_min(dev, buf, offset - 1); \
976} \
977static ssize_t set_temp##offset##_auto_temp_min (struct device *dev, struct device_attribute *attr, \
978 const char *buf, size_t count) \
979{ \
980 return set_temp_auto_temp_min(dev, buf, count, offset - 1); \
981} \
982static ssize_t show_temp##offset##_auto_temp_max (struct device *dev, struct device_attribute *attr, \
983 char *buf) \
984{ \
985 return show_temp_auto_temp_max(dev, buf, offset - 1); \
986} \
987static ssize_t set_temp##offset##_auto_temp_max (struct device *dev, struct device_attribute *attr, \
988 const char *buf, size_t count) \
989{ \
990 return set_temp_auto_temp_max(dev, buf, count, offset - 1); \
991} \
992static ssize_t show_temp##offset##_auto_temp_crit (struct device *dev, struct device_attribute *attr, \
993 char *buf) \
994{ \
995 return show_temp_auto_temp_crit(dev, buf, offset - 1); \
996} \
997static ssize_t set_temp##offset##_auto_temp_crit (struct device *dev, struct device_attribute *attr, \
998 const char *buf, size_t count) \
999{ \
1000 return set_temp_auto_temp_crit(dev, buf, count, offset - 1); \
1001} \
1002static DEVICE_ATTR(temp##offset##_auto_temp_off, S_IRUGO | S_IWUSR, \
1003 show_temp##offset##_auto_temp_off, \
1004 set_temp##offset##_auto_temp_off); \
1005static DEVICE_ATTR(temp##offset##_auto_temp_min, S_IRUGO | S_IWUSR, \
1006 show_temp##offset##_auto_temp_min, \
1007 set_temp##offset##_auto_temp_min); \
1008static DEVICE_ATTR(temp##offset##_auto_temp_max, S_IRUGO | S_IWUSR, \
1009 show_temp##offset##_auto_temp_max, \
1010 set_temp##offset##_auto_temp_max); \
1011static DEVICE_ATTR(temp##offset##_auto_temp_crit, S_IRUGO | S_IWUSR, \
1012 show_temp##offset##_auto_temp_crit, \
1013 set_temp##offset##_auto_temp_crit);
1014temp_auto(1);
1015temp_auto(2);
1016temp_auto(3);
1017
1018int lm85_attach_adapter(struct i2c_adapter *adapter)
1019{
1020 if (!(adapter->class & I2C_CLASS_HWMON))
1021 return 0;
1022 return i2c_detect(adapter, &addr_data, lm85_detect);
1023}
1024
1025int lm85_detect(struct i2c_adapter *adapter, int address,
1026 int kind)
1027{
1028 int company, verstep ;
1029 struct i2c_client *new_client = NULL;
1030 struct lm85_data *data;
1031 int err = 0;
1032 const char *type_name = "";
1033
1034 if (i2c_is_isa_adapter(adapter)) {
1035 /* This chip has no ISA interface */
1036 goto ERROR0 ;
1037 };
1038
1039 if (!i2c_check_functionality(adapter,
1040 I2C_FUNC_SMBUS_BYTE_DATA)) {
1041 /* We need to be able to do byte I/O */
1042 goto ERROR0 ;
1043 };
1044
1045 /* OK. For now, we presume we have a valid client. We now create the
1046 client structure, even though we cannot fill it completely yet.
1047 But it allows us to access lm85_{read,write}_value. */
1048
1049 if (!(data = kmalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
1050 err = -ENOMEM;
1051 goto ERROR0;
1052 }
1053 memset(data, 0, sizeof(struct lm85_data));
1054
1055 new_client = &data->client;
1056 i2c_set_clientdata(new_client, data);
1057 new_client->addr = address;
1058 new_client->adapter = adapter;
1059 new_client->driver = &lm85_driver;
1060 new_client->flags = 0;
1061
1062 /* Now, we do the remaining detection. */
1063
1064 company = lm85_read_value(new_client, LM85_REG_COMPANY);
1065 verstep = lm85_read_value(new_client, LM85_REG_VERSTEP);
1066
1067 dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
1068 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1069 i2c_adapter_id(new_client->adapter), new_client->addr,
1070 company, verstep);
1071
1072 /* If auto-detecting, Determine the chip type. */
1073 if (kind <= 0) {
1074 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n",
1075 i2c_adapter_id(adapter), address );
1076 if( company == LM85_COMPANY_NATIONAL
1077 && verstep == LM85_VERSTEP_LM85C ) {
1078 kind = lm85c ;
1079 } else if( company == LM85_COMPANY_NATIONAL
1080 && verstep == LM85_VERSTEP_LM85B ) {
1081 kind = lm85b ;
1082 } else if( company == LM85_COMPANY_NATIONAL
1083 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1084 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1085 " Defaulting to LM85.\n", verstep);
1086 kind = any_chip ;
1087 } else if( company == LM85_COMPANY_ANALOG_DEV
1088 && verstep == LM85_VERSTEP_ADM1027 ) {
1089 kind = adm1027 ;
1090 } else if( company == LM85_COMPANY_ANALOG_DEV
1091 && (verstep == LM85_VERSTEP_ADT7463
1092 || verstep == LM85_VERSTEP_ADT7463C) ) {
1093 kind = adt7463 ;
1094 } else if( company == LM85_COMPANY_ANALOG_DEV
1095 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1096 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1097 " Defaulting to Generic LM85.\n", verstep );
1098 kind = any_chip ;
1099 } else if( company == LM85_COMPANY_SMSC
1100 && (verstep == LM85_VERSTEP_EMC6D100_A0
1101 || verstep == LM85_VERSTEP_EMC6D100_A1) ) {
1102 /* Unfortunately, we can't tell a '100 from a '101
1103 * from the registers. Since a '101 is a '100
1104 * in a package with fewer pins and therefore no
1105 * 3.3V, 1.5V or 1.8V inputs, perhaps if those
1106 * inputs read 0, then it's a '101.
1107 */
1108 kind = emc6d100 ;
1109 } else if( company == LM85_COMPANY_SMSC
1110 && verstep == LM85_VERSTEP_EMC6D102) {
1111 kind = emc6d102 ;
1112 } else if( company == LM85_COMPANY_SMSC
1113 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1114 dev_err(&adapter->dev, "lm85: Detected SMSC chip\n");
1115 dev_err(&adapter->dev, "lm85: Unrecognized version/stepping 0x%02x"
1116 " Defaulting to Generic LM85.\n", verstep );
1117 kind = any_chip ;
1118 } else if( kind == any_chip
1119 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1120 dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n");
1121 /* Leave kind as "any_chip" */
1122 } else {
1123 dev_dbg(&adapter->dev, "Autodetection failed\n");
1124 /* Not an LM85 ... */
1125 if( kind == any_chip ) { /* User used force=x,y */
1126 dev_err(&adapter->dev, "Generic LM85 Version 6 not"
1127 " found at %d,0x%02x. Try force_lm85c.\n",
1128 i2c_adapter_id(adapter), address );
1129 }
1130 err = 0 ;
1131 goto ERROR1;
1132 }
1133 }
1134
1135 /* Fill in the chip specific driver values */
1136 if ( kind == any_chip ) {
1137 type_name = "lm85";
1138 } else if ( kind == lm85b ) {
1139 type_name = "lm85b";
1140 } else if ( kind == lm85c ) {
1141 type_name = "lm85c";
1142 } else if ( kind == adm1027 ) {
1143 type_name = "adm1027";
1144 } else if ( kind == adt7463 ) {
1145 type_name = "adt7463";
1146 } else if ( kind == emc6d100){
1147 type_name = "emc6d100";
1148 } else if ( kind == emc6d102 ) {
1149 type_name = "emc6d102";
1150 }
1151 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1152
1153 /* Fill in the remaining client fields */
1154 data->type = kind;
1155 data->valid = 0;
1156 init_MUTEX(&data->update_lock);
1157
1158 /* Tell the I2C layer a new client has arrived */
1159 if ((err = i2c_attach_client(new_client)))
1160 goto ERROR1;
1161
1162 /* Set the VRM version */
1163 data->vrm = i2c_which_vrm();
1164
1165 /* Initialize the LM85 chip */
1166 lm85_init_client(new_client);
1167
1168 /* Register sysfs hooks */
1169 device_create_file(&new_client->dev, &dev_attr_fan1_input);
1170 device_create_file(&new_client->dev, &dev_attr_fan2_input);
1171 device_create_file(&new_client->dev, &dev_attr_fan3_input);
1172 device_create_file(&new_client->dev, &dev_attr_fan4_input);
1173 device_create_file(&new_client->dev, &dev_attr_fan1_min);
1174 device_create_file(&new_client->dev, &dev_attr_fan2_min);
1175 device_create_file(&new_client->dev, &dev_attr_fan3_min);
1176 device_create_file(&new_client->dev, &dev_attr_fan4_min);
1177 device_create_file(&new_client->dev, &dev_attr_pwm1);
1178 device_create_file(&new_client->dev, &dev_attr_pwm2);
1179 device_create_file(&new_client->dev, &dev_attr_pwm3);
1180 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
1181 device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
1182 device_create_file(&new_client->dev, &dev_attr_pwm3_enable);
1183 device_create_file(&new_client->dev, &dev_attr_in0_input);
1184 device_create_file(&new_client->dev, &dev_attr_in1_input);
1185 device_create_file(&new_client->dev, &dev_attr_in2_input);
1186 device_create_file(&new_client->dev, &dev_attr_in3_input);
1187 device_create_file(&new_client->dev, &dev_attr_in4_input);
1188 device_create_file(&new_client->dev, &dev_attr_in0_min);
1189 device_create_file(&new_client->dev, &dev_attr_in1_min);
1190 device_create_file(&new_client->dev, &dev_attr_in2_min);
1191 device_create_file(&new_client->dev, &dev_attr_in3_min);
1192 device_create_file(&new_client->dev, &dev_attr_in4_min);
1193 device_create_file(&new_client->dev, &dev_attr_in0_max);
1194 device_create_file(&new_client->dev, &dev_attr_in1_max);
1195 device_create_file(&new_client->dev, &dev_attr_in2_max);
1196 device_create_file(&new_client->dev, &dev_attr_in3_max);
1197 device_create_file(&new_client->dev, &dev_attr_in4_max);
1198 device_create_file(&new_client->dev, &dev_attr_temp1_input);
1199 device_create_file(&new_client->dev, &dev_attr_temp2_input);
1200 device_create_file(&new_client->dev, &dev_attr_temp3_input);
1201 device_create_file(&new_client->dev, &dev_attr_temp1_min);
1202 device_create_file(&new_client->dev, &dev_attr_temp2_min);
1203 device_create_file(&new_client->dev, &dev_attr_temp3_min);
1204 device_create_file(&new_client->dev, &dev_attr_temp1_max);
1205 device_create_file(&new_client->dev, &dev_attr_temp2_max);
1206 device_create_file(&new_client->dev, &dev_attr_temp3_max);
1207 device_create_file(&new_client->dev, &dev_attr_vrm);
1208 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
1209 device_create_file(&new_client->dev, &dev_attr_alarms);
1210 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_channels);
1211 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_channels);
1212 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_channels);
1213 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_pwm_min);
1214 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_pwm_min);
1215 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_pwm_min);
1216 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_pwm_minctl);
1217 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_pwm_minctl);
1218 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_pwm_minctl);
1219 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_pwm_freq);
1220 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_pwm_freq);
1221 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_pwm_freq);
1222 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_off);
1223 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_off);
1224 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_off);
1225 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_min);
1226 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_min);
1227 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_min);
1228 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_max);
1229 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_max);
1230 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_max);
1231 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_crit);
1232 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_crit);
1233 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_crit);
1234
1235 return 0;
1236
1237 /* Error out and cleanup code */
1238 ERROR1:
1239 kfree(data);
1240 ERROR0:
1241 return err;
1242}
1243
1244int lm85_detach_client(struct i2c_client *client)
1245{
1246 i2c_detach_client(client);
1247 kfree(i2c_get_clientdata(client));
1248 return 0;
1249}
1250
1251
1252int lm85_read_value(struct i2c_client *client, u8 reg)
1253{
1254 int res;
1255
1256 /* What size location is it? */
1257 switch( reg ) {
1258 case LM85_REG_FAN(0) : /* Read WORD data */
1259 case LM85_REG_FAN(1) :
1260 case LM85_REG_FAN(2) :
1261 case LM85_REG_FAN(3) :
1262 case LM85_REG_FAN_MIN(0) :
1263 case LM85_REG_FAN_MIN(1) :
1264 case LM85_REG_FAN_MIN(2) :
1265 case LM85_REG_FAN_MIN(3) :
1266 case LM85_REG_ALARM1 : /* Read both bytes at once */
1267 res = i2c_smbus_read_byte_data(client, reg) & 0xff ;
1268 res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;
1269 break ;
1270 case ADT7463_REG_TMIN_CTL1 : /* Read WORD MSB, LSB */
1271 res = i2c_smbus_read_byte_data(client, reg) << 8 ;
1272 res |= i2c_smbus_read_byte_data(client, reg+1) & 0xff ;
1273 break ;
1274 default: /* Read BYTE data */
1275 res = i2c_smbus_read_byte_data(client, reg);
1276 break ;
1277 }
1278
1279 return res ;
1280}
1281
1282int lm85_write_value(struct i2c_client *client, u8 reg, int value)
1283{
1284 int res ;
1285
1286 switch( reg ) {
1287 case LM85_REG_FAN(0) : /* Write WORD data */
1288 case LM85_REG_FAN(1) :
1289 case LM85_REG_FAN(2) :
1290 case LM85_REG_FAN(3) :
1291 case LM85_REG_FAN_MIN(0) :
1292 case LM85_REG_FAN_MIN(1) :
1293 case LM85_REG_FAN_MIN(2) :
1294 case LM85_REG_FAN_MIN(3) :
1295 /* NOTE: ALARM is read only, so not included here */
1296 res = i2c_smbus_write_byte_data(client, reg, value & 0xff) ;
1297 res |= i2c_smbus_write_byte_data(client, reg+1, (value>>8) & 0xff) ;
1298 break ;
1299 case ADT7463_REG_TMIN_CTL1 : /* Write WORD MSB, LSB */
1300 res = i2c_smbus_write_byte_data(client, reg, (value>>8) & 0xff);
1301 res |= i2c_smbus_write_byte_data(client, reg+1, value & 0xff) ;
1302 break ;
1303 default: /* Write BYTE data */
1304 res = i2c_smbus_write_byte_data(client, reg, value);
1305 break ;
1306 }
1307
1308 return res ;
1309}
1310
1311void lm85_init_client(struct i2c_client *client)
1312{
1313 int value;
1314 struct lm85_data *data = i2c_get_clientdata(client);
1315
1316 dev_dbg(&client->dev, "Initializing device\n");
1317
1318 /* Warn if part was not "READY" */
1319 value = lm85_read_value(client, LM85_REG_CONFIG);
1320 dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value);
1321 if( value & 0x02 ) {
1322 dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n",
1323 i2c_adapter_id(client->adapter), client->addr );
1324 };
1325 if( ! (value & 0x04) ) {
1326 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
1327 i2c_adapter_id(client->adapter), client->addr );
1328 };
1329 if( value & 0x10
1330 && ( data->type == adm1027
1331 || data->type == adt7463 ) ) {
1332 dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set. "
1333 "Please report this to the lm85 maintainer.\n",
1334 i2c_adapter_id(client->adapter), client->addr );
1335 };
1336
1337 /* WE INTENTIONALLY make no changes to the limits,
1338 * offsets, pwms, fans and zones. If they were
1339 * configured, we don't want to mess with them.
1340 * If they weren't, the default is 100% PWM, no
1341 * control and will suffice until 'sensors -s'
1342 * can be run by the user.
1343 */
1344
1345 /* Start monitoring */
1346 value = lm85_read_value(client, LM85_REG_CONFIG);
1347 /* Try to clear LOCK, Set START, save everything else */
1348 value = (value & ~ 0x02) | 0x01 ;
1349 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
1350 lm85_write_value(client, LM85_REG_CONFIG, value);
1351}
1352
1353static struct lm85_data *lm85_update_device(struct device *dev)
1354{
1355 struct i2c_client *client = to_i2c_client(dev);
1356 struct lm85_data *data = i2c_get_clientdata(client);
1357 int i;
1358
1359 down(&data->update_lock);
1360
1361 if ( !data->valid ||
1362 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL) ) {
1363 /* Things that change quickly */
1364 dev_dbg(&client->dev, "Reading sensor values\n");
1365
1366 /* Have to read extended bits first to "freeze" the
1367 * more significant bits that are read later.
1368 */
1369 if ( (data->type == adm1027) || (data->type == adt7463) ) {
1370 int ext1 = lm85_read_value(client,
1371 ADM1027_REG_EXTEND_ADC1);
1372 int ext2 = lm85_read_value(client,
1373 ADM1027_REG_EXTEND_ADC2);
1374 int val = (ext1 << 8) + ext2;
1375
1376 for(i = 0; i <= 4; i++)
1377 data->in_ext[i] = (val>>(i * 2))&0x03;
1378
1379 for(i = 0; i <= 2; i++)
1380 data->temp_ext[i] = (val>>((i + 5) * 2))&0x03;
1381 }
1382
1383 /* adc_scale is 2^(number of LSBs). There are 4 extra bits in
1384 the emc6d102 and 2 in the adt7463 and adm1027. In all
1385 other chips ext is always 0 and the value of scale is
1386 irrelevant. So it is left in 4*/
1387 data->adc_scale = (data->type == emc6d102 ) ? 16 : 4;
1388
1389 for (i = 0; i <= 4; ++i) {
1390 data->in[i] =
1391 lm85_read_value(client, LM85_REG_IN(i));
1392 }
1393
1394 for (i = 0; i <= 3; ++i) {
1395 data->fan[i] =
1396 lm85_read_value(client, LM85_REG_FAN(i));
1397 }
1398
1399 for (i = 0; i <= 2; ++i) {
1400 data->temp[i] =
1401 lm85_read_value(client, LM85_REG_TEMP(i));
1402 }
1403
1404 for (i = 0; i <= 2; ++i) {
1405 data->pwm[i] =
1406 lm85_read_value(client, LM85_REG_PWM(i));
1407 }
1408
1409 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1410
1411 if ( data->type == adt7463 ) {
1412 if( data->therm_total < ULONG_MAX - 256 ) {
1413 data->therm_total +=
1414 lm85_read_value(client, ADT7463_REG_THERM );
1415 }
1416 } else if ( data->type == emc6d100 ) {
1417 /* Three more voltage sensors */
1418 for (i = 5; i <= 7; ++i) {
1419 data->in[i] =
1420 lm85_read_value(client, EMC6D100_REG_IN(i));
1421 }
1422 /* More alarm bits */
1423 data->alarms |=
1424 lm85_read_value(client, EMC6D100_REG_ALARM3) << 16;
1425 } else if (data->type == emc6d102 ) {
1426 /* Have to read LSB bits after the MSB ones because
1427 the reading of the MSB bits has frozen the
1428 LSBs (backward from the ADM1027).
1429 */
1430 int ext1 = lm85_read_value(client,
1431 EMC6D102_REG_EXTEND_ADC1);
1432 int ext2 = lm85_read_value(client,
1433 EMC6D102_REG_EXTEND_ADC2);
1434 int ext3 = lm85_read_value(client,
1435 EMC6D102_REG_EXTEND_ADC3);
1436 int ext4 = lm85_read_value(client,
1437 EMC6D102_REG_EXTEND_ADC4);
1438 data->in_ext[0] = ext3 & 0x0f;
1439 data->in_ext[1] = ext4 & 0x0f;
1440 data->in_ext[2] = (ext4 >> 4) & 0x0f;
1441 data->in_ext[3] = (ext3 >> 4) & 0x0f;
1442 data->in_ext[4] = (ext2 >> 4) & 0x0f;
1443
1444 data->temp_ext[0] = ext1 & 0x0f;
1445 data->temp_ext[1] = ext2 & 0x0f;
1446 data->temp_ext[2] = (ext1 >> 4) & 0x0f;
1447 }
1448
1449 data->last_reading = jiffies ;
1450 }; /* last_reading */
1451
1452 if ( !data->valid ||
1453 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL) ) {
1454 /* Things that don't change often */
1455 dev_dbg(&client->dev, "Reading config values\n");
1456
1457 for (i = 0; i <= 4; ++i) {
1458 data->in_min[i] =
1459 lm85_read_value(client, LM85_REG_IN_MIN(i));
1460 data->in_max[i] =
1461 lm85_read_value(client, LM85_REG_IN_MAX(i));
1462 }
1463
1464 if ( data->type == emc6d100 ) {
1465 for (i = 5; i <= 7; ++i) {
1466 data->in_min[i] =
1467 lm85_read_value(client, EMC6D100_REG_IN_MIN(i));
1468 data->in_max[i] =
1469 lm85_read_value(client, EMC6D100_REG_IN_MAX(i));
1470 }
1471 }
1472
1473 for (i = 0; i <= 3; ++i) {
1474 data->fan_min[i] =
1475 lm85_read_value(client, LM85_REG_FAN_MIN(i));
1476 }
1477
1478 for (i = 0; i <= 2; ++i) {
1479 data->temp_min[i] =
1480 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1481 data->temp_max[i] =
1482 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1483 }
1484
1485 data->vid = lm85_read_value(client, LM85_REG_VID);
1486
1487 for (i = 0; i <= 2; ++i) {
1488 int val ;
1489 data->autofan[i].config =
1490 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1491 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
1492 data->autofan[i].freq = val & 0x07 ;
1493 data->zone[i].range = (val >> 4) & 0x0f ;
1494 data->autofan[i].min_pwm =
1495 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1496 data->zone[i].limit =
1497 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1498 data->zone[i].critical =
1499 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1500 }
1501
1502 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1503 data->smooth[0] = i & 0x0f ;
1504 data->syncpwm3 = i & 0x10 ; /* Save PWM3 config */
1505 data->autofan[0].min_off = (i & 0x20) != 0 ;
1506 data->autofan[1].min_off = (i & 0x40) != 0 ;
1507 data->autofan[2].min_off = (i & 0x80) != 0 ;
1508 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE2);
1509 data->smooth[1] = (i>>4) & 0x0f ;
1510 data->smooth[2] = i & 0x0f ;
1511
1512 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1513 data->zone[0].hyst = (i>>4) & 0x0f ;
1514 data->zone[1].hyst = i & 0x0f ;
1515
1516 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1517 data->zone[2].hyst = (i>>4) & 0x0f ;
1518
1519 if ( (data->type == lm85b) || (data->type == lm85c) ) {
1520 data->tach_mode = lm85_read_value(client,
1521 LM85_REG_TACH_MODE );
1522 data->spinup_ctl = lm85_read_value(client,
1523 LM85_REG_SPINUP_CTL );
1524 } else if ( (data->type == adt7463) || (data->type == adm1027) ) {
1525 if ( data->type == adt7463 ) {
1526 for (i = 0; i <= 2; ++i) {
1527 data->oppoint[i] = lm85_read_value(client,
1528 ADT7463_REG_OPPOINT(i) );
1529 }
1530 data->tmin_ctl = lm85_read_value(client,
1531 ADT7463_REG_TMIN_CTL1 );
1532 data->therm_limit = lm85_read_value(client,
1533 ADT7463_REG_THERM_LIMIT );
1534 }
1535 for (i = 0; i <= 2; ++i) {
1536 data->temp_offset[i] = lm85_read_value(client,
1537 ADM1027_REG_TEMP_OFFSET(i) );
1538 }
1539 data->tach_mode = lm85_read_value(client,
1540 ADM1027_REG_CONFIG3 );
1541 data->fan_ppr = lm85_read_value(client,
1542 ADM1027_REG_FAN_PPR );
1543 }
1544
1545 data->last_config = jiffies;
1546 }; /* last_config */
1547
1548 data->valid = 1;
1549
1550 up(&data->update_lock);
1551
1552 return data;
1553}
1554
1555
1556static int __init sm_lm85_init(void)
1557{
1558 return i2c_add_driver(&lm85_driver);
1559}
1560
1561static void __exit sm_lm85_exit(void)
1562{
1563 i2c_del_driver(&lm85_driver);
1564}
1565
1566/* Thanks to Richard Barrington for adding the LM85 to sensors-detect.
1567 * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with
1568 * post 2.7.0 CVS changes.
1569 */
1570MODULE_LICENSE("GPL");
1571MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, Margit Schubert-While <margitsw@t-online.de>, Justin Thiessen <jthiessen@penguincomputing.com");
1572MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1573
1574module_init(sm_lm85_init);
1575module_exit(sm_lm85_exit);
diff --git a/drivers/hwmon/lm87.c b/drivers/hwmon/lm87.c
new file mode 100644
index 000000000000..1921ed1af182
--- /dev/null
+++ b/drivers/hwmon/lm87.c
@@ -0,0 +1,828 @@
1/*
2 * lm87.c
3 *
4 * Copyright (C) 2000 Frodo Looijaard <frodol@dds.nl>
5 * Philip Edelbrock <phil@netroedge.com>
6 * Stephen Rousset <stephen.rousset@rocketlogix.com>
7 * Dan Eaton <dan.eaton@rocketlogix.com>
8 * Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
9 *
10 * Original port to Linux 2.6 by Jeff Oliver.
11 *
12 * The LM87 is a sensor chip made by National Semiconductor. It monitors up
13 * to 8 voltages (including its own power source), up to three temperatures
14 * (its own plus up to two external ones) and up to two fans. The default
15 * configuration is 6 voltages, two temperatures and two fans (see below).
16 * Voltages are scaled internally with ratios such that the nominal value of
17 * each voltage correspond to a register value of 192 (which means a
18 * resolution of about 0.5% of the nominal value). Temperature values are
19 * reported with a 1 deg resolution and a 3-4 deg accuracy. Complete
20 * datasheet can be obtained from National's website at:
21 * http://www.national.com/pf/LM/LM87.html
22 *
23 * Some functions share pins, so not all functions are available at the same
24 * time. Which are depends on the hardware setup. This driver assumes that
25 * the BIOS configured the chip correctly. In that respect, it differs from
26 * the original driver (from lm_sensors for Linux 2.4), which would force the
27 * LM87 to an arbitrary, compile-time chosen mode, regardless of the actual
28 * chipset wiring.
29 * For reference, here is the list of exclusive functions:
30 * - in0+in5 (default) or temp3
31 * - fan1 (default) or in6
32 * - fan2 (default) or in7
33 * - VID lines (default) or IRQ lines (not handled by this driver)
34 *
35 * The LM87 additionally features an analog output, supposedly usable to
36 * control the speed of a fan. All new chips use pulse width modulation
37 * instead. The LM87 is the only hardware monitoring chipset I know of
38 * which uses amplitude modulation. Be careful when using this feature.
39 *
40 * This program is free software; you can redistribute it and/or modify
41 * it under the terms of the GNU General Public License as published by
42 * the Free Software Foundation; either version 2 of the License, or
43 * (at your option) any later version.
44 *
45 * This program is distributed in the hope that it will be useful,
46 * but WITHOUT ANY WARRANTY; without even the implied warranty of
47 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 * GNU General Public License for more details.
49 *
50 * You should have received a copy of the GNU General Public License
51 * along with this program; if not, write to the Free Software
52 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
53 */
54
55#include <linux/module.h>
56#include <linux/init.h>
57#include <linux/slab.h>
58#include <linux/jiffies.h>
59#include <linux/i2c.h>
60#include <linux/i2c-sensor.h>
61#include <linux/i2c-vid.h>
62
63/*
64 * Addresses to scan
65 * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e.
66 */
67
68static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
69static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
70
71/*
72 * Insmod parameters
73 */
74
75SENSORS_INSMOD_1(lm87);
76
77/*
78 * The LM87 registers
79 */
80
81/* nr in 0..5 */
82#define LM87_REG_IN(nr) (0x20 + (nr))
83#define LM87_REG_IN_MAX(nr) (0x2B + (nr) * 2)
84#define LM87_REG_IN_MIN(nr) (0x2C + (nr) * 2)
85/* nr in 0..1 */
86#define LM87_REG_AIN(nr) (0x28 + (nr))
87#define LM87_REG_AIN_MIN(nr) (0x1A + (nr))
88#define LM87_REG_AIN_MAX(nr) (0x3B + (nr))
89
90static u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 };
91static u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B };
92static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
93
94#define LM87_REG_TEMP_HW_INT_LOCK 0x13
95#define LM87_REG_TEMP_HW_EXT_LOCK 0x14
96#define LM87_REG_TEMP_HW_INT 0x17
97#define LM87_REG_TEMP_HW_EXT 0x18
98
99/* nr in 0..1 */
100#define LM87_REG_FAN(nr) (0x28 + (nr))
101#define LM87_REG_FAN_MIN(nr) (0x3B + (nr))
102#define LM87_REG_AOUT 0x19
103
104#define LM87_REG_CONFIG 0x40
105#define LM87_REG_CHANNEL_MODE 0x16
106#define LM87_REG_VID_FAN_DIV 0x47
107#define LM87_REG_VID4 0x49
108
109#define LM87_REG_ALARMS1 0x41
110#define LM87_REG_ALARMS2 0x42
111
112#define LM87_REG_COMPANY_ID 0x3E
113#define LM87_REG_REVISION 0x3F
114
115/*
116 * Conversions and various macros
117 * The LM87 uses signed 8-bit values for temperatures.
118 */
119
120#define IN_FROM_REG(reg,scale) (((reg) * (scale) + 96) / 192)
121#define IN_TO_REG(val,scale) ((val) <= 0 ? 0 : \
122 (val) * 192 >= (scale) * 255 ? 255 : \
123 ((val) * 192 + (scale)/2) / (scale))
124
125#define TEMP_FROM_REG(reg) ((reg) * 1000)
126#define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \
127 (val) >= 126500 ? 127 : \
128 (((val) < 0 ? (val)-500 : (val)+500) / 1000))
129
130#define FAN_FROM_REG(reg,div) ((reg) == 255 || (reg) == 0 ? 0 : \
131 1350000 + (reg)*(div) / 2) / ((reg)*(div))
132#define FAN_TO_REG(val,div) ((val)*(div) * 255 <= 1350000 ? 255 : \
133 (1350000 + (val)*(div) / 2) / ((val)*(div)))
134
135#define FAN_DIV_FROM_REG(reg) (1 << (reg))
136
137/* analog out is 9.80mV/LSB */
138#define AOUT_FROM_REG(reg) (((reg) * 98 + 5) / 10)
139#define AOUT_TO_REG(val) ((val) <= 0 ? 0 : \
140 (val) >= 2500 ? 255 : \
141 ((val) * 10 + 49) / 98)
142
143/* nr in 0..1 */
144#define CHAN_NO_FAN(nr) (1 << (nr))
145#define CHAN_TEMP3 (1 << 2)
146#define CHAN_VCC_5V (1 << 3)
147#define CHAN_NO_VID (1 << 8)
148
149/*
150 * Functions declaration
151 */
152
153static int lm87_attach_adapter(struct i2c_adapter *adapter);
154static int lm87_detect(struct i2c_adapter *adapter, int address, int kind);
155static void lm87_init_client(struct i2c_client *client);
156static int lm87_detach_client(struct i2c_client *client);
157static struct lm87_data *lm87_update_device(struct device *dev);
158
159/*
160 * Driver data (common to all clients)
161 */
162
163static struct i2c_driver lm87_driver = {
164 .owner = THIS_MODULE,
165 .name = "lm87",
166 .id = I2C_DRIVERID_LM87,
167 .flags = I2C_DF_NOTIFY,
168 .attach_adapter = lm87_attach_adapter,
169 .detach_client = lm87_detach_client,
170};
171
172/*
173 * Client data (each client gets its own)
174 */
175
176struct lm87_data {
177 struct i2c_client client;
178 struct semaphore update_lock;
179 char valid; /* zero until following fields are valid */
180 unsigned long last_updated; /* In jiffies */
181
182 u8 channel; /* register value */
183
184 u8 in[8]; /* register value */
185 u8 in_max[8]; /* register value */
186 u8 in_min[8]; /* register value */
187 u16 in_scale[8];
188
189 s8 temp[3]; /* register value */
190 s8 temp_high[3]; /* register value */
191 s8 temp_low[3]; /* register value */
192 s8 temp_crit_int; /* min of two register values */
193 s8 temp_crit_ext; /* min of two register values */
194
195 u8 fan[2]; /* register value */
196 u8 fan_min[2]; /* register value */
197 u8 fan_div[2]; /* register value, shifted right */
198 u8 aout; /* register value */
199
200 u16 alarms; /* register values, combined */
201 u8 vid; /* register values, combined */
202 u8 vrm;
203};
204
205/*
206 * Sysfs stuff
207 */
208
209static inline int lm87_read_value(struct i2c_client *client, u8 reg)
210{
211 return i2c_smbus_read_byte_data(client, reg);
212}
213
214static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
215{
216 return i2c_smbus_write_byte_data(client, reg, value);
217}
218
219#define show_in(offset) \
220static ssize_t show_in##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
221{ \
222 struct lm87_data *data = lm87_update_device(dev); \
223 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
224 data->in_scale[offset])); \
225} \
226static ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
227{ \
228 struct lm87_data *data = lm87_update_device(dev); \
229 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
230 data->in_scale[offset])); \
231} \
232static ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
233{ \
234 struct lm87_data *data = lm87_update_device(dev); \
235 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
236 data->in_scale[offset])); \
237} \
238static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
239 show_in##offset##_input, NULL);
240show_in(0);
241show_in(1);
242show_in(2);
243show_in(3);
244show_in(4);
245show_in(5);
246show_in(6);
247show_in(7);
248
249static void set_in_min(struct device *dev, const char *buf, int nr)
250{
251 struct i2c_client *client = to_i2c_client(dev);
252 struct lm87_data *data = i2c_get_clientdata(client);
253 long val = simple_strtol(buf, NULL, 10);
254
255 down(&data->update_lock);
256 data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
257 lm87_write_value(client, nr<6 ? LM87_REG_IN_MIN(nr) :
258 LM87_REG_AIN_MIN(nr-6), data->in_min[nr]);
259 up(&data->update_lock);
260}
261
262static void set_in_max(struct device *dev, const char *buf, int nr)
263{
264 struct i2c_client *client = to_i2c_client(dev);
265 struct lm87_data *data = i2c_get_clientdata(client);
266 long val = simple_strtol(buf, NULL, 10);
267
268 down(&data->update_lock);
269 data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
270 lm87_write_value(client, nr<6 ? LM87_REG_IN_MAX(nr) :
271 LM87_REG_AIN_MAX(nr-6), data->in_max[nr]);
272 up(&data->update_lock);
273}
274
275#define set_in(offset) \
276static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, \
277 const char *buf, size_t count) \
278{ \
279 set_in_min(dev, buf, offset); \
280 return count; \
281} \
282static ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, \
283 const char *buf, size_t count) \
284{ \
285 set_in_max(dev, buf, offset); \
286 return count; \
287} \
288static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
289 show_in##offset##_min, set_in##offset##_min); \
290static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
291 show_in##offset##_max, set_in##offset##_max);
292set_in(0);
293set_in(1);
294set_in(2);
295set_in(3);
296set_in(4);
297set_in(5);
298set_in(6);
299set_in(7);
300
301#define show_temp(offset) \
302static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
303{ \
304 struct lm87_data *data = lm87_update_device(dev); \
305 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
306} \
307static ssize_t show_temp##offset##_low(struct device *dev, struct device_attribute *attr, char *buf) \
308{ \
309 struct lm87_data *data = lm87_update_device(dev); \
310 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[offset-1])); \
311} \
312static ssize_t show_temp##offset##_high(struct device *dev, struct device_attribute *attr, char *buf) \
313{ \
314 struct lm87_data *data = lm87_update_device(dev); \
315 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[offset-1])); \
316}\
317static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
318 show_temp##offset##_input, NULL);
319show_temp(1);
320show_temp(2);
321show_temp(3);
322
323static void set_temp_low(struct device *dev, const char *buf, int nr)
324{
325 struct i2c_client *client = to_i2c_client(dev);
326 struct lm87_data *data = i2c_get_clientdata(client);
327 long val = simple_strtol(buf, NULL, 10);
328
329 down(&data->update_lock);
330 data->temp_low[nr] = TEMP_TO_REG(val);
331 lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
332 up(&data->update_lock);
333}
334
335static void set_temp_high(struct device *dev, const char *buf, int nr)
336{
337 struct i2c_client *client = to_i2c_client(dev);
338 struct lm87_data *data = i2c_get_clientdata(client);
339 long val = simple_strtol(buf, NULL, 10);
340
341 down(&data->update_lock);
342 data->temp_high[nr] = TEMP_TO_REG(val);
343 lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
344 up(&data->update_lock);
345}
346
347#define set_temp(offset) \
348static ssize_t set_temp##offset##_low(struct device *dev, struct device_attribute *attr, \
349 const char *buf, size_t count) \
350{ \
351 set_temp_low(dev, buf, offset-1); \
352 return count; \
353} \
354static ssize_t set_temp##offset##_high(struct device *dev, struct device_attribute *attr, \
355 const char *buf, size_t count) \
356{ \
357 set_temp_high(dev, buf, offset-1); \
358 return count; \
359} \
360static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
361 show_temp##offset##_high, set_temp##offset##_high); \
362static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
363 show_temp##offset##_low, set_temp##offset##_low);
364set_temp(1);
365set_temp(2);
366set_temp(3);
367
368static ssize_t show_temp_crit_int(struct device *dev, struct device_attribute *attr, char *buf)
369{
370 struct lm87_data *data = lm87_update_device(dev);
371 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
372}
373
374static ssize_t show_temp_crit_ext(struct device *dev, struct device_attribute *attr, char *buf)
375{
376 struct lm87_data *data = lm87_update_device(dev);
377 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
378}
379
380static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL);
381static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL);
382static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL);
383
384#define show_fan(offset) \
385static ssize_t show_fan##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
386{ \
387 struct lm87_data *data = lm87_update_device(dev); \
388 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[offset-1], \
389 FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
390} \
391static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
392{ \
393 struct lm87_data *data = lm87_update_device(dev); \
394 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[offset-1], \
395 FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
396} \
397static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
398{ \
399 struct lm87_data *data = lm87_update_device(dev); \
400 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[offset-1])); \
401} \
402static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
403 show_fan##offset##_input, NULL);
404show_fan(1);
405show_fan(2);
406
407static void set_fan_min(struct device *dev, const char *buf, int nr)
408{
409 struct i2c_client *client = to_i2c_client(dev);
410 struct lm87_data *data = i2c_get_clientdata(client);
411 long val = simple_strtol(buf, NULL, 10);
412
413 down(&data->update_lock);
414 data->fan_min[nr] = FAN_TO_REG(val,
415 FAN_DIV_FROM_REG(data->fan_div[nr]));
416 lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
417 up(&data->update_lock);
418}
419
420/* Note: we save and restore the fan minimum here, because its value is
421 determined in part by the fan clock divider. This follows the principle
422 of least suprise; the user doesn't expect the fan minimum to change just
423 because the divider changed. */
424static ssize_t set_fan_div(struct device *dev, const char *buf,
425 size_t count, int nr)
426{
427 struct i2c_client *client = to_i2c_client(dev);
428 struct lm87_data *data = i2c_get_clientdata(client);
429 long val = simple_strtol(buf, NULL, 10);
430 unsigned long min;
431 u8 reg;
432
433 down(&data->update_lock);
434 min = FAN_FROM_REG(data->fan_min[nr],
435 FAN_DIV_FROM_REG(data->fan_div[nr]));
436
437 switch (val) {
438 case 1: data->fan_div[nr] = 0; break;
439 case 2: data->fan_div[nr] = 1; break;
440 case 4: data->fan_div[nr] = 2; break;
441 case 8: data->fan_div[nr] = 3; break;
442 default:
443 up(&data->update_lock);
444 return -EINVAL;
445 }
446
447 reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
448 switch (nr) {
449 case 0:
450 reg = (reg & 0xCF) | (data->fan_div[0] << 4);
451 break;
452 case 1:
453 reg = (reg & 0x3F) | (data->fan_div[1] << 6);
454 break;
455 }
456 lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
457
458 data->fan_min[nr] = FAN_TO_REG(min, val);
459 lm87_write_value(client, LM87_REG_FAN_MIN(nr),
460 data->fan_min[nr]);
461 up(&data->update_lock);
462
463 return count;
464}
465
466#define set_fan(offset) \
467static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
468 size_t count) \
469{ \
470 set_fan_min(dev, buf, offset-1); \
471 return count; \
472} \
473static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \
474 size_t count) \
475{ \
476 return set_fan_div(dev, buf, count, offset-1); \
477} \
478static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
479 show_fan##offset##_min, set_fan##offset##_min); \
480static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
481 show_fan##offset##_div, set_fan##offset##_div);
482set_fan(1);
483set_fan(2);
484
485static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
486{
487 struct lm87_data *data = lm87_update_device(dev);
488 return sprintf(buf, "%d\n", data->alarms);
489}
490static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
491
492static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
493{
494 struct lm87_data *data = lm87_update_device(dev);
495 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
496}
497static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
498
499static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
500{
501 struct lm87_data *data = lm87_update_device(dev);
502 return sprintf(buf, "%d\n", data->vrm);
503}
504static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
505{
506 struct i2c_client *client = to_i2c_client(dev);
507 struct lm87_data *data = i2c_get_clientdata(client);
508 data->vrm = simple_strtoul(buf, NULL, 10);
509 return count;
510}
511static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
512
513static ssize_t show_aout(struct device *dev, struct device_attribute *attr, char *buf)
514{
515 struct lm87_data *data = lm87_update_device(dev);
516 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
517}
518static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
519{
520 struct i2c_client *client = to_i2c_client(dev);
521 struct lm87_data *data = i2c_get_clientdata(client);
522 long val = simple_strtol(buf, NULL, 10);
523
524 down(&data->update_lock);
525 data->aout = AOUT_TO_REG(val);
526 lm87_write_value(client, LM87_REG_AOUT, data->aout);
527 up(&data->update_lock);
528 return count;
529}
530static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
531
532/*
533 * Real code
534 */
535
536static int lm87_attach_adapter(struct i2c_adapter *adapter)
537{
538 if (!(adapter->class & I2C_CLASS_HWMON))
539 return 0;
540 return i2c_detect(adapter, &addr_data, lm87_detect);
541}
542
543/*
544 * The following function does more than just detection. If detection
545 * succeeds, it also registers the new chip.
546 */
547static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
548{
549 struct i2c_client *new_client;
550 struct lm87_data *data;
551 int err = 0;
552
553 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
554 goto exit;
555
556 if (!(data = kmalloc(sizeof(struct lm87_data), GFP_KERNEL))) {
557 err = -ENOMEM;
558 goto exit;
559 }
560 memset(data, 0, sizeof(struct lm87_data));
561
562 /* The common I2C client data is placed right before the
563 LM87-specific data. */
564 new_client = &data->client;
565 i2c_set_clientdata(new_client, data);
566 new_client->addr = address;
567 new_client->adapter = adapter;
568 new_client->driver = &lm87_driver;
569 new_client->flags = 0;
570
571 /* Default to an LM87 if forced */
572 if (kind == 0)
573 kind = lm87;
574
575 /* Now, we do the remaining detection. */
576 if (kind < 0) {
577 u8 rev = lm87_read_value(new_client, LM87_REG_REVISION);
578
579 if (rev < 0x01 || rev > 0x08
580 || (lm87_read_value(new_client, LM87_REG_CONFIG) & 0x80)
581 || lm87_read_value(new_client, LM87_REG_COMPANY_ID) != 0x02) {
582 dev_dbg(&adapter->dev,
583 "LM87 detection failed at 0x%02x.\n",
584 address);
585 goto exit_free;
586 }
587 }
588
589 /* We can fill in the remaining client fields */
590 strlcpy(new_client->name, "lm87", I2C_NAME_SIZE);
591 data->valid = 0;
592 init_MUTEX(&data->update_lock);
593
594 /* Tell the I2C layer a new client has arrived */
595 if ((err = i2c_attach_client(new_client)))
596 goto exit_free;
597
598 /* Initialize the LM87 chip */
599 lm87_init_client(new_client);
600
601 data->in_scale[0] = 2500;
602 data->in_scale[1] = 2700;
603 data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
604 data->in_scale[3] = 5000;
605 data->in_scale[4] = 12000;
606 data->in_scale[5] = 2700;
607 data->in_scale[6] = 1875;
608 data->in_scale[7] = 1875;
609
610 /* Register sysfs hooks */
611 device_create_file(&new_client->dev, &dev_attr_in1_input);
612 device_create_file(&new_client->dev, &dev_attr_in1_min);
613 device_create_file(&new_client->dev, &dev_attr_in1_max);
614 device_create_file(&new_client->dev, &dev_attr_in2_input);
615 device_create_file(&new_client->dev, &dev_attr_in2_min);
616 device_create_file(&new_client->dev, &dev_attr_in2_max);
617 device_create_file(&new_client->dev, &dev_attr_in3_input);
618 device_create_file(&new_client->dev, &dev_attr_in3_min);
619 device_create_file(&new_client->dev, &dev_attr_in3_max);
620 device_create_file(&new_client->dev, &dev_attr_in4_input);
621 device_create_file(&new_client->dev, &dev_attr_in4_min);
622 device_create_file(&new_client->dev, &dev_attr_in4_max);
623
624 if (data->channel & CHAN_NO_FAN(0)) {
625 device_create_file(&new_client->dev, &dev_attr_in6_input);
626 device_create_file(&new_client->dev, &dev_attr_in6_min);
627 device_create_file(&new_client->dev, &dev_attr_in6_max);
628 } else {
629 device_create_file(&new_client->dev, &dev_attr_fan1_input);
630 device_create_file(&new_client->dev, &dev_attr_fan1_min);
631 device_create_file(&new_client->dev, &dev_attr_fan1_div);
632 }
633 if (data->channel & CHAN_NO_FAN(1)) {
634 device_create_file(&new_client->dev, &dev_attr_in7_input);
635 device_create_file(&new_client->dev, &dev_attr_in7_min);
636 device_create_file(&new_client->dev, &dev_attr_in7_max);
637 } else {
638 device_create_file(&new_client->dev, &dev_attr_fan2_input);
639 device_create_file(&new_client->dev, &dev_attr_fan2_min);
640 device_create_file(&new_client->dev, &dev_attr_fan2_div);
641 }
642
643 device_create_file(&new_client->dev, &dev_attr_temp1_input);
644 device_create_file(&new_client->dev, &dev_attr_temp1_max);
645 device_create_file(&new_client->dev, &dev_attr_temp1_min);
646 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
647 device_create_file(&new_client->dev, &dev_attr_temp2_input);
648 device_create_file(&new_client->dev, &dev_attr_temp2_max);
649 device_create_file(&new_client->dev, &dev_attr_temp2_min);
650 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
651
652 if (data->channel & CHAN_TEMP3) {
653 device_create_file(&new_client->dev, &dev_attr_temp3_input);
654 device_create_file(&new_client->dev, &dev_attr_temp3_max);
655 device_create_file(&new_client->dev, &dev_attr_temp3_min);
656 device_create_file(&new_client->dev, &dev_attr_temp3_crit);
657 } else {
658 device_create_file(&new_client->dev, &dev_attr_in0_input);
659 device_create_file(&new_client->dev, &dev_attr_in0_min);
660 device_create_file(&new_client->dev, &dev_attr_in0_max);
661 device_create_file(&new_client->dev, &dev_attr_in5_input);
662 device_create_file(&new_client->dev, &dev_attr_in5_min);
663 device_create_file(&new_client->dev, &dev_attr_in5_max);
664 }
665
666 if (!(data->channel & CHAN_NO_VID)) {
667 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
668 device_create_file(&new_client->dev, &dev_attr_vrm);
669 }
670
671 device_create_file(&new_client->dev, &dev_attr_alarms);
672 device_create_file(&new_client->dev, &dev_attr_aout_output);
673
674 return 0;
675
676exit_free:
677 kfree(data);
678exit:
679 return err;
680}
681
682static void lm87_init_client(struct i2c_client *client)
683{
684 struct lm87_data *data = i2c_get_clientdata(client);
685 u8 config;
686
687 data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
688 data->vrm = i2c_which_vrm();
689
690 config = lm87_read_value(client, LM87_REG_CONFIG);
691 if (!(config & 0x01)) {
692 int i;
693
694 /* Limits are left uninitialized after power-up */
695 for (i = 1; i < 6; i++) {
696 lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
697 lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
698 }
699 for (i = 0; i < 2; i++) {
700 lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
701 lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
702 lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
703 lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
704 }
705 if (data->channel & CHAN_TEMP3) {
706 lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
707 lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
708 } else {
709 lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
710 lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
711 }
712 }
713 if ((config & 0x81) != 0x01) {
714 /* Start monitoring */
715 lm87_write_value(client, LM87_REG_CONFIG,
716 (config & 0xF7) | 0x01);
717 }
718}
719
720static int lm87_detach_client(struct i2c_client *client)
721{
722 int err;
723
724 if ((err = i2c_detach_client(client))) {
725 dev_err(&client->dev, "Client deregistration failed, "
726 "client not detached.\n");
727 return err;
728 }
729
730 kfree(i2c_get_clientdata(client));
731 return 0;
732}
733
734static struct lm87_data *lm87_update_device(struct device *dev)
735{
736 struct i2c_client *client = to_i2c_client(dev);
737 struct lm87_data *data = i2c_get_clientdata(client);
738
739 down(&data->update_lock);
740
741 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
742 int i, j;
743
744 dev_dbg(&client->dev, "Updating data.\n");
745
746 i = (data->channel & CHAN_TEMP3) ? 1 : 0;
747 j = (data->channel & CHAN_TEMP3) ? 5 : 6;
748 for (; i < j; i++) {
749 data->in[i] = lm87_read_value(client,
750 LM87_REG_IN(i));
751 data->in_min[i] = lm87_read_value(client,
752 LM87_REG_IN_MIN(i));
753 data->in_max[i] = lm87_read_value(client,
754 LM87_REG_IN_MAX(i));
755 }
756
757 for (i = 0; i < 2; i++) {
758 if (data->channel & CHAN_NO_FAN(i)) {
759 data->in[6+i] = lm87_read_value(client,
760 LM87_REG_AIN(i));
761 data->in_max[6+i] = lm87_read_value(client,
762 LM87_REG_AIN_MAX(i));
763 data->in_min[6+i] = lm87_read_value(client,
764 LM87_REG_AIN_MIN(i));
765
766 } else {
767 data->fan[i] = lm87_read_value(client,
768 LM87_REG_FAN(i));
769 data->fan_min[i] = lm87_read_value(client,
770 LM87_REG_FAN_MIN(i));
771 }
772 }
773
774 j = (data->channel & CHAN_TEMP3) ? 3 : 2;
775 for (i = 0 ; i < j; i++) {
776 data->temp[i] = lm87_read_value(client,
777 LM87_REG_TEMP[i]);
778 data->temp_high[i] = lm87_read_value(client,
779 LM87_REG_TEMP_HIGH[i]);
780 data->temp_low[i] = lm87_read_value(client,
781 LM87_REG_TEMP_LOW[i]);
782 }
783
784 i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
785 j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
786 data->temp_crit_int = min(i, j);
787
788 i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
789 j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
790 data->temp_crit_ext = min(i, j);
791
792 i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
793 data->fan_div[0] = (i >> 4) & 0x03;
794 data->fan_div[1] = (i >> 6) & 0x03;
795 data->vid = (i & 0x0F)
796 | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
797 << 4;
798
799 data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
800 | (lm87_read_value(client, LM87_REG_ALARMS2)
801 << 8);
802 data->aout = lm87_read_value(client, LM87_REG_AOUT);
803
804 data->last_updated = jiffies;
805 data->valid = 1;
806 }
807
808 up(&data->update_lock);
809
810 return data;
811}
812
813static int __init sensors_lm87_init(void)
814{
815 return i2c_add_driver(&lm87_driver);
816}
817
818static void __exit sensors_lm87_exit(void)
819{
820 i2c_del_driver(&lm87_driver);
821}
822
823MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org> and others");
824MODULE_DESCRIPTION("LM87 driver");
825MODULE_LICENSE("GPL");
826
827module_init(sensors_lm87_init);
828module_exit(sensors_lm87_exit);
diff --git a/drivers/hwmon/lm90.c b/drivers/hwmon/lm90.c
new file mode 100644
index 000000000000..a67dcadf7cb0
--- /dev/null
+++ b/drivers/hwmon/lm90.c
@@ -0,0 +1,655 @@
1/*
2 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 2003-2005 Jean Delvare <khali@linux-fr.org>
5 *
6 * Based on the lm83 driver. The LM90 is a sensor chip made by National
7 * Semiconductor. It reports up to two temperatures (its own plus up to
8 * one external one) with a 0.125 deg resolution (1 deg for local
9 * temperature) and a 3-4 deg accuracy. Complete datasheet can be
10 * obtained from National's website at:
11 * http://www.national.com/pf/LM/LM90.html
12 *
13 * This driver also supports the LM89 and LM99, two other sensor chips
14 * made by National Semiconductor. Both have an increased remote
15 * temperature measurement accuracy (1 degree), and the LM99
16 * additionally shifts remote temperatures (measured and limits) by 16
17 * degrees, which allows for higher temperatures measurement. The
18 * driver doesn't handle it since it can be done easily in user-space.
19 * Complete datasheets can be obtained from National's website at:
20 * http://www.national.com/pf/LM/LM89.html
21 * http://www.national.com/pf/LM/LM99.html
22 * Note that there is no way to differentiate between both chips.
23 *
24 * This driver also supports the LM86, another sensor chip made by
25 * National Semiconductor. It is exactly similar to the LM90 except it
26 * has a higher accuracy.
27 * Complete datasheet can be obtained from National's website at:
28 * http://www.national.com/pf/LM/LM86.html
29 *
30 * This driver also supports the ADM1032, a sensor chip made by Analog
31 * Devices. That chip is similar to the LM90, with a few differences
32 * that are not handled by this driver. Complete datasheet can be
33 * obtained from Analog's website at:
34 * http://products.analog.com/products/info.asp?product=ADM1032
35 * Among others, it has a higher accuracy than the LM90, much like the
36 * LM86 does.
37 *
38 * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
39 * chips made by Maxim. These chips are similar to the LM86. Complete
40 * datasheet can be obtained at Maxim's website at:
41 * http://www.maxim-ic.com/quick_view2.cfm/qv_pk/2578
42 * Note that there is no easy way to differentiate between the three
43 * variants. The extra address and features of the MAX6659 are not
44 * supported by this driver.
45 *
46 * This driver also supports the ADT7461 chip from Analog Devices but
47 * only in its "compatability mode". If an ADT7461 chip is found but
48 * is configured in non-compatible mode (where its temperature
49 * register values are decoded differently) it is ignored by this
50 * driver. Complete datasheet can be obtained from Analog's website
51 * at:
52 * http://products.analog.com/products/info.asp?product=ADT7461
53 *
54 * Since the LM90 was the first chipset supported by this driver, most
55 * comments will refer to this chipset, but are actually general and
56 * concern all supported chipsets, unless mentioned otherwise.
57 *
58 * This program is free software; you can redistribute it and/or modify
59 * it under the terms of the GNU General Public License as published by
60 * the Free Software Foundation; either version 2 of the License, or
61 * (at your option) any later version.
62 *
63 * This program is distributed in the hope that it will be useful,
64 * but WITHOUT ANY WARRANTY; without even the implied warranty of
65 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
66 * GNU General Public License for more details.
67 *
68 * You should have received a copy of the GNU General Public License
69 * along with this program; if not, write to the Free Software
70 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
71 */
72
73#include <linux/module.h>
74#include <linux/init.h>
75#include <linux/slab.h>
76#include <linux/jiffies.h>
77#include <linux/i2c.h>
78#include <linux/i2c-sensor.h>
79#include <linux/hwmon-sysfs.h>
80
81/*
82 * Addresses to scan
83 * Address is fully defined internally and cannot be changed except for
84 * MAX6659.
85 * LM86, LM89, LM90, LM99, ADM1032, MAX6657 and MAX6658 have address 0x4c.
86 * LM89-1, and LM99-1 have address 0x4d.
87 * MAX6659 can have address 0x4c, 0x4d or 0x4e (unsupported).
88 * ADT7461 always has address 0x4c.
89 */
90
91static unsigned short normal_i2c[] = { 0x4c, 0x4d, I2C_CLIENT_END };
92static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
93
94/*
95 * Insmod parameters
96 */
97
98SENSORS_INSMOD_6(lm90, adm1032, lm99, lm86, max6657, adt7461);
99
100/*
101 * The LM90 registers
102 */
103
104#define LM90_REG_R_MAN_ID 0xFE
105#define LM90_REG_R_CHIP_ID 0xFF
106#define LM90_REG_R_CONFIG1 0x03
107#define LM90_REG_W_CONFIG1 0x09
108#define LM90_REG_R_CONFIG2 0xBF
109#define LM90_REG_W_CONFIG2 0xBF
110#define LM90_REG_R_CONVRATE 0x04
111#define LM90_REG_W_CONVRATE 0x0A
112#define LM90_REG_R_STATUS 0x02
113#define LM90_REG_R_LOCAL_TEMP 0x00
114#define LM90_REG_R_LOCAL_HIGH 0x05
115#define LM90_REG_W_LOCAL_HIGH 0x0B
116#define LM90_REG_R_LOCAL_LOW 0x06
117#define LM90_REG_W_LOCAL_LOW 0x0C
118#define LM90_REG_R_LOCAL_CRIT 0x20
119#define LM90_REG_W_LOCAL_CRIT 0x20
120#define LM90_REG_R_REMOTE_TEMPH 0x01
121#define LM90_REG_R_REMOTE_TEMPL 0x10
122#define LM90_REG_R_REMOTE_OFFSH 0x11
123#define LM90_REG_W_REMOTE_OFFSH 0x11
124#define LM90_REG_R_REMOTE_OFFSL 0x12
125#define LM90_REG_W_REMOTE_OFFSL 0x12
126#define LM90_REG_R_REMOTE_HIGHH 0x07
127#define LM90_REG_W_REMOTE_HIGHH 0x0D
128#define LM90_REG_R_REMOTE_HIGHL 0x13
129#define LM90_REG_W_REMOTE_HIGHL 0x13
130#define LM90_REG_R_REMOTE_LOWH 0x08
131#define LM90_REG_W_REMOTE_LOWH 0x0E
132#define LM90_REG_R_REMOTE_LOWL 0x14
133#define LM90_REG_W_REMOTE_LOWL 0x14
134#define LM90_REG_R_REMOTE_CRIT 0x19
135#define LM90_REG_W_REMOTE_CRIT 0x19
136#define LM90_REG_R_TCRIT_HYST 0x21
137#define LM90_REG_W_TCRIT_HYST 0x21
138
139/*
140 * Conversions and various macros
141 * For local temperatures and limits, critical limits and the hysteresis
142 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
143 * For remote temperatures and limits, it uses signed 11-bit values with
144 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
145 */
146
147#define TEMP1_FROM_REG(val) ((val) * 1000)
148#define TEMP1_TO_REG(val) ((val) <= -128000 ? -128 : \
149 (val) >= 127000 ? 127 : \
150 (val) < 0 ? ((val) - 500) / 1000 : \
151 ((val) + 500) / 1000)
152#define TEMP2_FROM_REG(val) ((val) / 32 * 125)
153#define TEMP2_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
154 (val) >= 127875 ? 0x7FE0 : \
155 (val) < 0 ? ((val) - 62) / 125 * 32 : \
156 ((val) + 62) / 125 * 32)
157#define HYST_TO_REG(val) ((val) <= 0 ? 0 : (val) >= 30500 ? 31 : \
158 ((val) + 500) / 1000)
159
160/*
161 * ADT7461 is almost identical to LM90 except that attempts to write
162 * values that are outside the range 0 < temp < 127 are treated as
163 * the boundary value.
164 */
165
166#define TEMP1_TO_REG_ADT7461(val) ((val) <= 0 ? 0 : \
167 (val) >= 127000 ? 127 : \
168 ((val) + 500) / 1000)
169#define TEMP2_TO_REG_ADT7461(val) ((val) <= 0 ? 0 : \
170 (val) >= 127750 ? 0x7FC0 : \
171 ((val) + 125) / 250 * 64)
172
173/*
174 * Functions declaration
175 */
176
177static int lm90_attach_adapter(struct i2c_adapter *adapter);
178static int lm90_detect(struct i2c_adapter *adapter, int address,
179 int kind);
180static void lm90_init_client(struct i2c_client *client);
181static int lm90_detach_client(struct i2c_client *client);
182static struct lm90_data *lm90_update_device(struct device *dev);
183
184/*
185 * Driver data (common to all clients)
186 */
187
188static struct i2c_driver lm90_driver = {
189 .owner = THIS_MODULE,
190 .name = "lm90",
191 .id = I2C_DRIVERID_LM90,
192 .flags = I2C_DF_NOTIFY,
193 .attach_adapter = lm90_attach_adapter,
194 .detach_client = lm90_detach_client,
195};
196
197/*
198 * Client data (each client gets its own)
199 */
200
201struct lm90_data {
202 struct i2c_client client;
203 struct semaphore update_lock;
204 char valid; /* zero until following fields are valid */
205 unsigned long last_updated; /* in jiffies */
206 int kind;
207
208 /* registers values */
209 s8 temp8[5]; /* 0: local input
210 1: local low limit
211 2: local high limit
212 3: local critical limit
213 4: remote critical limit */
214 s16 temp11[3]; /* 0: remote input
215 1: remote low limit
216 2: remote high limit */
217 u8 temp_hyst;
218 u8 alarms; /* bitvector */
219};
220
221/*
222 * Sysfs stuff
223 */
224
225static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
226 char *buf)
227{
228 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
229 struct lm90_data *data = lm90_update_device(dev);
230 return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp8[attr->index]));
231}
232
233static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
234 const char *buf, size_t count)
235{
236 static const u8 reg[4] = {
237 LM90_REG_W_LOCAL_LOW,
238 LM90_REG_W_LOCAL_HIGH,
239 LM90_REG_W_LOCAL_CRIT,
240 LM90_REG_W_REMOTE_CRIT,
241 };
242
243 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
244 struct i2c_client *client = to_i2c_client(dev);
245 struct lm90_data *data = i2c_get_clientdata(client);
246 long val = simple_strtol(buf, NULL, 10);
247 int nr = attr->index;
248
249 down(&data->update_lock);
250 if (data->kind == adt7461)
251 data->temp8[nr] = TEMP1_TO_REG_ADT7461(val);
252 else
253 data->temp8[nr] = TEMP1_TO_REG(val);
254 i2c_smbus_write_byte_data(client, reg[nr - 1], data->temp8[nr]);
255 up(&data->update_lock);
256 return count;
257}
258
259static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
260 char *buf)
261{
262 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
263 struct lm90_data *data = lm90_update_device(dev);
264 return sprintf(buf, "%d\n", TEMP2_FROM_REG(data->temp11[attr->index]));
265}
266
267static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
268 const char *buf, size_t count)
269{
270 static const u8 reg[4] = {
271 LM90_REG_W_REMOTE_LOWH,
272 LM90_REG_W_REMOTE_LOWL,
273 LM90_REG_W_REMOTE_HIGHH,
274 LM90_REG_W_REMOTE_HIGHL,
275 };
276
277 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
278 struct i2c_client *client = to_i2c_client(dev);
279 struct lm90_data *data = i2c_get_clientdata(client);
280 long val = simple_strtol(buf, NULL, 10);
281 int nr = attr->index;
282
283 down(&data->update_lock);
284 if (data->kind == adt7461)
285 data->temp11[nr] = TEMP2_TO_REG_ADT7461(val);
286 else
287 data->temp11[nr] = TEMP2_TO_REG(val);
288 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
289 data->temp11[nr] >> 8);
290 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
291 data->temp11[nr] & 0xff);
292 up(&data->update_lock);
293 return count;
294}
295
296static ssize_t show_temphyst(struct device *dev, struct device_attribute *devattr,
297 char *buf)
298{
299 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
300 struct lm90_data *data = lm90_update_device(dev);
301 return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp8[attr->index])
302 - TEMP1_FROM_REG(data->temp_hyst));
303}
304
305static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
306 const char *buf, size_t count)
307{
308 struct i2c_client *client = to_i2c_client(dev);
309 struct lm90_data *data = i2c_get_clientdata(client);
310 long val = simple_strtol(buf, NULL, 10);
311 long hyst;
312
313 down(&data->update_lock);
314 hyst = TEMP1_FROM_REG(data->temp8[3]) - val;
315 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
316 HYST_TO_REG(hyst));
317 up(&data->update_lock);
318 return count;
319}
320
321static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
322 char *buf)
323{
324 struct lm90_data *data = lm90_update_device(dev);
325 return sprintf(buf, "%d\n", data->alarms);
326}
327
328static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp8, NULL, 0);
329static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
330static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
331 set_temp8, 1);
332static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
333 set_temp11, 1);
334static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
335 set_temp8, 2);
336static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
337 set_temp11, 2);
338static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
339 set_temp8, 3);
340static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
341 set_temp8, 4);
342static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
343 set_temphyst, 3);
344static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 4);
345static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
346
347/*
348 * Real code
349 */
350
351static int lm90_attach_adapter(struct i2c_adapter *adapter)
352{
353 if (!(adapter->class & I2C_CLASS_HWMON))
354 return 0;
355 return i2c_detect(adapter, &addr_data, lm90_detect);
356}
357
358/*
359 * The following function does more than just detection. If detection
360 * succeeds, it also registers the new chip.
361 */
362static int lm90_detect(struct i2c_adapter *adapter, int address, int kind)
363{
364 struct i2c_client *new_client;
365 struct lm90_data *data;
366 int err = 0;
367 const char *name = "";
368
369 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
370 goto exit;
371
372 if (!(data = kmalloc(sizeof(struct lm90_data), GFP_KERNEL))) {
373 err = -ENOMEM;
374 goto exit;
375 }
376 memset(data, 0, sizeof(struct lm90_data));
377
378 /* The common I2C client data is placed right before the
379 LM90-specific data. */
380 new_client = &data->client;
381 i2c_set_clientdata(new_client, data);
382 new_client->addr = address;
383 new_client->adapter = adapter;
384 new_client->driver = &lm90_driver;
385 new_client->flags = 0;
386
387 /*
388 * Now we do the remaining detection. A negative kind means that
389 * the driver was loaded with no force parameter (default), so we
390 * must both detect and identify the chip. A zero kind means that
391 * the driver was loaded with the force parameter, the detection
392 * step shall be skipped. A positive kind means that the driver
393 * was loaded with the force parameter and a given kind of chip is
394 * requested, so both the detection and the identification steps
395 * are skipped.
396 */
397
398 /* Default to an LM90 if forced */
399 if (kind == 0)
400 kind = lm90;
401
402 if (kind < 0) { /* detection and identification */
403 u8 man_id, chip_id, reg_config1, reg_convrate;
404
405 man_id = i2c_smbus_read_byte_data(new_client,
406 LM90_REG_R_MAN_ID);
407 chip_id = i2c_smbus_read_byte_data(new_client,
408 LM90_REG_R_CHIP_ID);
409 reg_config1 = i2c_smbus_read_byte_data(new_client,
410 LM90_REG_R_CONFIG1);
411 reg_convrate = i2c_smbus_read_byte_data(new_client,
412 LM90_REG_R_CONVRATE);
413
414 if (man_id == 0x01) { /* National Semiconductor */
415 u8 reg_config2;
416
417 reg_config2 = i2c_smbus_read_byte_data(new_client,
418 LM90_REG_R_CONFIG2);
419
420 if ((reg_config1 & 0x2A) == 0x00
421 && (reg_config2 & 0xF8) == 0x00
422 && reg_convrate <= 0x09) {
423 if (address == 0x4C
424 && (chip_id & 0xF0) == 0x20) { /* LM90 */
425 kind = lm90;
426 } else
427 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
428 kind = lm99;
429 } else
430 if (address == 0x4C
431 && (chip_id & 0xF0) == 0x10) { /* LM86 */
432 kind = lm86;
433 }
434 }
435 } else
436 if (man_id == 0x41) { /* Analog Devices */
437 if (address == 0x4C
438 && (chip_id & 0xF0) == 0x40 /* ADM1032 */
439 && (reg_config1 & 0x3F) == 0x00
440 && reg_convrate <= 0x0A) {
441 kind = adm1032;
442 } else
443 if (address == 0x4c
444 && chip_id == 0x51 /* ADT7461 */
445 && (reg_config1 & 0x1F) == 0x00 /* check compat mode */
446 && reg_convrate <= 0x0A) {
447 kind = adt7461;
448 }
449 } else
450 if (man_id == 0x4D) { /* Maxim */
451 /*
452 * The Maxim variants do NOT have a chip_id register.
453 * Reading from that address will return the last read
454 * value, which in our case is those of the man_id
455 * register. Likewise, the config1 register seems to
456 * lack a low nibble, so the value will be those of the
457 * previous read, so in our case those of the man_id
458 * register.
459 */
460 if (chip_id == man_id
461 && (reg_config1 & 0x1F) == (man_id & 0x0F)
462 && reg_convrate <= 0x09) {
463 kind = max6657;
464 }
465 }
466
467 if (kind <= 0) { /* identification failed */
468 dev_info(&adapter->dev,
469 "Unsupported chip (man_id=0x%02X, "
470 "chip_id=0x%02X).\n", man_id, chip_id);
471 goto exit_free;
472 }
473 }
474
475 if (kind == lm90) {
476 name = "lm90";
477 } else if (kind == adm1032) {
478 name = "adm1032";
479 } else if (kind == lm99) {
480 name = "lm99";
481 } else if (kind == lm86) {
482 name = "lm86";
483 } else if (kind == max6657) {
484 name = "max6657";
485 } else if (kind == adt7461) {
486 name = "adt7461";
487 }
488
489 /* We can fill in the remaining client fields */
490 strlcpy(new_client->name, name, I2C_NAME_SIZE);
491 data->valid = 0;
492 data->kind = kind;
493 init_MUTEX(&data->update_lock);
494
495 /* Tell the I2C layer a new client has arrived */
496 if ((err = i2c_attach_client(new_client)))
497 goto exit_free;
498
499 /* Initialize the LM90 chip */
500 lm90_init_client(new_client);
501
502 /* Register sysfs hooks */
503 device_create_file(&new_client->dev,
504 &sensor_dev_attr_temp1_input.dev_attr);
505 device_create_file(&new_client->dev,
506 &sensor_dev_attr_temp2_input.dev_attr);
507 device_create_file(&new_client->dev,
508 &sensor_dev_attr_temp1_min.dev_attr);
509 device_create_file(&new_client->dev,
510 &sensor_dev_attr_temp2_min.dev_attr);
511 device_create_file(&new_client->dev,
512 &sensor_dev_attr_temp1_max.dev_attr);
513 device_create_file(&new_client->dev,
514 &sensor_dev_attr_temp2_max.dev_attr);
515 device_create_file(&new_client->dev,
516 &sensor_dev_attr_temp1_crit.dev_attr);
517 device_create_file(&new_client->dev,
518 &sensor_dev_attr_temp2_crit.dev_attr);
519 device_create_file(&new_client->dev,
520 &sensor_dev_attr_temp1_crit_hyst.dev_attr);
521 device_create_file(&new_client->dev,
522 &sensor_dev_attr_temp2_crit_hyst.dev_attr);
523 device_create_file(&new_client->dev, &dev_attr_alarms);
524
525 return 0;
526
527exit_free:
528 kfree(data);
529exit:
530 return err;
531}
532
533static void lm90_init_client(struct i2c_client *client)
534{
535 u8 config;
536
537 /*
538 * Start the conversions.
539 */
540 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
541 5); /* 2 Hz */
542 config = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1);
543 if (config & 0x40)
544 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
545 config & 0xBF); /* run */
546}
547
548static int lm90_detach_client(struct i2c_client *client)
549{
550 int err;
551
552 if ((err = i2c_detach_client(client))) {
553 dev_err(&client->dev, "Client deregistration failed, "
554 "client not detached.\n");
555 return err;
556 }
557
558 kfree(i2c_get_clientdata(client));
559 return 0;
560}
561
562static struct lm90_data *lm90_update_device(struct device *dev)
563{
564 struct i2c_client *client = to_i2c_client(dev);
565 struct lm90_data *data = i2c_get_clientdata(client);
566
567 down(&data->update_lock);
568
569 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
570 u8 oldh, newh;
571
572 dev_dbg(&client->dev, "Updating lm90 data.\n");
573 data->temp8[0] = i2c_smbus_read_byte_data(client,
574 LM90_REG_R_LOCAL_TEMP);
575 data->temp8[1] = i2c_smbus_read_byte_data(client,
576 LM90_REG_R_LOCAL_LOW);
577 data->temp8[2] = i2c_smbus_read_byte_data(client,
578 LM90_REG_R_LOCAL_HIGH);
579 data->temp8[3] = i2c_smbus_read_byte_data(client,
580 LM90_REG_R_LOCAL_CRIT);
581 data->temp8[4] = i2c_smbus_read_byte_data(client,
582 LM90_REG_R_REMOTE_CRIT);
583 data->temp_hyst = i2c_smbus_read_byte_data(client,
584 LM90_REG_R_TCRIT_HYST);
585
586 /*
587 * There is a trick here. We have to read two registers to
588 * have the remote sensor temperature, but we have to beware
589 * a conversion could occur inbetween the readings. The
590 * datasheet says we should either use the one-shot
591 * conversion register, which we don't want to do (disables
592 * hardware monitoring) or monitor the busy bit, which is
593 * impossible (we can't read the values and monitor that bit
594 * at the exact same time). So the solution used here is to
595 * read the high byte once, then the low byte, then the high
596 * byte again. If the new high byte matches the old one,
597 * then we have a valid reading. Else we have to read the low
598 * byte again, and now we believe we have a correct reading.
599 */
600 oldh = i2c_smbus_read_byte_data(client,
601 LM90_REG_R_REMOTE_TEMPH);
602 data->temp11[0] = i2c_smbus_read_byte_data(client,
603 LM90_REG_R_REMOTE_TEMPL);
604 newh = i2c_smbus_read_byte_data(client,
605 LM90_REG_R_REMOTE_TEMPH);
606 if (newh != oldh) {
607 data->temp11[0] = i2c_smbus_read_byte_data(client,
608 LM90_REG_R_REMOTE_TEMPL);
609#ifdef DEBUG
610 oldh = i2c_smbus_read_byte_data(client,
611 LM90_REG_R_REMOTE_TEMPH);
612 /* oldh is actually newer */
613 if (newh != oldh)
614 dev_warn(&client->dev, "Remote temperature may be "
615 "wrong.\n");
616#endif
617 }
618 data->temp11[0] |= (newh << 8);
619
620 data->temp11[1] = (i2c_smbus_read_byte_data(client,
621 LM90_REG_R_REMOTE_LOWH) << 8) +
622 i2c_smbus_read_byte_data(client,
623 LM90_REG_R_REMOTE_LOWL);
624 data->temp11[2] = (i2c_smbus_read_byte_data(client,
625 LM90_REG_R_REMOTE_HIGHH) << 8) +
626 i2c_smbus_read_byte_data(client,
627 LM90_REG_R_REMOTE_HIGHL);
628 data->alarms = i2c_smbus_read_byte_data(client,
629 LM90_REG_R_STATUS);
630
631 data->last_updated = jiffies;
632 data->valid = 1;
633 }
634
635 up(&data->update_lock);
636
637 return data;
638}
639
640static int __init sensors_lm90_init(void)
641{
642 return i2c_add_driver(&lm90_driver);
643}
644
645static void __exit sensors_lm90_exit(void)
646{
647 i2c_del_driver(&lm90_driver);
648}
649
650MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
651MODULE_DESCRIPTION("LM90/ADM1032 driver");
652MODULE_LICENSE("GPL");
653
654module_init(sensors_lm90_init);
655module_exit(sensors_lm90_exit);
diff --git a/drivers/hwmon/lm92.c b/drivers/hwmon/lm92.c
new file mode 100644
index 000000000000..215c8e40ffdd
--- /dev/null
+++ b/drivers/hwmon/lm92.c
@@ -0,0 +1,429 @@
1/*
2 * lm92 - Hardware monitoring driver
3 * Copyright (C) 2005 Jean Delvare <khali@linux-fr.org>
4 *
5 * Based on the lm90 driver, with some ideas taken from the lm_sensors
6 * lm92 driver as well.
7 *
8 * The LM92 is a sensor chip made by National Semiconductor. It reports
9 * its own temperature with a 0.0625 deg resolution and a 0.33 deg
10 * accuracy. Complete datasheet can be obtained from National's website
11 * at:
12 * http://www.national.com/pf/LM/LM92.html
13 *
14 * This driver also supports the MAX6635 sensor chip made by Maxim.
15 * This chip is compatible with the LM92, but has a lesser accuracy
16 * (1.0 deg). Complete datasheet can be obtained from Maxim's website
17 * at:
18 * http://www.maxim-ic.com/quick_view2.cfm/qv_pk/3074
19 *
20 * Since the LM92 was the first chipset supported by this driver, most
21 * comments will refer to this chipset, but are actually general and
22 * concern all supported chipsets, unless mentioned otherwise.
23 *
24 * Support could easily be added for the National Semiconductor LM76
25 * and Maxim MAX6633 and MAX6634 chips, which are mostly compatible
26 * with the LM92.
27 *
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation; either version 2 of the License, or
31 * (at your option) any later version.
32 *
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
37 *
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
41 */
42
43#include <linux/module.h>
44#include <linux/init.h>
45#include <linux/slab.h>
46#include <linux/i2c.h>
47#include <linux/i2c-sensor.h>
48
49
50/* The LM92 and MAX6635 have 2 two-state pins for address selection,
51 resulting in 4 possible addresses. */
52static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
53 I2C_CLIENT_END };
54static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
55
56/* Insmod parameters */
57SENSORS_INSMOD_1(lm92);
58
59/* The LM92 registers */
60#define LM92_REG_CONFIG 0x01 /* 8-bit, RW */
61#define LM92_REG_TEMP 0x00 /* 16-bit, RO */
62#define LM92_REG_TEMP_HYST 0x02 /* 16-bit, RW */
63#define LM92_REG_TEMP_CRIT 0x03 /* 16-bit, RW */
64#define LM92_REG_TEMP_LOW 0x04 /* 16-bit, RW */
65#define LM92_REG_TEMP_HIGH 0x05 /* 16-bit, RW */
66#define LM92_REG_MAN_ID 0x07 /* 16-bit, RO, LM92 only */
67
68/* The LM92 uses signed 13-bit values with LSB = 0.0625 degree Celsius,
69 left-justified in 16-bit registers. No rounding is done, with such
70 a resolution it's just not worth it. Note that the MAX6635 doesn't
71 make use of the 4 lower bits for limits (i.e. effective resolution
72 for limits is 1 degree Celsius). */
73static inline int TEMP_FROM_REG(s16 reg)
74{
75 return reg / 8 * 625 / 10;
76}
77
78static inline s16 TEMP_TO_REG(int val)
79{
80 if (val <= -60000)
81 return -60000 * 10 / 625 * 8;
82 if (val >= 160000)
83 return 160000 * 10 / 625 * 8;
84 return val * 10 / 625 * 8;
85}
86
87/* Alarm flags are stored in the 3 LSB of the temperature register */
88static inline u8 ALARMS_FROM_REG(s16 reg)
89{
90 return reg & 0x0007;
91}
92
93/* Driver data (common to all clients) */
94static struct i2c_driver lm92_driver;
95
96/* Client data (each client gets its own) */
97struct lm92_data {
98 struct i2c_client client;
99 struct semaphore update_lock;
100 char valid; /* zero until following fields are valid */
101 unsigned long last_updated; /* in jiffies */
102
103 /* registers values */
104 s16 temp1_input, temp1_crit, temp1_min, temp1_max, temp1_hyst;
105};
106
107
108/*
109 * Sysfs attributes and callback functions
110 */
111
112static struct lm92_data *lm92_update_device(struct device *dev)
113{
114 struct i2c_client *client = to_i2c_client(dev);
115 struct lm92_data *data = i2c_get_clientdata(client);
116
117 down(&data->update_lock);
118
119 if (time_after(jiffies, data->last_updated + HZ)
120 || !data->valid) {
121 dev_dbg(&client->dev, "Updating lm92 data\n");
122 data->temp1_input = swab16(i2c_smbus_read_word_data(client,
123 LM92_REG_TEMP));
124 data->temp1_hyst = swab16(i2c_smbus_read_word_data(client,
125 LM92_REG_TEMP_HYST));
126 data->temp1_crit = swab16(i2c_smbus_read_word_data(client,
127 LM92_REG_TEMP_CRIT));
128 data->temp1_min = swab16(i2c_smbus_read_word_data(client,
129 LM92_REG_TEMP_LOW));
130 data->temp1_max = swab16(i2c_smbus_read_word_data(client,
131 LM92_REG_TEMP_HIGH));
132
133 data->last_updated = jiffies;
134 data->valid = 1;
135 }
136
137 up(&data->update_lock);
138
139 return data;
140}
141
142#define show_temp(value) \
143static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
144{ \
145 struct lm92_data *data = lm92_update_device(dev); \
146 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \
147}
148show_temp(temp1_input);
149show_temp(temp1_crit);
150show_temp(temp1_min);
151show_temp(temp1_max);
152
153#define set_temp(value, reg) \
154static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, \
155 size_t count) \
156{ \
157 struct i2c_client *client = to_i2c_client(dev); \
158 struct lm92_data *data = i2c_get_clientdata(client); \
159 long val = simple_strtol(buf, NULL, 10); \
160 \
161 down(&data->update_lock); \
162 data->value = TEMP_TO_REG(val); \
163 i2c_smbus_write_word_data(client, reg, swab16(data->value)); \
164 up(&data->update_lock); \
165 return count; \
166}
167set_temp(temp1_crit, LM92_REG_TEMP_CRIT);
168set_temp(temp1_min, LM92_REG_TEMP_LOW);
169set_temp(temp1_max, LM92_REG_TEMP_HIGH);
170
171static ssize_t show_temp1_crit_hyst(struct device *dev, struct device_attribute *attr, char *buf)
172{
173 struct lm92_data *data = lm92_update_device(dev);
174 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp1_crit)
175 - TEMP_FROM_REG(data->temp1_hyst));
176}
177static ssize_t show_temp1_max_hyst(struct device *dev, struct device_attribute *attr, char *buf)
178{
179 struct lm92_data *data = lm92_update_device(dev);
180 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp1_max)
181 - TEMP_FROM_REG(data->temp1_hyst));
182}
183static ssize_t show_temp1_min_hyst(struct device *dev, struct device_attribute *attr, char *buf)
184{
185 struct lm92_data *data = lm92_update_device(dev);
186 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp1_min)
187 + TEMP_FROM_REG(data->temp1_hyst));
188}
189
190static ssize_t set_temp1_crit_hyst(struct device *dev, struct device_attribute *attr, const char *buf,
191 size_t count)
192{
193 struct i2c_client *client = to_i2c_client(dev);
194 struct lm92_data *data = i2c_get_clientdata(client);
195 long val = simple_strtol(buf, NULL, 10);
196
197 down(&data->update_lock);
198 data->temp1_hyst = TEMP_FROM_REG(data->temp1_crit) - val;
199 i2c_smbus_write_word_data(client, LM92_REG_TEMP_HYST,
200 swab16(TEMP_TO_REG(data->temp1_hyst)));
201 up(&data->update_lock);
202 return count;
203}
204
205static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
206{
207 struct lm92_data *data = lm92_update_device(dev);
208 return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->temp1_input));
209}
210
211static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp1_input, NULL);
212static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp1_crit,
213 set_temp1_crit);
214static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp1_crit_hyst,
215 set_temp1_crit_hyst);
216static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp1_min,
217 set_temp1_min);
218static DEVICE_ATTR(temp1_min_hyst, S_IRUGO, show_temp1_min_hyst, NULL);
219static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp1_max,
220 set_temp1_max);
221static DEVICE_ATTR(temp1_max_hyst, S_IRUGO, show_temp1_max_hyst, NULL);
222static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
223
224
225/*
226 * Detection and registration
227 */
228
229static void lm92_init_client(struct i2c_client *client)
230{
231 u8 config;
232
233 /* Start the conversions if needed */
234 config = i2c_smbus_read_byte_data(client, LM92_REG_CONFIG);
235 if (config & 0x01)
236 i2c_smbus_write_byte_data(client, LM92_REG_CONFIG,
237 config & 0xFE);
238}
239
240/* The MAX6635 has no identification register, so we have to use tricks
241 to identify it reliably. This is somewhat slow.
242 Note that we do NOT rely on the 2 MSB of the configuration register
243 always reading 0, as suggested by the datasheet, because it was once
244 reported not to be true. */
245static int max6635_check(struct i2c_client *client)
246{
247 u16 temp_low, temp_high, temp_hyst, temp_crit;
248 u8 conf;
249 int i;
250
251 /* No manufacturer ID register, so a read from this address will
252 always return the last read value. */
253 temp_low = i2c_smbus_read_word_data(client, LM92_REG_TEMP_LOW);
254 if (i2c_smbus_read_word_data(client, LM92_REG_MAN_ID) != temp_low)
255 return 0;
256 temp_high = i2c_smbus_read_word_data(client, LM92_REG_TEMP_HIGH);
257 if (i2c_smbus_read_word_data(client, LM92_REG_MAN_ID) != temp_high)
258 return 0;
259
260 /* Limits are stored as integer values (signed, 9-bit). */
261 if ((temp_low & 0x7f00) || (temp_high & 0x7f00))
262 return 0;
263 temp_hyst = i2c_smbus_read_word_data(client, LM92_REG_TEMP_HYST);
264 temp_crit = i2c_smbus_read_word_data(client, LM92_REG_TEMP_CRIT);
265 if ((temp_hyst & 0x7f00) || (temp_crit & 0x7f00))
266 return 0;
267
268 /* Registers addresses were found to cycle over 16-byte boundaries.
269 We don't test all registers with all offsets so as to save some
270 reads and time, but this should still be sufficient to dismiss
271 non-MAX6635 chips. */
272 conf = i2c_smbus_read_byte_data(client, LM92_REG_CONFIG);
273 for (i=16; i<96; i*=2) {
274 if (temp_hyst != i2c_smbus_read_word_data(client,
275 LM92_REG_TEMP_HYST + i - 16)
276 || temp_crit != i2c_smbus_read_word_data(client,
277 LM92_REG_TEMP_CRIT + i)
278 || temp_low != i2c_smbus_read_word_data(client,
279 LM92_REG_TEMP_LOW + i + 16)
280 || temp_high != i2c_smbus_read_word_data(client,
281 LM92_REG_TEMP_HIGH + i + 32)
282 || conf != i2c_smbus_read_byte_data(client,
283 LM92_REG_CONFIG + i))
284 return 0;
285 }
286
287 return 1;
288}
289
290/* The following function does more than just detection. If detection
291 succeeds, it also registers the new chip. */
292static int lm92_detect(struct i2c_adapter *adapter, int address, int kind)
293{
294 struct i2c_client *new_client;
295 struct lm92_data *data;
296 int err = 0;
297 char *name;
298
299 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
300 | I2C_FUNC_SMBUS_WORD_DATA))
301 goto exit;
302
303 if (!(data = kmalloc(sizeof(struct lm92_data), GFP_KERNEL))) {
304 err = -ENOMEM;
305 goto exit;
306 }
307 memset(data, 0, sizeof(struct lm92_data));
308
309 /* Fill in enough client fields so that we can read from the chip,
310 which is required for identication */
311 new_client = &data->client;
312 i2c_set_clientdata(new_client, data);
313 new_client->addr = address;
314 new_client->adapter = adapter;
315 new_client->driver = &lm92_driver;
316 new_client->flags = 0;
317
318 /* A negative kind means that the driver was loaded with no force
319 parameter (default), so we must identify the chip. */
320 if (kind < 0) {
321 u8 config = i2c_smbus_read_byte_data(new_client,
322 LM92_REG_CONFIG);
323 u16 man_id = i2c_smbus_read_word_data(new_client,
324 LM92_REG_MAN_ID);
325
326 if ((config & 0xe0) == 0x00
327 && man_id == 0x0180) {
328 pr_info("lm92: Found National Semiconductor LM92 chip\n");
329 kind = lm92;
330 } else
331 if (max6635_check(new_client)) {
332 pr_info("lm92: Found Maxim MAX6635 chip\n");
333 kind = lm92; /* No separate prefix */
334 }
335 else
336 goto exit_free;
337 } else
338 if (kind == 0) /* Default to an LM92 if forced */
339 kind = lm92;
340
341 /* Give it the proper name */
342 if (kind == lm92) {
343 name = "lm92";
344 } else { /* Supposedly cannot happen */
345 dev_dbg(&new_client->dev, "Kind out of range?\n");
346 goto exit_free;
347 }
348
349 /* Fill in the remaining client fields */
350 strlcpy(new_client->name, name, I2C_NAME_SIZE);
351 data->valid = 0;
352 init_MUTEX(&data->update_lock);
353
354 /* Tell the i2c subsystem a new client has arrived */
355 if ((err = i2c_attach_client(new_client)))
356 goto exit_free;
357
358 /* Initialize the chipset */
359 lm92_init_client(new_client);
360
361 /* Register sysfs hooks */
362 device_create_file(&new_client->dev, &dev_attr_temp1_input);
363 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
364 device_create_file(&new_client->dev, &dev_attr_temp1_crit_hyst);
365 device_create_file(&new_client->dev, &dev_attr_temp1_min);
366 device_create_file(&new_client->dev, &dev_attr_temp1_min_hyst);
367 device_create_file(&new_client->dev, &dev_attr_temp1_max);
368 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
369 device_create_file(&new_client->dev, &dev_attr_alarms);
370
371 return 0;
372
373exit_free:
374 kfree(data);
375exit:
376 return err;
377}
378
379static int lm92_attach_adapter(struct i2c_adapter *adapter)
380{
381 if (!(adapter->class & I2C_CLASS_HWMON))
382 return 0;
383 return i2c_detect(adapter, &addr_data, lm92_detect);
384}
385
386static int lm92_detach_client(struct i2c_client *client)
387{
388 int err;
389
390 if ((err = i2c_detach_client(client))) {
391 dev_err(&client->dev, "Client deregistration failed, "
392 "client not detached.\n");
393 return err;
394 }
395
396 kfree(i2c_get_clientdata(client));
397 return 0;
398}
399
400
401/*
402 * Module and driver stuff
403 */
404
405static struct i2c_driver lm92_driver = {
406 .owner = THIS_MODULE,
407 .name = "lm92",
408 .id = I2C_DRIVERID_LM92,
409 .flags = I2C_DF_NOTIFY,
410 .attach_adapter = lm92_attach_adapter,
411 .detach_client = lm92_detach_client,
412};
413
414static int __init sensors_lm92_init(void)
415{
416 return i2c_add_driver(&lm92_driver);
417}
418
419static void __exit sensors_lm92_exit(void)
420{
421 i2c_del_driver(&lm92_driver);
422}
423
424MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
425MODULE_DESCRIPTION("LM92/MAX6635 driver");
426MODULE_LICENSE("GPL");
427
428module_init(sensors_lm92_init);
429module_exit(sensors_lm92_exit);
diff --git a/drivers/hwmon/max1619.c b/drivers/hwmon/max1619.c
new file mode 100644
index 000000000000..bf553dcd97d6
--- /dev/null
+++ b/drivers/hwmon/max1619.c
@@ -0,0 +1,372 @@
1/*
2 * max1619.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 2003-2004 Alexey Fisher <fishor@mail.ru>
5 * Jean Delvare <khali@linux-fr.org>
6 *
7 * Based on the lm90 driver. The MAX1619 is a sensor chip made by Maxim.
8 * It reports up to two temperatures (its own plus up to
9 * one external one). Complete datasheet can be
10 * obtained from Maxim's website at:
11 * http://pdfserv.maxim-ic.com/en/ds/MAX1619.pdf
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/slab.h>
32#include <linux/jiffies.h>
33#include <linux/i2c.h>
34#include <linux/i2c-sensor.h>
35
36
37static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
38 0x29, 0x2a, 0x2b,
39 0x4c, 0x4d, 0x4e,
40 I2C_CLIENT_END };
41static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
42
43/*
44 * Insmod parameters
45 */
46
47SENSORS_INSMOD_1(max1619);
48
49/*
50 * The MAX1619 registers
51 */
52
53#define MAX1619_REG_R_MAN_ID 0xFE
54#define MAX1619_REG_R_CHIP_ID 0xFF
55#define MAX1619_REG_R_CONFIG 0x03
56#define MAX1619_REG_W_CONFIG 0x09
57#define MAX1619_REG_R_CONVRATE 0x04
58#define MAX1619_REG_W_CONVRATE 0x0A
59#define MAX1619_REG_R_STATUS 0x02
60#define MAX1619_REG_R_LOCAL_TEMP 0x00
61#define MAX1619_REG_R_REMOTE_TEMP 0x01
62#define MAX1619_REG_R_REMOTE_HIGH 0x07
63#define MAX1619_REG_W_REMOTE_HIGH 0x0D
64#define MAX1619_REG_R_REMOTE_LOW 0x08
65#define MAX1619_REG_W_REMOTE_LOW 0x0E
66#define MAX1619_REG_R_REMOTE_CRIT 0x10
67#define MAX1619_REG_W_REMOTE_CRIT 0x12
68#define MAX1619_REG_R_TCRIT_HYST 0x11
69#define MAX1619_REG_W_TCRIT_HYST 0x13
70
71/*
72 * Conversions and various macros
73 */
74
75#define TEMP_FROM_REG(val) ((val & 0x80 ? val-0x100 : val) * 1000)
76#define TEMP_TO_REG(val) ((val < 0 ? val+0x100*1000 : val) / 1000)
77
78/*
79 * Functions declaration
80 */
81
82static int max1619_attach_adapter(struct i2c_adapter *adapter);
83static int max1619_detect(struct i2c_adapter *adapter, int address,
84 int kind);
85static void max1619_init_client(struct i2c_client *client);
86static int max1619_detach_client(struct i2c_client *client);
87static struct max1619_data *max1619_update_device(struct device *dev);
88
89/*
90 * Driver data (common to all clients)
91 */
92
93static struct i2c_driver max1619_driver = {
94 .owner = THIS_MODULE,
95 .name = "max1619",
96 .flags = I2C_DF_NOTIFY,
97 .attach_adapter = max1619_attach_adapter,
98 .detach_client = max1619_detach_client,
99};
100
101/*
102 * Client data (each client gets its own)
103 */
104
105struct max1619_data {
106 struct i2c_client client;
107 struct semaphore update_lock;
108 char valid; /* zero until following fields are valid */
109 unsigned long last_updated; /* in jiffies */
110
111 /* registers values */
112 u8 temp_input1; /* local */
113 u8 temp_input2, temp_low2, temp_high2; /* remote */
114 u8 temp_crit2;
115 u8 temp_hyst2;
116 u8 alarms;
117};
118
119/*
120 * Sysfs stuff
121 */
122
123#define show_temp(value) \
124static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
125{ \
126 struct max1619_data *data = max1619_update_device(dev); \
127 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \
128}
129show_temp(temp_input1);
130show_temp(temp_input2);
131show_temp(temp_low2);
132show_temp(temp_high2);
133show_temp(temp_crit2);
134show_temp(temp_hyst2);
135
136#define set_temp2(value, reg) \
137static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, \
138 size_t count) \
139{ \
140 struct i2c_client *client = to_i2c_client(dev); \
141 struct max1619_data *data = i2c_get_clientdata(client); \
142 long val = simple_strtol(buf, NULL, 10); \
143 \
144 down(&data->update_lock); \
145 data->value = TEMP_TO_REG(val); \
146 i2c_smbus_write_byte_data(client, reg, data->value); \
147 up(&data->update_lock); \
148 return count; \
149}
150
151set_temp2(temp_low2, MAX1619_REG_W_REMOTE_LOW);
152set_temp2(temp_high2, MAX1619_REG_W_REMOTE_HIGH);
153set_temp2(temp_crit2, MAX1619_REG_W_REMOTE_CRIT);
154set_temp2(temp_hyst2, MAX1619_REG_W_TCRIT_HYST);
155
156static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
157{
158 struct max1619_data *data = max1619_update_device(dev);
159 return sprintf(buf, "%d\n", data->alarms);
160}
161
162static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
163static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input2, NULL);
164static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_low2,
165 set_temp_low2);
166static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_high2,
167 set_temp_high2);
168static DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit2,
169 set_temp_crit2);
170static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp_hyst2,
171 set_temp_hyst2);
172static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
173
174/*
175 * Real code
176 */
177
178static int max1619_attach_adapter(struct i2c_adapter *adapter)
179{
180 if (!(adapter->class & I2C_CLASS_HWMON))
181 return 0;
182 return i2c_detect(adapter, &addr_data, max1619_detect);
183}
184
185/*
186 * The following function does more than just detection. If detection
187 * succeeds, it also registers the new chip.
188 */
189static int max1619_detect(struct i2c_adapter *adapter, int address, int kind)
190{
191 struct i2c_client *new_client;
192 struct max1619_data *data;
193 int err = 0;
194 const char *name = "";
195 u8 reg_config=0, reg_convrate=0, reg_status=0;
196 u8 man_id, chip_id;
197 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
198 goto exit;
199
200 if (!(data = kmalloc(sizeof(struct max1619_data), GFP_KERNEL))) {
201 err = -ENOMEM;
202 goto exit;
203 }
204 memset(data, 0, sizeof(struct max1619_data));
205
206 /* The common I2C client data is placed right before the
207 MAX1619-specific data. */
208 new_client = &data->client;
209 i2c_set_clientdata(new_client, data);
210 new_client->addr = address;
211 new_client->adapter = adapter;
212 new_client->driver = &max1619_driver;
213 new_client->flags = 0;
214
215 /*
216 * Now we do the remaining detection. A negative kind means that
217 * the driver was loaded with no force parameter (default), so we
218 * must both detect and identify the chip. A zero kind means that
219 * the driver was loaded with the force parameter, the detection
220 * step shall be skipped. A positive kind means that the driver
221 * was loaded with the force parameter and a given kind of chip is
222 * requested, so both the detection and the identification steps
223 * are skipped.
224 */
225 if (kind < 0) { /* detection */
226 reg_config = i2c_smbus_read_byte_data(new_client,
227 MAX1619_REG_R_CONFIG);
228 reg_convrate = i2c_smbus_read_byte_data(new_client,
229 MAX1619_REG_R_CONVRATE);
230 reg_status = i2c_smbus_read_byte_data(new_client,
231 MAX1619_REG_R_STATUS);
232 if ((reg_config & 0x03) != 0x00
233 || reg_convrate > 0x07 || (reg_status & 0x61 ) !=0x00) {
234 dev_dbg(&adapter->dev,
235 "MAX1619 detection failed at 0x%02x.\n",
236 address);
237 goto exit_free;
238 }
239 }
240
241 if (kind <= 0) { /* identification */
242
243 man_id = i2c_smbus_read_byte_data(new_client,
244 MAX1619_REG_R_MAN_ID);
245 chip_id = i2c_smbus_read_byte_data(new_client,
246 MAX1619_REG_R_CHIP_ID);
247
248 if ((man_id == 0x4D) && (chip_id == 0x04)){
249 kind = max1619;
250 }
251 }
252
253 if (kind <= 0) { /* identification failed */
254 dev_info(&adapter->dev,
255 "Unsupported chip (man_id=0x%02X, "
256 "chip_id=0x%02X).\n", man_id, chip_id);
257 goto exit_free;
258 }
259
260
261 if (kind == max1619){
262 name = "max1619";
263 }
264
265 /* We can fill in the remaining client fields */
266 strlcpy(new_client->name, name, I2C_NAME_SIZE);
267 data->valid = 0;
268 init_MUTEX(&data->update_lock);
269
270 /* Tell the I2C layer a new client has arrived */
271 if ((err = i2c_attach_client(new_client)))
272 goto exit_free;
273
274 /* Initialize the MAX1619 chip */
275 max1619_init_client(new_client);
276
277 /* Register sysfs hooks */
278 device_create_file(&new_client->dev, &dev_attr_temp1_input);
279 device_create_file(&new_client->dev, &dev_attr_temp2_input);
280 device_create_file(&new_client->dev, &dev_attr_temp2_min);
281 device_create_file(&new_client->dev, &dev_attr_temp2_max);
282 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
283 device_create_file(&new_client->dev, &dev_attr_temp2_crit_hyst);
284 device_create_file(&new_client->dev, &dev_attr_alarms);
285
286 return 0;
287
288exit_free:
289 kfree(data);
290exit:
291 return err;
292}
293
294static void max1619_init_client(struct i2c_client *client)
295{
296 u8 config;
297
298 /*
299 * Start the conversions.
300 */
301 i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONVRATE,
302 5); /* 2 Hz */
303 config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
304 if (config & 0x40)
305 i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONFIG,
306 config & 0xBF); /* run */
307}
308
309static int max1619_detach_client(struct i2c_client *client)
310{
311 int err;
312
313 if ((err = i2c_detach_client(client))) {
314 dev_err(&client->dev, "Client deregistration failed, "
315 "client not detached.\n");
316 return err;
317 }
318
319 kfree(i2c_get_clientdata(client));
320 return 0;
321}
322
323static struct max1619_data *max1619_update_device(struct device *dev)
324{
325 struct i2c_client *client = to_i2c_client(dev);
326 struct max1619_data *data = i2c_get_clientdata(client);
327
328 down(&data->update_lock);
329
330 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
331 dev_dbg(&client->dev, "Updating max1619 data.\n");
332 data->temp_input1 = i2c_smbus_read_byte_data(client,
333 MAX1619_REG_R_LOCAL_TEMP);
334 data->temp_input2 = i2c_smbus_read_byte_data(client,
335 MAX1619_REG_R_REMOTE_TEMP);
336 data->temp_high2 = i2c_smbus_read_byte_data(client,
337 MAX1619_REG_R_REMOTE_HIGH);
338 data->temp_low2 = i2c_smbus_read_byte_data(client,
339 MAX1619_REG_R_REMOTE_LOW);
340 data->temp_crit2 = i2c_smbus_read_byte_data(client,
341 MAX1619_REG_R_REMOTE_CRIT);
342 data->temp_hyst2 = i2c_smbus_read_byte_data(client,
343 MAX1619_REG_R_TCRIT_HYST);
344 data->alarms = i2c_smbus_read_byte_data(client,
345 MAX1619_REG_R_STATUS);
346
347 data->last_updated = jiffies;
348 data->valid = 1;
349 }
350
351 up(&data->update_lock);
352
353 return data;
354}
355
356static int __init sensors_max1619_init(void)
357{
358 return i2c_add_driver(&max1619_driver);
359}
360
361static void __exit sensors_max1619_exit(void)
362{
363 i2c_del_driver(&max1619_driver);
364}
365
366MODULE_AUTHOR("Alexey Fisher <fishor@mail.ru> and"
367 "Jean Delvare <khali@linux-fr.org>");
368MODULE_DESCRIPTION("MAX1619 sensor driver");
369MODULE_LICENSE("GPL");
370
371module_init(sensors_max1619_init);
372module_exit(sensors_max1619_exit);
diff --git a/drivers/hwmon/pc87360.c b/drivers/hwmon/pc87360.c
new file mode 100644
index 000000000000..876c68f3af31
--- /dev/null
+++ b/drivers/hwmon/pc87360.c
@@ -0,0 +1,1348 @@
1/*
2 * pc87360.c - Part of lm_sensors, Linux kernel modules
3 * for hardware monitoring
4 * Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
5 *
6 * Copied from smsc47m1.c:
7 * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
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 *
23 * Supports the following chips:
24 *
25 * Chip #vin #fan #pwm #temp devid
26 * PC87360 - 2 2 - 0xE1
27 * PC87363 - 2 2 - 0xE8
28 * PC87364 - 3 3 - 0xE4
29 * PC87365 11 3 3 2 0xE5
30 * PC87366 11 3 3 3-4 0xE9
31 *
32 * This driver assumes that no more than one chip is present, and one of
33 * the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
34 */
35
36#include <linux/module.h>
37#include <linux/init.h>
38#include <linux/slab.h>
39#include <linux/jiffies.h>
40#include <linux/i2c.h>
41#include <linux/i2c-sensor.h>
42#include <linux/i2c-vid.h>
43#include <asm/io.h>
44
45static unsigned short normal_i2c[] = { I2C_CLIENT_END };
46static unsigned int normal_isa[] = { 0, I2C_CLIENT_ISA_END };
47static struct i2c_force_data forces[] = {{ NULL }};
48static u8 devid;
49static unsigned int extra_isa[3];
50static u8 confreg[4];
51
52enum chips { any_chip, pc87360, pc87363, pc87364, pc87365, pc87366 };
53static struct i2c_address_data addr_data = {
54 .normal_i2c = normal_i2c,
55 .normal_isa = normal_isa,
56 .forces = forces,
57};
58
59static int init = 1;
60module_param(init, int, 0);
61MODULE_PARM_DESC(init,
62 "Chip initialization level:\n"
63 " 0: None\n"
64 "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
65 " 2: Forcibly enable all voltage and temperature channels, except in9\n"
66 " 3: Forcibly enable all voltage and temperature channels, including in9");
67
68/*
69 * Super-I/O registers and operations
70 */
71
72#define DEV 0x07 /* Register: Logical device select */
73#define DEVID 0x20 /* Register: Device ID */
74#define ACT 0x30 /* Register: Device activation */
75#define BASE 0x60 /* Register: Base address */
76
77#define FSCM 0x09 /* Logical device: fans */
78#define VLM 0x0d /* Logical device: voltages */
79#define TMS 0x0e /* Logical device: temperatures */
80static const u8 logdev[3] = { FSCM, VLM, TMS };
81
82#define LD_FAN 0
83#define LD_IN 1
84#define LD_TEMP 2
85
86static inline void superio_outb(int sioaddr, int reg, int val)
87{
88 outb(reg, sioaddr);
89 outb(val, sioaddr+1);
90}
91
92static inline int superio_inb(int sioaddr, int reg)
93{
94 outb(reg, sioaddr);
95 return inb(sioaddr+1);
96}
97
98static inline void superio_exit(int sioaddr)
99{
100 outb(0x02, sioaddr);
101 outb(0x02, sioaddr+1);
102}
103
104/*
105 * Logical devices
106 */
107
108#define PC87360_EXTENT 0x10
109#define PC87365_REG_BANK 0x09
110#define NO_BANK 0xff
111
112/*
113 * Fan registers and conversions
114 */
115
116/* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
117#define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
118#define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
119#define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
120#define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
121#define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
122
123#define FAN_FROM_REG(val,div) ((val) == 0 ? 0: \
124 480000 / ((val)*(div)))
125#define FAN_TO_REG(val,div) ((val) <= 100 ? 0 : \
126 480000 / ((val)*(div)))
127#define FAN_DIV_FROM_REG(val) (1 << ((val >> 5) & 0x03))
128#define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
129
130#define FAN_CONFIG_MONITOR(val,nr) (((val) >> (2 + nr * 3)) & 1)
131#define FAN_CONFIG_CONTROL(val,nr) (((val) >> (3 + nr * 3)) & 1)
132#define FAN_CONFIG_INVERT(val,nr) (((val) >> (4 + nr * 3)) & 1)
133
134#define PWM_FROM_REG(val,inv) ((inv) ? 255 - (val) : (val))
135static inline u8 PWM_TO_REG(int val, int inv)
136{
137 if (inv)
138 val = 255 - val;
139 if (val < 0)
140 return 0;
141 if (val > 255)
142 return 255;
143 return val;
144}
145
146/*
147 * Voltage registers and conversions
148 */
149
150#define PC87365_REG_IN_CONVRATE 0x07
151#define PC87365_REG_IN_CONFIG 0x08
152#define PC87365_REG_IN 0x0B
153#define PC87365_REG_IN_MIN 0x0D
154#define PC87365_REG_IN_MAX 0x0C
155#define PC87365_REG_IN_STATUS 0x0A
156#define PC87365_REG_IN_ALARMS1 0x00
157#define PC87365_REG_IN_ALARMS2 0x01
158#define PC87365_REG_VID 0x06
159
160#define IN_FROM_REG(val,ref) (((val) * (ref) + 128) / 256)
161#define IN_TO_REG(val,ref) ((val) < 0 ? 0 : \
162 (val)*256 >= (ref)*255 ? 255: \
163 ((val) * 256 + (ref)/2) / (ref))
164
165/*
166 * Temperature registers and conversions
167 */
168
169#define PC87365_REG_TEMP_CONFIG 0x08
170#define PC87365_REG_TEMP 0x0B
171#define PC87365_REG_TEMP_MIN 0x0D
172#define PC87365_REG_TEMP_MAX 0x0C
173#define PC87365_REG_TEMP_CRIT 0x0E
174#define PC87365_REG_TEMP_STATUS 0x0A
175#define PC87365_REG_TEMP_ALARMS 0x00
176
177#define TEMP_FROM_REG(val) ((val) * 1000)
178#define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
179 (val) > 127000 ? 127 : \
180 (val) < 0 ? ((val) - 500) / 1000 : \
181 ((val) + 500) / 1000)
182
183/*
184 * Client data (each client gets its own)
185 */
186
187struct pc87360_data {
188 struct i2c_client client;
189 struct semaphore lock;
190 struct semaphore update_lock;
191 char valid; /* !=0 if following fields are valid */
192 unsigned long last_updated; /* In jiffies */
193
194 int address[3];
195
196 u8 fannr, innr, tempnr;
197
198 u8 fan[3]; /* Register value */
199 u8 fan_min[3]; /* Register value */
200 u8 fan_status[3]; /* Register value */
201 u8 pwm[3]; /* Register value */
202 u16 fan_conf; /* Configuration register values, combined */
203
204 u16 in_vref; /* 1 mV/bit */
205 u8 in[14]; /* Register value */
206 u8 in_min[14]; /* Register value */
207 u8 in_max[14]; /* Register value */
208 u8 in_crit[3]; /* Register value */
209 u8 in_status[14]; /* Register value */
210 u16 in_alarms; /* Register values, combined, masked */
211 u8 vid_conf; /* Configuration register value */
212 u8 vrm;
213 u8 vid; /* Register value */
214
215 s8 temp[3]; /* Register value */
216 s8 temp_min[3]; /* Register value */
217 s8 temp_max[3]; /* Register value */
218 s8 temp_crit[3]; /* Register value */
219 u8 temp_status[3]; /* Register value */
220 u8 temp_alarms; /* Register value, masked */
221};
222
223/*
224 * Functions declaration
225 */
226
227static int pc87360_attach_adapter(struct i2c_adapter *adapter);
228static int pc87360_detect(struct i2c_adapter *adapter, int address, int kind);
229static int pc87360_detach_client(struct i2c_client *client);
230
231static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
232 u8 reg);
233static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
234 u8 reg, u8 value);
235static void pc87360_init_client(struct i2c_client *client, int use_thermistors);
236static struct pc87360_data *pc87360_update_device(struct device *dev);
237
238/*
239 * Driver data (common to all clients)
240 */
241
242static struct i2c_driver pc87360_driver = {
243 .owner = THIS_MODULE,
244 .name = "pc87360",
245 .flags = I2C_DF_NOTIFY,
246 .attach_adapter = pc87360_attach_adapter,
247 .detach_client = pc87360_detach_client,
248};
249
250/*
251 * Sysfs stuff
252 */
253
254static ssize_t set_fan_min(struct device *dev, const char *buf,
255 size_t count, int nr)
256{
257 struct i2c_client *client = to_i2c_client(dev);
258 struct pc87360_data *data = i2c_get_clientdata(client);
259 long fan_min = simple_strtol(buf, NULL, 10);
260
261 down(&data->update_lock);
262 fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[nr]));
263
264 /* If it wouldn't fit, change clock divisor */
265 while (fan_min > 255
266 && (data->fan_status[nr] & 0x60) != 0x60) {
267 fan_min >>= 1;
268 data->fan[nr] >>= 1;
269 data->fan_status[nr] += 0x20;
270 }
271 data->fan_min[nr] = fan_min > 255 ? 255 : fan_min;
272 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(nr),
273 data->fan_min[nr]);
274
275 /* Write new divider, preserve alarm bits */
276 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(nr),
277 data->fan_status[nr] & 0xF9);
278 up(&data->update_lock);
279
280 return count;
281}
282
283#define show_and_set_fan(offset) \
284static ssize_t show_fan##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
285{ \
286 struct pc87360_data *data = pc87360_update_device(dev); \
287 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[offset-1], \
288 FAN_DIV_FROM_REG(data->fan_status[offset-1]))); \
289} \
290static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
291{ \
292 struct pc87360_data *data = pc87360_update_device(dev); \
293 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[offset-1], \
294 FAN_DIV_FROM_REG(data->fan_status[offset-1]))); \
295} \
296static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
297{ \
298 struct pc87360_data *data = pc87360_update_device(dev); \
299 return sprintf(buf, "%u\n", \
300 FAN_DIV_FROM_REG(data->fan_status[offset-1])); \
301} \
302static ssize_t show_fan##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
303{ \
304 struct pc87360_data *data = pc87360_update_device(dev); \
305 return sprintf(buf, "%u\n", \
306 FAN_STATUS_FROM_REG(data->fan_status[offset-1])); \
307} \
308static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
309 size_t count) \
310{ \
311 return set_fan_min(dev, buf, count, offset-1); \
312} \
313static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
314 show_fan##offset##_input, NULL); \
315static DEVICE_ATTR(fan##offset##_min, S_IWUSR | S_IRUGO, \
316 show_fan##offset##_min, set_fan##offset##_min); \
317static DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
318 show_fan##offset##_div, NULL); \
319static DEVICE_ATTR(fan##offset##_status, S_IRUGO, \
320 show_fan##offset##_status, NULL);
321show_and_set_fan(1)
322show_and_set_fan(2)
323show_and_set_fan(3)
324
325#define show_and_set_pwm(offset) \
326static ssize_t show_pwm##offset(struct device *dev, struct device_attribute *attr, char *buf) \
327{ \
328 struct pc87360_data *data = pc87360_update_device(dev); \
329 return sprintf(buf, "%u\n", \
330 PWM_FROM_REG(data->pwm[offset-1], \
331 FAN_CONFIG_INVERT(data->fan_conf, \
332 offset-1))); \
333} \
334static ssize_t set_pwm##offset(struct device *dev, struct device_attribute *attr, const char *buf, \
335 size_t count) \
336{ \
337 struct i2c_client *client = to_i2c_client(dev); \
338 struct pc87360_data *data = i2c_get_clientdata(client); \
339 long val = simple_strtol(buf, NULL, 10); \
340 \
341 down(&data->update_lock); \
342 data->pwm[offset-1] = PWM_TO_REG(val, \
343 FAN_CONFIG_INVERT(data->fan_conf, offset-1)); \
344 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(offset-1), \
345 data->pwm[offset-1]); \
346 up(&data->update_lock); \
347 return count; \
348} \
349static DEVICE_ATTR(pwm##offset, S_IWUSR | S_IRUGO, \
350 show_pwm##offset, set_pwm##offset);
351show_and_set_pwm(1)
352show_and_set_pwm(2)
353show_and_set_pwm(3)
354
355#define show_and_set_in(offset) \
356static ssize_t show_in##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
357{ \
358 struct pc87360_data *data = pc87360_update_device(dev); \
359 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
360 data->in_vref)); \
361} \
362static ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
363{ \
364 struct pc87360_data *data = pc87360_update_device(dev); \
365 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
366 data->in_vref)); \
367} \
368static ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
369{ \
370 struct pc87360_data *data = pc87360_update_device(dev); \
371 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
372 data->in_vref)); \
373} \
374static ssize_t show_in##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
375{ \
376 struct pc87360_data *data = pc87360_update_device(dev); \
377 return sprintf(buf, "%u\n", data->in_status[offset]); \
378} \
379static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
380 size_t count) \
381{ \
382 struct i2c_client *client = to_i2c_client(dev); \
383 struct pc87360_data *data = i2c_get_clientdata(client); \
384 long val = simple_strtol(buf, NULL, 10); \
385 \
386 down(&data->update_lock); \
387 data->in_min[offset] = IN_TO_REG(val, data->in_vref); \
388 pc87360_write_value(data, LD_IN, offset, PC87365_REG_IN_MIN, \
389 data->in_min[offset]); \
390 up(&data->update_lock); \
391 return count; \
392} \
393static ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
394 size_t count) \
395{ \
396 struct i2c_client *client = to_i2c_client(dev); \
397 struct pc87360_data *data = i2c_get_clientdata(client); \
398 long val = simple_strtol(buf, NULL, 10); \
399 \
400 down(&data->update_lock); \
401 data->in_max[offset] = IN_TO_REG(val, \
402 data->in_vref); \
403 pc87360_write_value(data, LD_IN, offset, PC87365_REG_IN_MAX, \
404 data->in_max[offset]); \
405 up(&data->update_lock); \
406 return count; \
407} \
408static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
409 show_in##offset##_input, NULL); \
410static DEVICE_ATTR(in##offset##_min, S_IWUSR | S_IRUGO, \
411 show_in##offset##_min, set_in##offset##_min); \
412static DEVICE_ATTR(in##offset##_max, S_IWUSR | S_IRUGO, \
413 show_in##offset##_max, set_in##offset##_max); \
414static DEVICE_ATTR(in##offset##_status, S_IRUGO, \
415 show_in##offset##_status, NULL);
416show_and_set_in(0)
417show_and_set_in(1)
418show_and_set_in(2)
419show_and_set_in(3)
420show_and_set_in(4)
421show_and_set_in(5)
422show_and_set_in(6)
423show_and_set_in(7)
424show_and_set_in(8)
425show_and_set_in(9)
426show_and_set_in(10)
427
428#define show_and_set_therm(offset) \
429static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
430{ \
431 struct pc87360_data *data = pc87360_update_device(dev); \
432 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset+7], \
433 data->in_vref)); \
434} \
435static ssize_t show_temp##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
436{ \
437 struct pc87360_data *data = pc87360_update_device(dev); \
438 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset+7], \
439 data->in_vref)); \
440} \
441static ssize_t show_temp##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
442{ \
443 struct pc87360_data *data = pc87360_update_device(dev); \
444 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset+7], \
445 data->in_vref)); \
446} \
447static ssize_t show_temp##offset##_crit(struct device *dev, struct device_attribute *attr, char *buf) \
448{ \
449 struct pc87360_data *data = pc87360_update_device(dev); \
450 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[offset-4], \
451 data->in_vref)); \
452} \
453static ssize_t show_temp##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
454{ \
455 struct pc87360_data *data = pc87360_update_device(dev); \
456 return sprintf(buf, "%u\n", data->in_status[offset+7]); \
457} \
458static ssize_t set_temp##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
459 size_t count) \
460{ \
461 struct i2c_client *client = to_i2c_client(dev); \
462 struct pc87360_data *data = i2c_get_clientdata(client); \
463 long val = simple_strtol(buf, NULL, 10); \
464 \
465 down(&data->update_lock); \
466 data->in_min[offset+7] = IN_TO_REG(val, data->in_vref); \
467 pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_MIN, \
468 data->in_min[offset+7]); \
469 up(&data->update_lock); \
470 return count; \
471} \
472static ssize_t set_temp##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
473 size_t count) \
474{ \
475 struct i2c_client *client = to_i2c_client(dev); \
476 struct pc87360_data *data = i2c_get_clientdata(client); \
477 long val = simple_strtol(buf, NULL, 10); \
478 \
479 down(&data->update_lock); \
480 data->in_max[offset+7] = IN_TO_REG(val, data->in_vref); \
481 pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_MAX, \
482 data->in_max[offset+7]); \
483 up(&data->update_lock); \
484 return count; \
485} \
486static ssize_t set_temp##offset##_crit(struct device *dev, struct device_attribute *attr, const char *buf, \
487 size_t count) \
488{ \
489 struct i2c_client *client = to_i2c_client(dev); \
490 struct pc87360_data *data = i2c_get_clientdata(client); \
491 long val = simple_strtol(buf, NULL, 10); \
492 \
493 down(&data->update_lock); \
494 data->in_crit[offset-4] = IN_TO_REG(val, data->in_vref); \
495 pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_CRIT, \
496 data->in_crit[offset-4]); \
497 up(&data->update_lock); \
498 return count; \
499} \
500static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
501 show_temp##offset##_input, NULL); \
502static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
503 show_temp##offset##_min, set_temp##offset##_min); \
504static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
505 show_temp##offset##_max, set_temp##offset##_max); \
506static DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
507 show_temp##offset##_crit, set_temp##offset##_crit); \
508static DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
509 show_temp##offset##_status, NULL);
510show_and_set_therm(4)
511show_and_set_therm(5)
512show_and_set_therm(6)
513
514static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
515{
516 struct pc87360_data *data = pc87360_update_device(dev);
517 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
518}
519static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
520
521static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
522{
523 struct pc87360_data *data = pc87360_update_device(dev);
524 return sprintf(buf, "%u\n", data->vrm);
525}
526static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
527{
528 struct i2c_client *client = to_i2c_client(dev);
529 struct pc87360_data *data = i2c_get_clientdata(client);
530 data->vrm = simple_strtoul(buf, NULL, 10);
531 return count;
532}
533static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
534
535static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
536{
537 struct pc87360_data *data = pc87360_update_device(dev);
538 return sprintf(buf, "%u\n", data->in_alarms);
539}
540static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
541
542#define show_and_set_temp(offset) \
543static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
544{ \
545 struct pc87360_data *data = pc87360_update_device(dev); \
546 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
547} \
548static ssize_t show_temp##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
549{ \
550 struct pc87360_data *data = pc87360_update_device(dev); \
551 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[offset-1])); \
552} \
553static ssize_t show_temp##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
554{ \
555 struct pc87360_data *data = pc87360_update_device(dev); \
556 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[offset-1])); \
557}\
558static ssize_t show_temp##offset##_crit(struct device *dev, struct device_attribute *attr, char *buf) \
559{ \
560 struct pc87360_data *data = pc87360_update_device(dev); \
561 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[offset-1])); \
562}\
563static ssize_t show_temp##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
564{ \
565 struct pc87360_data *data = pc87360_update_device(dev); \
566 return sprintf(buf, "%d\n", data->temp_status[offset-1]); \
567}\
568static ssize_t set_temp##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
569 size_t count) \
570{ \
571 struct i2c_client *client = to_i2c_client(dev); \
572 struct pc87360_data *data = i2c_get_clientdata(client); \
573 long val = simple_strtol(buf, NULL, 10); \
574 \
575 down(&data->update_lock); \
576 data->temp_min[offset-1] = TEMP_TO_REG(val); \
577 pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_MIN, \
578 data->temp_min[offset-1]); \
579 up(&data->update_lock); \
580 return count; \
581} \
582static ssize_t set_temp##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
583 size_t count) \
584{ \
585 struct i2c_client *client = to_i2c_client(dev); \
586 struct pc87360_data *data = i2c_get_clientdata(client); \
587 long val = simple_strtol(buf, NULL, 10); \
588 \
589 down(&data->update_lock); \
590 data->temp_max[offset-1] = TEMP_TO_REG(val); \
591 pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_MAX, \
592 data->temp_max[offset-1]); \
593 up(&data->update_lock); \
594 return count; \
595} \
596static ssize_t set_temp##offset##_crit(struct device *dev, struct device_attribute *attr, const char *buf, \
597 size_t count) \
598{ \
599 struct i2c_client *client = to_i2c_client(dev); \
600 struct pc87360_data *data = i2c_get_clientdata(client); \
601 long val = simple_strtol(buf, NULL, 10); \
602 \
603 down(&data->update_lock); \
604 data->temp_crit[offset-1] = TEMP_TO_REG(val); \
605 pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_CRIT, \
606 data->temp_crit[offset-1]); \
607 up(&data->update_lock); \
608 return count; \
609} \
610static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
611 show_temp##offset##_input, NULL); \
612static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
613 show_temp##offset##_min, set_temp##offset##_min); \
614static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
615 show_temp##offset##_max, set_temp##offset##_max); \
616static DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
617 show_temp##offset##_crit, set_temp##offset##_crit); \
618static DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
619 show_temp##offset##_status, NULL);
620show_and_set_temp(1)
621show_and_set_temp(2)
622show_and_set_temp(3)
623
624static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
625{
626 struct pc87360_data *data = pc87360_update_device(dev);
627 return sprintf(buf, "%u\n", data->temp_alarms);
628}
629static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
630
631/*
632 * Device detection, registration and update
633 */
634
635static int pc87360_attach_adapter(struct i2c_adapter *adapter)
636{
637 return i2c_detect(adapter, &addr_data, pc87360_detect);
638}
639
640static int pc87360_find(int sioaddr, u8 *devid, int *address)
641{
642 u16 val;
643 int i;
644 int nrdev; /* logical device count */
645
646 /* No superio_enter */
647
648 /* Identify device */
649 val = superio_inb(sioaddr, DEVID);
650 switch (val) {
651 case 0xE1: /* PC87360 */
652 case 0xE8: /* PC87363 */
653 case 0xE4: /* PC87364 */
654 nrdev = 1;
655 break;
656 case 0xE5: /* PC87365 */
657 case 0xE9: /* PC87366 */
658 nrdev = 3;
659 break;
660 default:
661 superio_exit(sioaddr);
662 return -ENODEV;
663 }
664 /* Remember the device id */
665 *devid = val;
666
667 for (i = 0; i < nrdev; i++) {
668 /* select logical device */
669 superio_outb(sioaddr, DEV, logdev[i]);
670
671 val = superio_inb(sioaddr, ACT);
672 if (!(val & 0x01)) {
673 printk(KERN_INFO "pc87360: Device 0x%02x not "
674 "activated\n", logdev[i]);
675 continue;
676 }
677
678 val = (superio_inb(sioaddr, BASE) << 8)
679 | superio_inb(sioaddr, BASE + 1);
680 if (!val) {
681 printk(KERN_INFO "pc87360: Base address not set for "
682 "device 0x%02x\n", logdev[i]);
683 continue;
684 }
685
686 address[i] = val;
687
688 if (i==0) { /* Fans */
689 confreg[0] = superio_inb(sioaddr, 0xF0);
690 confreg[1] = superio_inb(sioaddr, 0xF1);
691
692#ifdef DEBUG
693 printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
694 "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
695 (confreg[0]>>3)&1, (confreg[0]>>4)&1);
696 printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
697 "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
698 (confreg[0]>>6)&1, (confreg[0]>>7)&1);
699 printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
700 "ctrl=%d inv=%d\n", confreg[1]&1,
701 (confreg[1]>>1)&1, (confreg[1]>>2)&1);
702#endif
703 } else if (i==1) { /* Voltages */
704 /* Are we using thermistors? */
705 if (*devid == 0xE9) { /* PC87366 */
706 /* These registers are not logical-device
707 specific, just that we won't need them if
708 we don't use the VLM device */
709 confreg[2] = superio_inb(sioaddr, 0x2B);
710 confreg[3] = superio_inb(sioaddr, 0x25);
711
712 if (confreg[2] & 0x40) {
713 printk(KERN_INFO "pc87360: Using "
714 "thermistors for temperature "
715 "monitoring\n");
716 }
717 if (confreg[3] & 0xE0) {
718 printk(KERN_INFO "pc87360: VID "
719 "inputs routed (mode %u)\n",
720 confreg[3] >> 5);
721 }
722 }
723 }
724 }
725
726 superio_exit(sioaddr);
727 return 0;
728}
729
730/* We don't really care about the address.
731 Read from extra_isa instead. */
732int pc87360_detect(struct i2c_adapter *adapter, int address, int kind)
733{
734 int i;
735 struct i2c_client *new_client;
736 struct pc87360_data *data;
737 int err = 0;
738 const char *name = "pc87360";
739 int use_thermistors = 0;
740
741 if (!i2c_is_isa_adapter(adapter))
742 return -ENODEV;
743
744 if (!(data = kmalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
745 return -ENOMEM;
746 memset(data, 0x00, sizeof(struct pc87360_data));
747
748 new_client = &data->client;
749 i2c_set_clientdata(new_client, data);
750 new_client->addr = address;
751 init_MUTEX(&data->lock);
752 new_client->adapter = adapter;
753 new_client->driver = &pc87360_driver;
754 new_client->flags = 0;
755
756 data->fannr = 2;
757 data->innr = 0;
758 data->tempnr = 0;
759
760 switch (devid) {
761 case 0xe8:
762 name = "pc87363";
763 break;
764 case 0xe4:
765 name = "pc87364";
766 data->fannr = 3;
767 break;
768 case 0xe5:
769 name = "pc87365";
770 data->fannr = extra_isa[0] ? 3 : 0;
771 data->innr = extra_isa[1] ? 11 : 0;
772 data->tempnr = extra_isa[2] ? 2 : 0;
773 break;
774 case 0xe9:
775 name = "pc87366";
776 data->fannr = extra_isa[0] ? 3 : 0;
777 data->innr = extra_isa[1] ? 14 : 0;
778 data->tempnr = extra_isa[2] ? 3 : 0;
779 break;
780 }
781
782 strcpy(new_client->name, name);
783 data->valid = 0;
784 init_MUTEX(&data->update_lock);
785
786 for (i = 0; i < 3; i++) {
787 if (((data->address[i] = extra_isa[i]))
788 && !request_region(extra_isa[i], PC87360_EXTENT,
789 pc87360_driver.name)) {
790 dev_err(&new_client->dev, "Region 0x%x-0x%x already "
791 "in use!\n", extra_isa[i],
792 extra_isa[i]+PC87360_EXTENT-1);
793 for (i--; i >= 0; i--)
794 release_region(extra_isa[i], PC87360_EXTENT);
795 err = -EBUSY;
796 goto ERROR1;
797 }
798 }
799
800 /* Retrieve the fans configuration from Super-I/O space */
801 if (data->fannr)
802 data->fan_conf = confreg[0] | (confreg[1] << 8);
803
804 if ((err = i2c_attach_client(new_client)))
805 goto ERROR2;
806
807 /* Use the correct reference voltage
808 Unless both the VLM and the TMS logical devices agree to
809 use an external Vref, the internal one is used. */
810 if (data->innr) {
811 i = pc87360_read_value(data, LD_IN, NO_BANK,
812 PC87365_REG_IN_CONFIG);
813 if (data->tempnr) {
814 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
815 PC87365_REG_TEMP_CONFIG);
816 }
817 data->in_vref = (i&0x02) ? 3025 : 2966;
818 dev_dbg(&new_client->dev, "Using %s reference voltage\n",
819 (i&0x02) ? "external" : "internal");
820
821 data->vid_conf = confreg[3];
822 data->vrm = 90;
823 }
824
825 /* Fan clock dividers may be needed before any data is read */
826 for (i = 0; i < data->fannr; i++) {
827 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
828 data->fan_status[i] = pc87360_read_value(data,
829 LD_FAN, NO_BANK,
830 PC87360_REG_FAN_STATUS(i));
831 }
832
833 if (init > 0) {
834 if (devid == 0xe9 && data->address[1]) /* PC87366 */
835 use_thermistors = confreg[2] & 0x40;
836
837 pc87360_init_client(new_client, use_thermistors);
838 }
839
840 /* Register sysfs hooks */
841 if (data->innr) {
842 device_create_file(&new_client->dev, &dev_attr_in0_input);
843 device_create_file(&new_client->dev, &dev_attr_in1_input);
844 device_create_file(&new_client->dev, &dev_attr_in2_input);
845 device_create_file(&new_client->dev, &dev_attr_in3_input);
846 device_create_file(&new_client->dev, &dev_attr_in4_input);
847 device_create_file(&new_client->dev, &dev_attr_in5_input);
848 device_create_file(&new_client->dev, &dev_attr_in6_input);
849 device_create_file(&new_client->dev, &dev_attr_in7_input);
850 device_create_file(&new_client->dev, &dev_attr_in8_input);
851 device_create_file(&new_client->dev, &dev_attr_in9_input);
852 device_create_file(&new_client->dev, &dev_attr_in10_input);
853 device_create_file(&new_client->dev, &dev_attr_in0_min);
854 device_create_file(&new_client->dev, &dev_attr_in1_min);
855 device_create_file(&new_client->dev, &dev_attr_in2_min);
856 device_create_file(&new_client->dev, &dev_attr_in3_min);
857 device_create_file(&new_client->dev, &dev_attr_in4_min);
858 device_create_file(&new_client->dev, &dev_attr_in5_min);
859 device_create_file(&new_client->dev, &dev_attr_in6_min);
860 device_create_file(&new_client->dev, &dev_attr_in7_min);
861 device_create_file(&new_client->dev, &dev_attr_in8_min);
862 device_create_file(&new_client->dev, &dev_attr_in9_min);
863 device_create_file(&new_client->dev, &dev_attr_in10_min);
864 device_create_file(&new_client->dev, &dev_attr_in0_max);
865 device_create_file(&new_client->dev, &dev_attr_in1_max);
866 device_create_file(&new_client->dev, &dev_attr_in2_max);
867 device_create_file(&new_client->dev, &dev_attr_in3_max);
868 device_create_file(&new_client->dev, &dev_attr_in4_max);
869 device_create_file(&new_client->dev, &dev_attr_in5_max);
870 device_create_file(&new_client->dev, &dev_attr_in6_max);
871 device_create_file(&new_client->dev, &dev_attr_in7_max);
872 device_create_file(&new_client->dev, &dev_attr_in8_max);
873 device_create_file(&new_client->dev, &dev_attr_in9_max);
874 device_create_file(&new_client->dev, &dev_attr_in10_max);
875 device_create_file(&new_client->dev, &dev_attr_in0_status);
876 device_create_file(&new_client->dev, &dev_attr_in1_status);
877 device_create_file(&new_client->dev, &dev_attr_in2_status);
878 device_create_file(&new_client->dev, &dev_attr_in3_status);
879 device_create_file(&new_client->dev, &dev_attr_in4_status);
880 device_create_file(&new_client->dev, &dev_attr_in5_status);
881 device_create_file(&new_client->dev, &dev_attr_in6_status);
882 device_create_file(&new_client->dev, &dev_attr_in7_status);
883 device_create_file(&new_client->dev, &dev_attr_in8_status);
884 device_create_file(&new_client->dev, &dev_attr_in9_status);
885 device_create_file(&new_client->dev, &dev_attr_in10_status);
886
887 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
888 device_create_file(&new_client->dev, &dev_attr_vrm);
889 device_create_file(&new_client->dev, &dev_attr_alarms_in);
890 }
891
892 if (data->tempnr) {
893 device_create_file(&new_client->dev, &dev_attr_temp1_input);
894 device_create_file(&new_client->dev, &dev_attr_temp2_input);
895 device_create_file(&new_client->dev, &dev_attr_temp1_min);
896 device_create_file(&new_client->dev, &dev_attr_temp2_min);
897 device_create_file(&new_client->dev, &dev_attr_temp1_max);
898 device_create_file(&new_client->dev, &dev_attr_temp2_max);
899 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
900 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
901 device_create_file(&new_client->dev, &dev_attr_temp1_status);
902 device_create_file(&new_client->dev, &dev_attr_temp2_status);
903
904 device_create_file(&new_client->dev, &dev_attr_alarms_temp);
905 }
906 if (data->tempnr == 3) {
907 device_create_file(&new_client->dev, &dev_attr_temp3_input);
908 device_create_file(&new_client->dev, &dev_attr_temp3_min);
909 device_create_file(&new_client->dev, &dev_attr_temp3_max);
910 device_create_file(&new_client->dev, &dev_attr_temp3_crit);
911 device_create_file(&new_client->dev, &dev_attr_temp3_status);
912 }
913 if (data->innr == 14) {
914 device_create_file(&new_client->dev, &dev_attr_temp4_input);
915 device_create_file(&new_client->dev, &dev_attr_temp5_input);
916 device_create_file(&new_client->dev, &dev_attr_temp6_input);
917 device_create_file(&new_client->dev, &dev_attr_temp4_min);
918 device_create_file(&new_client->dev, &dev_attr_temp5_min);
919 device_create_file(&new_client->dev, &dev_attr_temp6_min);
920 device_create_file(&new_client->dev, &dev_attr_temp4_max);
921 device_create_file(&new_client->dev, &dev_attr_temp5_max);
922 device_create_file(&new_client->dev, &dev_attr_temp6_max);
923 device_create_file(&new_client->dev, &dev_attr_temp4_crit);
924 device_create_file(&new_client->dev, &dev_attr_temp5_crit);
925 device_create_file(&new_client->dev, &dev_attr_temp6_crit);
926 device_create_file(&new_client->dev, &dev_attr_temp4_status);
927 device_create_file(&new_client->dev, &dev_attr_temp5_status);
928 device_create_file(&new_client->dev, &dev_attr_temp6_status);
929 }
930
931 if (data->fannr) {
932 if (FAN_CONFIG_MONITOR(data->fan_conf, 0)) {
933 device_create_file(&new_client->dev,
934 &dev_attr_fan1_input);
935 device_create_file(&new_client->dev,
936 &dev_attr_fan1_min);
937 device_create_file(&new_client->dev,
938 &dev_attr_fan1_div);
939 device_create_file(&new_client->dev,
940 &dev_attr_fan1_status);
941 }
942
943 if (FAN_CONFIG_MONITOR(data->fan_conf, 1)) {
944 device_create_file(&new_client->dev,
945 &dev_attr_fan2_input);
946 device_create_file(&new_client->dev,
947 &dev_attr_fan2_min);
948 device_create_file(&new_client->dev,
949 &dev_attr_fan2_div);
950 device_create_file(&new_client->dev,
951 &dev_attr_fan2_status);
952 }
953
954 if (FAN_CONFIG_CONTROL(data->fan_conf, 0))
955 device_create_file(&new_client->dev, &dev_attr_pwm1);
956 if (FAN_CONFIG_CONTROL(data->fan_conf, 1))
957 device_create_file(&new_client->dev, &dev_attr_pwm2);
958 }
959 if (data->fannr == 3) {
960 if (FAN_CONFIG_MONITOR(data->fan_conf, 2)) {
961 device_create_file(&new_client->dev,
962 &dev_attr_fan3_input);
963 device_create_file(&new_client->dev,
964 &dev_attr_fan3_min);
965 device_create_file(&new_client->dev,
966 &dev_attr_fan3_div);
967 device_create_file(&new_client->dev,
968 &dev_attr_fan3_status);
969 }
970
971 if (FAN_CONFIG_CONTROL(data->fan_conf, 2))
972 device_create_file(&new_client->dev, &dev_attr_pwm3);
973 }
974
975 return 0;
976
977ERROR2:
978 for (i = 0; i < 3; i++) {
979 if (data->address[i]) {
980 release_region(data->address[i], PC87360_EXTENT);
981 }
982 }
983ERROR1:
984 kfree(data);
985 return err;
986}
987
988static int pc87360_detach_client(struct i2c_client *client)
989{
990 struct pc87360_data *data = i2c_get_clientdata(client);
991 int i;
992
993 if ((i = i2c_detach_client(client))) {
994 dev_err(&client->dev, "Client deregistration failed, "
995 "client not detached.\n");
996 return i;
997 }
998
999 for (i = 0; i < 3; i++) {
1000 if (data->address[i]) {
1001 release_region(data->address[i], PC87360_EXTENT);
1002 }
1003 }
1004 kfree(data);
1005
1006 return 0;
1007}
1008
1009/* ldi is the logical device index
1010 bank is for voltages and temperatures only */
1011static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1012 u8 reg)
1013{
1014 int res;
1015
1016 down(&(data->lock));
1017 if (bank != NO_BANK)
1018 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1019 res = inb_p(data->address[ldi] + reg);
1020 up(&(data->lock));
1021
1022 return res;
1023}
1024
1025static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1026 u8 reg, u8 value)
1027{
1028 down(&(data->lock));
1029 if (bank != NO_BANK)
1030 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1031 outb_p(value, data->address[ldi] + reg);
1032 up(&(data->lock));
1033}
1034
1035static void pc87360_init_client(struct i2c_client *client, int use_thermistors)
1036{
1037 struct pc87360_data *data = i2c_get_clientdata(client);
1038 int i, nr;
1039 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1040 const u8 init_temp[3] = { 2, 2, 1 };
1041 u8 reg;
1042
1043 if (init >= 2 && data->innr) {
1044 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1045 PC87365_REG_IN_CONVRATE);
1046 dev_info(&client->dev, "VLM conversion set to"
1047 "1s period, 160us delay\n");
1048 pc87360_write_value(data, LD_IN, NO_BANK,
1049 PC87365_REG_IN_CONVRATE,
1050 (reg & 0xC0) | 0x11);
1051 }
1052
1053 nr = data->innr < 11 ? data->innr : 11;
1054 for (i=0; i<nr; i++) {
1055 if (init >= init_in[i]) {
1056 /* Forcibly enable voltage channel */
1057 reg = pc87360_read_value(data, LD_IN, i,
1058 PC87365_REG_IN_STATUS);
1059 if (!(reg & 0x01)) {
1060 dev_dbg(&client->dev, "Forcibly "
1061 "enabling in%d\n", i);
1062 pc87360_write_value(data, LD_IN, i,
1063 PC87365_REG_IN_STATUS,
1064 (reg & 0x68) | 0x87);
1065 }
1066 }
1067 }
1068
1069 /* We can't blindly trust the Super-I/O space configuration bit,
1070 most BIOS won't set it properly */
1071 for (i=11; i<data->innr; i++) {
1072 reg = pc87360_read_value(data, LD_IN, i,
1073 PC87365_REG_TEMP_STATUS);
1074 use_thermistors = use_thermistors || (reg & 0x01);
1075 }
1076
1077 i = use_thermistors ? 2 : 0;
1078 for (; i<data->tempnr; i++) {
1079 if (init >= init_temp[i]) {
1080 /* Forcibly enable temperature channel */
1081 reg = pc87360_read_value(data, LD_TEMP, i,
1082 PC87365_REG_TEMP_STATUS);
1083 if (!(reg & 0x01)) {
1084 dev_dbg(&client->dev, "Forcibly "
1085 "enabling temp%d\n", i+1);
1086 pc87360_write_value(data, LD_TEMP, i,
1087 PC87365_REG_TEMP_STATUS,
1088 0xCF);
1089 }
1090 }
1091 }
1092
1093 if (use_thermistors) {
1094 for (i=11; i<data->innr; i++) {
1095 if (init >= init_in[i]) {
1096 /* The pin may already be used by thermal
1097 diodes */
1098 reg = pc87360_read_value(data, LD_TEMP,
1099 (i-11)/2, PC87365_REG_TEMP_STATUS);
1100 if (reg & 0x01) {
1101 dev_dbg(&client->dev, "Skipping "
1102 "temp%d, pin already in use "
1103 "by temp%d\n", i-7, (i-11)/2);
1104 continue;
1105 }
1106
1107 /* Forcibly enable thermistor channel */
1108 reg = pc87360_read_value(data, LD_IN, i,
1109 PC87365_REG_IN_STATUS);
1110 if (!(reg & 0x01)) {
1111 dev_dbg(&client->dev, "Forcibly "
1112 "enabling temp%d\n", i-7);
1113 pc87360_write_value(data, LD_IN, i,
1114 PC87365_REG_TEMP_STATUS,
1115 (reg & 0x60) | 0x8F);
1116 }
1117 }
1118 }
1119 }
1120
1121 if (data->innr) {
1122 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1123 PC87365_REG_IN_CONFIG);
1124 if (reg & 0x01) {
1125 dev_dbg(&client->dev, "Forcibly "
1126 "enabling monitoring (VLM)\n");
1127 pc87360_write_value(data, LD_IN, NO_BANK,
1128 PC87365_REG_IN_CONFIG,
1129 reg & 0xFE);
1130 }
1131 }
1132
1133 if (data->tempnr) {
1134 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1135 PC87365_REG_TEMP_CONFIG);
1136 if (reg & 0x01) {
1137 dev_dbg(&client->dev, "Forcibly enabling "
1138 "monitoring (TMS)\n");
1139 pc87360_write_value(data, LD_TEMP, NO_BANK,
1140 PC87365_REG_TEMP_CONFIG,
1141 reg & 0xFE);
1142 }
1143
1144 if (init >= 2) {
1145 /* Chip config as documented by National Semi. */
1146 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1147 /* We voluntarily omit the bank here, in case the
1148 sequence itself matters. It shouldn't be a problem,
1149 since nobody else is supposed to access the
1150 device at that point. */
1151 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1152 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1153 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1154 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1155 }
1156 }
1157}
1158
1159static void pc87360_autodiv(struct i2c_client *client, int nr)
1160{
1161 struct pc87360_data *data = i2c_get_clientdata(client);
1162 u8 old_min = data->fan_min[nr];
1163
1164 /* Increase clock divider if needed and possible */
1165 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1166 || (data->fan[nr] >= 224)) { /* next to overflow */
1167 if ((data->fan_status[nr] & 0x60) != 0x60) {
1168 data->fan_status[nr] += 0x20;
1169 data->fan_min[nr] >>= 1;
1170 data->fan[nr] >>= 1;
1171 dev_dbg(&client->dev, "Increasing "
1172 "clock divider to %d for fan %d\n",
1173 FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
1174 }
1175 } else {
1176 /* Decrease clock divider if possible */
1177 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1178 && data->fan[nr] < 85 /* bad accuracy */
1179 && (data->fan_status[nr] & 0x60) != 0x00) {
1180 data->fan_status[nr] -= 0x20;
1181 data->fan_min[nr] <<= 1;
1182 data->fan[nr] <<= 1;
1183 dev_dbg(&client->dev, "Decreasing "
1184 "clock divider to %d for fan %d\n",
1185 FAN_DIV_FROM_REG(data->fan_status[nr]),
1186 nr+1);
1187 }
1188 }
1189
1190 /* Write new fan min if it changed */
1191 if (old_min != data->fan_min[nr]) {
1192 pc87360_write_value(data, LD_FAN, NO_BANK,
1193 PC87360_REG_FAN_MIN(nr),
1194 data->fan_min[nr]);
1195 }
1196}
1197
1198static struct pc87360_data *pc87360_update_device(struct device *dev)
1199{
1200 struct i2c_client *client = to_i2c_client(dev);
1201 struct pc87360_data *data = i2c_get_clientdata(client);
1202 u8 i;
1203
1204 down(&data->update_lock);
1205
1206 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
1207 dev_dbg(&client->dev, "Data update\n");
1208
1209 /* Fans */
1210 for (i = 0; i < data->fannr; i++) {
1211 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1212 data->fan_status[i] =
1213 pc87360_read_value(data, LD_FAN,
1214 NO_BANK, PC87360_REG_FAN_STATUS(i));
1215 data->fan[i] = pc87360_read_value(data, LD_FAN,
1216 NO_BANK, PC87360_REG_FAN(i));
1217 data->fan_min[i] = pc87360_read_value(data,
1218 LD_FAN, NO_BANK,
1219 PC87360_REG_FAN_MIN(i));
1220 /* Change clock divider if needed */
1221 pc87360_autodiv(client, i);
1222 /* Clear bits and write new divider */
1223 pc87360_write_value(data, LD_FAN, NO_BANK,
1224 PC87360_REG_FAN_STATUS(i),
1225 data->fan_status[i]);
1226 }
1227 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1228 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1229 NO_BANK, PC87360_REG_PWM(i));
1230 }
1231
1232 /* Voltages */
1233 for (i = 0; i < data->innr; i++) {
1234 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1235 PC87365_REG_IN_STATUS);
1236 /* Clear bits */
1237 pc87360_write_value(data, LD_IN, i,
1238 PC87365_REG_IN_STATUS,
1239 data->in_status[i]);
1240 if ((data->in_status[i] & 0x81) == 0x81) {
1241 data->in[i] = pc87360_read_value(data, LD_IN,
1242 i, PC87365_REG_IN);
1243 }
1244 if (data->in_status[i] & 0x01) {
1245 data->in_min[i] = pc87360_read_value(data,
1246 LD_IN, i,
1247 PC87365_REG_IN_MIN);
1248 data->in_max[i] = pc87360_read_value(data,
1249 LD_IN, i,
1250 PC87365_REG_IN_MAX);
1251 if (i >= 11)
1252 data->in_crit[i-11] =
1253 pc87360_read_value(data, LD_IN,
1254 i, PC87365_REG_TEMP_CRIT);
1255 }
1256 }
1257 if (data->innr) {
1258 data->in_alarms = pc87360_read_value(data, LD_IN,
1259 NO_BANK, PC87365_REG_IN_ALARMS1)
1260 | ((pc87360_read_value(data, LD_IN,
1261 NO_BANK, PC87365_REG_IN_ALARMS2)
1262 & 0x07) << 8);
1263 data->vid = (data->vid_conf & 0xE0) ?
1264 pc87360_read_value(data, LD_IN,
1265 NO_BANK, PC87365_REG_VID) : 0x1F;
1266 }
1267
1268 /* Temperatures */
1269 for (i = 0; i < data->tempnr; i++) {
1270 data->temp_status[i] = pc87360_read_value(data,
1271 LD_TEMP, i,
1272 PC87365_REG_TEMP_STATUS);
1273 /* Clear bits */
1274 pc87360_write_value(data, LD_TEMP, i,
1275 PC87365_REG_TEMP_STATUS,
1276 data->temp_status[i]);
1277 if ((data->temp_status[i] & 0x81) == 0x81) {
1278 data->temp[i] = pc87360_read_value(data,
1279 LD_TEMP, i,
1280 PC87365_REG_TEMP);
1281 }
1282 if (data->temp_status[i] & 0x01) {
1283 data->temp_min[i] = pc87360_read_value(data,
1284 LD_TEMP, i,
1285 PC87365_REG_TEMP_MIN);
1286 data->temp_max[i] = pc87360_read_value(data,
1287 LD_TEMP, i,
1288 PC87365_REG_TEMP_MAX);
1289 data->temp_crit[i] = pc87360_read_value(data,
1290 LD_TEMP, i,
1291 PC87365_REG_TEMP_CRIT);
1292 }
1293 }
1294 if (data->tempnr) {
1295 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1296 NO_BANK, PC87365_REG_TEMP_ALARMS)
1297 & 0x3F;
1298 }
1299
1300 data->last_updated = jiffies;
1301 data->valid = 1;
1302 }
1303
1304 up(&data->update_lock);
1305
1306 return data;
1307}
1308
1309static int __init pc87360_init(void)
1310{
1311 int i;
1312
1313 if (pc87360_find(0x2e, &devid, extra_isa)
1314 && pc87360_find(0x4e, &devid, extra_isa)) {
1315 printk(KERN_WARNING "pc87360: PC8736x not detected, "
1316 "module not inserted.\n");
1317 return -ENODEV;
1318 }
1319
1320 /* Arbitrarily pick one of the addresses */
1321 for (i = 0; i < 3; i++) {
1322 if (extra_isa[i] != 0x0000) {
1323 normal_isa[0] = extra_isa[i];
1324 break;
1325 }
1326 }
1327
1328 if (normal_isa[0] == 0x0000) {
1329 printk(KERN_WARNING "pc87360: No active logical device, "
1330 "module not inserted.\n");
1331 return -ENODEV;
1332 }
1333
1334 return i2c_add_driver(&pc87360_driver);
1335}
1336
1337static void __exit pc87360_exit(void)
1338{
1339 i2c_del_driver(&pc87360_driver);
1340}
1341
1342
1343MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1344MODULE_DESCRIPTION("PC8736x hardware monitor");
1345MODULE_LICENSE("GPL");
1346
1347module_init(pc87360_init);
1348module_exit(pc87360_exit);
diff --git a/drivers/hwmon/sis5595.c b/drivers/hwmon/sis5595.c
new file mode 100644
index 000000000000..6bbfc8fb4f13
--- /dev/null
+++ b/drivers/hwmon/sis5595.c
@@ -0,0 +1,817 @@
1/*
2 sis5595.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
4
5 Copyright (C) 1998 - 2001 Frodo Looijaard <frodol@dds.nl>,
6 Kyösti Mälkki <kmalkki@cc.hut.fi>, and
7 Mark D. Studebaker <mdsxyz123@yahoo.com>
8 Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
9 the help of Jean Delvare <khali@linux-fr.org>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24*/
25
26/*
27 SiS southbridge has a LM78-like chip integrated on the same IC.
28 This driver is a customized copy of lm78.c
29
30 Supports following revisions:
31 Version PCI ID PCI Revision
32 1 1039/0008 AF or less
33 2 1039/0008 B0 or greater
34
35 Note: these chips contain a 0008 device which is incompatible with the
36 5595. We recognize these by the presence of the listed
37 "blacklist" PCI ID and refuse to load.
38
39 NOT SUPPORTED PCI ID BLACKLIST PCI ID
40 540 0008 0540
41 550 0008 0550
42 5513 0008 5511
43 5581 0008 5597
44 5582 0008 5597
45 5597 0008 5597
46 5598 0008 5597/5598
47 630 0008 0630
48 645 0008 0645
49 730 0008 0730
50 735 0008 0735
51*/
52
53#include <linux/module.h>
54#include <linux/slab.h>
55#include <linux/ioport.h>
56#include <linux/pci.h>
57#include <linux/i2c.h>
58#include <linux/i2c-sensor.h>
59#include <linux/init.h>
60#include <linux/jiffies.h>
61#include <asm/io.h>
62
63
64/* If force_addr is set to anything different from 0, we forcibly enable
65 the device at the given address. */
66static u16 force_addr;
67module_param(force_addr, ushort, 0);
68MODULE_PARM_DESC(force_addr,
69 "Initialize the base address of the sensors");
70
71/* Addresses to scan.
72 Note that we can't determine the ISA address until we have initialized
73 our module */
74static unsigned short normal_i2c[] = { I2C_CLIENT_END };
75static unsigned int normal_isa[] = { 0x0000, I2C_CLIENT_ISA_END };
76
77/* Insmod parameters */
78SENSORS_INSMOD_1(sis5595);
79
80/* Many SIS5595 constants specified below */
81
82/* Length of ISA address segment */
83#define SIS5595_EXTENT 8
84/* PCI Config Registers */
85#define SIS5595_REVISION_REG 0x08
86#define SIS5595_BASE_REG 0x68
87#define SIS5595_PIN_REG 0x7A
88#define SIS5595_ENABLE_REG 0x7B
89
90/* Where are the ISA address/data registers relative to the base address */
91#define SIS5595_ADDR_REG_OFFSET 5
92#define SIS5595_DATA_REG_OFFSET 6
93
94/* The SIS5595 registers */
95#define SIS5595_REG_IN_MAX(nr) (0x2b + (nr) * 2)
96#define SIS5595_REG_IN_MIN(nr) (0x2c + (nr) * 2)
97#define SIS5595_REG_IN(nr) (0x20 + (nr))
98
99#define SIS5595_REG_FAN_MIN(nr) (0x3b + (nr))
100#define SIS5595_REG_FAN(nr) (0x28 + (nr))
101
102/* On the first version of the chip, the temp registers are separate.
103 On the second version,
104 TEMP pin is shared with IN4, configured in PCI register 0x7A.
105 The registers are the same as well.
106 OVER and HYST are really MAX and MIN. */
107
108#define REV2MIN 0xb0
109#define SIS5595_REG_TEMP (( data->revision) >= REV2MIN) ? \
110 SIS5595_REG_IN(4) : 0x27
111#define SIS5595_REG_TEMP_OVER (( data->revision) >= REV2MIN) ? \
112 SIS5595_REG_IN_MAX(4) : 0x39
113#define SIS5595_REG_TEMP_HYST (( data->revision) >= REV2MIN) ? \
114 SIS5595_REG_IN_MIN(4) : 0x3a
115
116#define SIS5595_REG_CONFIG 0x40
117#define SIS5595_REG_ALARM1 0x41
118#define SIS5595_REG_ALARM2 0x42
119#define SIS5595_REG_FANDIV 0x47
120
121/* Conversions. Limit checking is only done on the TO_REG
122 variants. */
123
124/* IN: mV, (0V to 4.08V)
125 REG: 16mV/bit */
126static inline u8 IN_TO_REG(unsigned long val)
127{
128 unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
129 return (nval + 8) / 16;
130}
131#define IN_FROM_REG(val) ((val) * 16)
132
133static inline u8 FAN_TO_REG(long rpm, int div)
134{
135 if (rpm <= 0)
136 return 255;
137 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
138}
139
140static inline int FAN_FROM_REG(u8 val, int div)
141{
142 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
143}
144
145/* TEMP: mC (-54.12C to +157.53C)
146 REG: 0.83C/bit + 52.12, two's complement */
147static inline int TEMP_FROM_REG(s8 val)
148{
149 return val * 830 + 52120;
150}
151static inline s8 TEMP_TO_REG(int val)
152{
153 int nval = SENSORS_LIMIT(val, -54120, 157530) ;
154 return nval<0 ? (nval-5212-415)/830 : (nval-5212+415)/830;
155}
156
157/* FAN DIV: 1, 2, 4, or 8 (defaults to 2)
158 REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */
159static inline u8 DIV_TO_REG(int val)
160{
161 return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1;
162}
163#define DIV_FROM_REG(val) (1 << (val))
164
165/* For the SIS5595, we need to keep some data in memory. That
166 data is pointed to by sis5595_list[NR]->data. The structure itself is
167 dynamically allocated, at the time when the new sis5595 client is
168 allocated. */
169struct sis5595_data {
170 struct i2c_client client;
171 struct semaphore lock;
172
173 struct semaphore update_lock;
174 char valid; /* !=0 if following fields are valid */
175 unsigned long last_updated; /* In jiffies */
176 char maxins; /* == 3 if temp enabled, otherwise == 4 */
177 u8 revision; /* Reg. value */
178
179 u8 in[5]; /* Register value */
180 u8 in_max[5]; /* Register value */
181 u8 in_min[5]; /* Register value */
182 u8 fan[2]; /* Register value */
183 u8 fan_min[2]; /* Register value */
184 s8 temp; /* Register value */
185 s8 temp_over; /* Register value */
186 s8 temp_hyst; /* Register value */
187 u8 fan_div[2]; /* Register encoding, shifted right */
188 u16 alarms; /* Register encoding, combined */
189};
190
191static struct pci_dev *s_bridge; /* pointer to the (only) sis5595 */
192
193static int sis5595_attach_adapter(struct i2c_adapter *adapter);
194static int sis5595_detect(struct i2c_adapter *adapter, int address, int kind);
195static int sis5595_detach_client(struct i2c_client *client);
196
197static int sis5595_read_value(struct i2c_client *client, u8 register);
198static int sis5595_write_value(struct i2c_client *client, u8 register, u8 value);
199static struct sis5595_data *sis5595_update_device(struct device *dev);
200static void sis5595_init_client(struct i2c_client *client);
201
202static struct i2c_driver sis5595_driver = {
203 .owner = THIS_MODULE,
204 .name = "sis5595",
205 .id = I2C_DRIVERID_SIS5595,
206 .flags = I2C_DF_NOTIFY,
207 .attach_adapter = sis5595_attach_adapter,
208 .detach_client = sis5595_detach_client,
209};
210
211/* 4 Voltages */
212static ssize_t show_in(struct device *dev, char *buf, int nr)
213{
214 struct sis5595_data *data = sis5595_update_device(dev);
215 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
216}
217
218static ssize_t show_in_min(struct device *dev, char *buf, int nr)
219{
220 struct sis5595_data *data = sis5595_update_device(dev);
221 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
222}
223
224static ssize_t show_in_max(struct device *dev, char *buf, int nr)
225{
226 struct sis5595_data *data = sis5595_update_device(dev);
227 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
228}
229
230static ssize_t set_in_min(struct device *dev, const char *buf,
231 size_t count, int nr)
232{
233 struct i2c_client *client = to_i2c_client(dev);
234 struct sis5595_data *data = i2c_get_clientdata(client);
235 unsigned long val = simple_strtoul(buf, NULL, 10);
236
237 down(&data->update_lock);
238 data->in_min[nr] = IN_TO_REG(val);
239 sis5595_write_value(client, SIS5595_REG_IN_MIN(nr), data->in_min[nr]);
240 up(&data->update_lock);
241 return count;
242}
243
244static ssize_t set_in_max(struct device *dev, const char *buf,
245 size_t count, int nr)
246{
247 struct i2c_client *client = to_i2c_client(dev);
248 struct sis5595_data *data = i2c_get_clientdata(client);
249 unsigned long val = simple_strtoul(buf, NULL, 10);
250
251 down(&data->update_lock);
252 data->in_max[nr] = IN_TO_REG(val);
253 sis5595_write_value(client, SIS5595_REG_IN_MAX(nr), data->in_max[nr]);
254 up(&data->update_lock);
255 return count;
256}
257
258#define show_in_offset(offset) \
259static ssize_t \
260 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
261{ \
262 return show_in(dev, buf, offset); \
263} \
264static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
265 show_in##offset, NULL); \
266static ssize_t \
267 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
268{ \
269 return show_in_min(dev, buf, offset); \
270} \
271static ssize_t \
272 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
273{ \
274 return show_in_max(dev, buf, offset); \
275} \
276static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
277 const char *buf, size_t count) \
278{ \
279 return set_in_min(dev, buf, count, offset); \
280} \
281static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
282 const char *buf, size_t count) \
283{ \
284 return set_in_max(dev, buf, count, offset); \
285} \
286static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
287 show_in##offset##_min, set_in##offset##_min); \
288static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
289 show_in##offset##_max, set_in##offset##_max);
290
291show_in_offset(0);
292show_in_offset(1);
293show_in_offset(2);
294show_in_offset(3);
295show_in_offset(4);
296
297/* Temperature */
298static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
299{
300 struct sis5595_data *data = sis5595_update_device(dev);
301 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
302}
303
304static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr, char *buf)
305{
306 struct sis5595_data *data = sis5595_update_device(dev);
307 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
308}
309
310static ssize_t set_temp_over(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
311{
312 struct i2c_client *client = to_i2c_client(dev);
313 struct sis5595_data *data = i2c_get_clientdata(client);
314 long val = simple_strtol(buf, NULL, 10);
315
316 down(&data->update_lock);
317 data->temp_over = TEMP_TO_REG(val);
318 sis5595_write_value(client, SIS5595_REG_TEMP_OVER, data->temp_over);
319 up(&data->update_lock);
320 return count;
321}
322
323static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
324{
325 struct sis5595_data *data = sis5595_update_device(dev);
326 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
327}
328
329static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
330{
331 struct i2c_client *client = to_i2c_client(dev);
332 struct sis5595_data *data = i2c_get_clientdata(client);
333 long val = simple_strtol(buf, NULL, 10);
334
335 down(&data->update_lock);
336 data->temp_hyst = TEMP_TO_REG(val);
337 sis5595_write_value(client, SIS5595_REG_TEMP_HYST, data->temp_hyst);
338 up(&data->update_lock);
339 return count;
340}
341
342static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
343static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
344 show_temp_over, set_temp_over);
345static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
346 show_temp_hyst, set_temp_hyst);
347
348/* 2 Fans */
349static ssize_t show_fan(struct device *dev, char *buf, int nr)
350{
351 struct sis5595_data *data = sis5595_update_device(dev);
352 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
353 DIV_FROM_REG(data->fan_div[nr])) );
354}
355
356static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
357{
358 struct sis5595_data *data = sis5595_update_device(dev);
359 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
360 DIV_FROM_REG(data->fan_div[nr])) );
361}
362
363static ssize_t set_fan_min(struct device *dev, const char *buf,
364 size_t count, int nr)
365{
366 struct i2c_client *client = to_i2c_client(dev);
367 struct sis5595_data *data = i2c_get_clientdata(client);
368 unsigned long val = simple_strtoul(buf, NULL, 10);
369
370 down(&data->update_lock);
371 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
372 sis5595_write_value(client, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
373 up(&data->update_lock);
374 return count;
375}
376
377static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
378{
379 struct sis5595_data *data = sis5595_update_device(dev);
380 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
381}
382
383/* Note: we save and restore the fan minimum here, because its value is
384 determined in part by the fan divisor. This follows the principle of
385 least suprise; the user doesn't expect the fan minimum to change just
386 because the divisor changed. */
387static ssize_t set_fan_div(struct device *dev, const char *buf,
388 size_t count, int nr)
389{
390 struct i2c_client *client = to_i2c_client(dev);
391 struct sis5595_data *data = i2c_get_clientdata(client);
392 unsigned long min;
393 unsigned long val = simple_strtoul(buf, NULL, 10);
394 int reg;
395
396 down(&data->update_lock);
397 min = FAN_FROM_REG(data->fan_min[nr],
398 DIV_FROM_REG(data->fan_div[nr]));
399 reg = sis5595_read_value(client, SIS5595_REG_FANDIV);
400
401 switch (val) {
402 case 1: data->fan_div[nr] = 0; break;
403 case 2: data->fan_div[nr] = 1; break;
404 case 4: data->fan_div[nr] = 2; break;
405 case 8: data->fan_div[nr] = 3; break;
406 default:
407 dev_err(&client->dev, "fan_div value %ld not "
408 "supported. Choose one of 1, 2, 4 or 8!\n", val);
409 up(&data->update_lock);
410 return -EINVAL;
411 }
412
413 switch (nr) {
414 case 0:
415 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
416 break;
417 case 1:
418 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
419 break;
420 }
421 sis5595_write_value(client, SIS5595_REG_FANDIV, reg);
422 data->fan_min[nr] =
423 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
424 sis5595_write_value(client, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
425 up(&data->update_lock);
426 return count;
427}
428
429#define show_fan_offset(offset) \
430static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
431{ \
432 return show_fan(dev, buf, offset - 1); \
433} \
434static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
435{ \
436 return show_fan_min(dev, buf, offset - 1); \
437} \
438static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
439{ \
440 return show_fan_div(dev, buf, offset - 1); \
441} \
442static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
443 const char *buf, size_t count) \
444{ \
445 return set_fan_min(dev, buf, count, offset - 1); \
446} \
447static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
448static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
449 show_fan_##offset##_min, set_fan_##offset##_min);
450
451show_fan_offset(1);
452show_fan_offset(2);
453
454static ssize_t set_fan_1_div(struct device *dev, struct device_attribute *attr, const char *buf,
455 size_t count)
456{
457 return set_fan_div(dev, buf, count, 0) ;
458}
459
460static ssize_t set_fan_2_div(struct device *dev, struct device_attribute *attr, const char *buf,
461 size_t count)
462{
463 return set_fan_div(dev, buf, count, 1) ;
464}
465static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
466 show_fan_1_div, set_fan_1_div);
467static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
468 show_fan_2_div, set_fan_2_div);
469
470/* Alarms */
471static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
472{
473 struct sis5595_data *data = sis5595_update_device(dev);
474 return sprintf(buf, "%d\n", data->alarms);
475}
476static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
477
478/* This is called when the module is loaded */
479static int sis5595_attach_adapter(struct i2c_adapter *adapter)
480{
481 if (!(adapter->class & I2C_CLASS_HWMON))
482 return 0;
483 return i2c_detect(adapter, &addr_data, sis5595_detect);
484}
485
486int sis5595_detect(struct i2c_adapter *adapter, int address, int kind)
487{
488 int err = 0;
489 int i;
490 struct i2c_client *new_client;
491 struct sis5595_data *data;
492 char val;
493 u16 a;
494
495 /* Make sure we are probing the ISA bus!! */
496 if (!i2c_is_isa_adapter(adapter))
497 goto exit;
498
499 if (force_addr)
500 address = force_addr & ~(SIS5595_EXTENT - 1);
501 /* Reserve the ISA region */
502 if (!request_region(address, SIS5595_EXTENT, sis5595_driver.name)) {
503 err = -EBUSY;
504 goto exit;
505 }
506 if (force_addr) {
507 dev_warn(&adapter->dev, "forcing ISA address 0x%04X\n", address);
508 if (PCIBIOS_SUCCESSFUL !=
509 pci_write_config_word(s_bridge, SIS5595_BASE_REG, address))
510 goto exit_release;
511 if (PCIBIOS_SUCCESSFUL !=
512 pci_read_config_word(s_bridge, SIS5595_BASE_REG, &a))
513 goto exit_release;
514 if ((a & ~(SIS5595_EXTENT - 1)) != address)
515 /* doesn't work for some chips? */
516 goto exit_release;
517 }
518
519 if (PCIBIOS_SUCCESSFUL !=
520 pci_read_config_byte(s_bridge, SIS5595_ENABLE_REG, &val)) {
521 goto exit_release;
522 }
523 if ((val & 0x80) == 0) {
524 if (PCIBIOS_SUCCESSFUL !=
525 pci_write_config_byte(s_bridge, SIS5595_ENABLE_REG,
526 val | 0x80))
527 goto exit_release;
528 if (PCIBIOS_SUCCESSFUL !=
529 pci_read_config_byte(s_bridge, SIS5595_ENABLE_REG, &val))
530 goto exit_release;
531 if ((val & 0x80) == 0)
532 /* doesn't work for some chips! */
533 goto exit_release;
534 }
535
536 if (!(data = kmalloc(sizeof(struct sis5595_data), GFP_KERNEL))) {
537 err = -ENOMEM;
538 goto exit_release;
539 }
540 memset(data, 0, sizeof(struct sis5595_data));
541
542 new_client = &data->client;
543 new_client->addr = address;
544 init_MUTEX(&data->lock);
545 i2c_set_clientdata(new_client, data);
546 new_client->adapter = adapter;
547 new_client->driver = &sis5595_driver;
548 new_client->flags = 0;
549
550 /* Check revision and pin registers to determine whether 4 or 5 voltages */
551 pci_read_config_byte(s_bridge, SIS5595_REVISION_REG, &(data->revision));
552 /* 4 voltages, 1 temp */
553 data->maxins = 3;
554 if (data->revision >= REV2MIN) {
555 pci_read_config_byte(s_bridge, SIS5595_PIN_REG, &val);
556 if (!(val & 0x80))
557 /* 5 voltages, no temps */
558 data->maxins = 4;
559 }
560
561 /* Fill in the remaining client fields and put it into the global list */
562 strlcpy(new_client->name, "sis5595", I2C_NAME_SIZE);
563
564 data->valid = 0;
565 init_MUTEX(&data->update_lock);
566
567 /* Tell the I2C layer a new client has arrived */
568 if ((err = i2c_attach_client(new_client)))
569 goto exit_free;
570
571 /* Initialize the SIS5595 chip */
572 sis5595_init_client(new_client);
573
574 /* A few vars need to be filled upon startup */
575 for (i = 0; i < 2; i++) {
576 data->fan_min[i] = sis5595_read_value(new_client,
577 SIS5595_REG_FAN_MIN(i));
578 }
579
580 /* Register sysfs hooks */
581 device_create_file(&new_client->dev, &dev_attr_in0_input);
582 device_create_file(&new_client->dev, &dev_attr_in0_min);
583 device_create_file(&new_client->dev, &dev_attr_in0_max);
584 device_create_file(&new_client->dev, &dev_attr_in1_input);
585 device_create_file(&new_client->dev, &dev_attr_in1_min);
586 device_create_file(&new_client->dev, &dev_attr_in1_max);
587 device_create_file(&new_client->dev, &dev_attr_in2_input);
588 device_create_file(&new_client->dev, &dev_attr_in2_min);
589 device_create_file(&new_client->dev, &dev_attr_in2_max);
590 device_create_file(&new_client->dev, &dev_attr_in3_input);
591 device_create_file(&new_client->dev, &dev_attr_in3_min);
592 device_create_file(&new_client->dev, &dev_attr_in3_max);
593 if (data->maxins == 4) {
594 device_create_file(&new_client->dev, &dev_attr_in4_input);
595 device_create_file(&new_client->dev, &dev_attr_in4_min);
596 device_create_file(&new_client->dev, &dev_attr_in4_max);
597 }
598 device_create_file(&new_client->dev, &dev_attr_fan1_input);
599 device_create_file(&new_client->dev, &dev_attr_fan1_min);
600 device_create_file(&new_client->dev, &dev_attr_fan1_div);
601 device_create_file(&new_client->dev, &dev_attr_fan2_input);
602 device_create_file(&new_client->dev, &dev_attr_fan2_min);
603 device_create_file(&new_client->dev, &dev_attr_fan2_div);
604 device_create_file(&new_client->dev, &dev_attr_alarms);
605 if (data->maxins == 3) {
606 device_create_file(&new_client->dev, &dev_attr_temp1_input);
607 device_create_file(&new_client->dev, &dev_attr_temp1_max);
608 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
609 }
610 return 0;
611
612exit_free:
613 kfree(data);
614exit_release:
615 release_region(address, SIS5595_EXTENT);
616exit:
617 return err;
618}
619
620static int sis5595_detach_client(struct i2c_client *client)
621{
622 int err;
623
624 if ((err = i2c_detach_client(client))) {
625 dev_err(&client->dev,
626 "Client deregistration failed, client not detached.\n");
627 return err;
628 }
629
630 if (i2c_is_isa_client(client))
631 release_region(client->addr, SIS5595_EXTENT);
632
633 kfree(i2c_get_clientdata(client));
634
635 return 0;
636}
637
638
639/* ISA access must be locked explicitly. */
640static int sis5595_read_value(struct i2c_client *client, u8 reg)
641{
642 int res;
643
644 struct sis5595_data *data = i2c_get_clientdata(client);
645 down(&data->lock);
646 outb_p(reg, client->addr + SIS5595_ADDR_REG_OFFSET);
647 res = inb_p(client->addr + SIS5595_DATA_REG_OFFSET);
648 up(&data->lock);
649 return res;
650}
651
652static int sis5595_write_value(struct i2c_client *client, u8 reg, u8 value)
653{
654 struct sis5595_data *data = i2c_get_clientdata(client);
655 down(&data->lock);
656 outb_p(reg, client->addr + SIS5595_ADDR_REG_OFFSET);
657 outb_p(value, client->addr + SIS5595_DATA_REG_OFFSET);
658 up(&data->lock);
659 return 0;
660}
661
662/* Called when we have found a new SIS5595. */
663static void sis5595_init_client(struct i2c_client *client)
664{
665 u8 config = sis5595_read_value(client, SIS5595_REG_CONFIG);
666 if (!(config & 0x01))
667 sis5595_write_value(client, SIS5595_REG_CONFIG,
668 (config & 0xf7) | 0x01);
669}
670
671static struct sis5595_data *sis5595_update_device(struct device *dev)
672{
673 struct i2c_client *client = to_i2c_client(dev);
674 struct sis5595_data *data = i2c_get_clientdata(client);
675 int i;
676
677 down(&data->update_lock);
678
679 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
680 || !data->valid) {
681
682 for (i = 0; i <= data->maxins; i++) {
683 data->in[i] =
684 sis5595_read_value(client, SIS5595_REG_IN(i));
685 data->in_min[i] =
686 sis5595_read_value(client,
687 SIS5595_REG_IN_MIN(i));
688 data->in_max[i] =
689 sis5595_read_value(client,
690 SIS5595_REG_IN_MAX(i));
691 }
692 for (i = 0; i < 2; i++) {
693 data->fan[i] =
694 sis5595_read_value(client, SIS5595_REG_FAN(i));
695 data->fan_min[i] =
696 sis5595_read_value(client,
697 SIS5595_REG_FAN_MIN(i));
698 }
699 if (data->maxins == 3) {
700 data->temp =
701 sis5595_read_value(client, SIS5595_REG_TEMP);
702 data->temp_over =
703 sis5595_read_value(client, SIS5595_REG_TEMP_OVER);
704 data->temp_hyst =
705 sis5595_read_value(client, SIS5595_REG_TEMP_HYST);
706 }
707 i = sis5595_read_value(client, SIS5595_REG_FANDIV);
708 data->fan_div[0] = (i >> 4) & 0x03;
709 data->fan_div[1] = i >> 6;
710 data->alarms =
711 sis5595_read_value(client, SIS5595_REG_ALARM1) |
712 (sis5595_read_value(client, SIS5595_REG_ALARM2) << 8);
713 data->last_updated = jiffies;
714 data->valid = 1;
715 }
716
717 up(&data->update_lock);
718
719 return data;
720}
721
722static struct pci_device_id sis5595_pci_ids[] = {
723 { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
724 { 0, }
725};
726
727MODULE_DEVICE_TABLE(pci, sis5595_pci_ids);
728
729static int blacklist[] __devinitdata = {
730 PCI_DEVICE_ID_SI_540,
731 PCI_DEVICE_ID_SI_550,
732 PCI_DEVICE_ID_SI_630,
733 PCI_DEVICE_ID_SI_645,
734 PCI_DEVICE_ID_SI_730,
735 PCI_DEVICE_ID_SI_735,
736 PCI_DEVICE_ID_SI_5511, /* 5513 chip has the 0008 device but
737 that ID shows up in other chips so we
738 use the 5511 ID for recognition */
739 PCI_DEVICE_ID_SI_5597,
740 PCI_DEVICE_ID_SI_5598,
741 0 };
742
743static int __devinit sis5595_pci_probe(struct pci_dev *dev,
744 const struct pci_device_id *id)
745{
746 u16 val;
747 int *i;
748 int addr = 0;
749
750 for (i = blacklist; *i != 0; i++) {
751 struct pci_dev *dev;
752 dev = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
753 if (dev) {
754 dev_err(&dev->dev, "Looked for SIS5595 but found unsupported device %.4x\n", *i);
755 pci_dev_put(dev);
756 return -ENODEV;
757 }
758 }
759
760 if (PCIBIOS_SUCCESSFUL !=
761 pci_read_config_word(dev, SIS5595_BASE_REG, &val))
762 return -ENODEV;
763
764 addr = val & ~(SIS5595_EXTENT - 1);
765 if (addr == 0 && force_addr == 0) {
766 dev_err(&dev->dev, "Base address not set - upgrade BIOS or use force_addr=0xaddr\n");
767 return -ENODEV;
768 }
769 if (force_addr)
770 addr = force_addr; /* so detect will get called */
771
772 if (!addr) {
773 dev_err(&dev->dev,"No SiS 5595 sensors found.\n");
774 return -ENODEV;
775 }
776 normal_isa[0] = addr;
777
778 s_bridge = pci_dev_get(dev);
779 if (i2c_add_driver(&sis5595_driver)) {
780 pci_dev_put(s_bridge);
781 s_bridge = NULL;
782 }
783
784 /* Always return failure here. This is to allow other drivers to bind
785 * to this pci device. We don't really want to have control over the
786 * pci device, we only wanted to read as few register values from it.
787 */
788 return -ENODEV;
789}
790
791static struct pci_driver sis5595_pci_driver = {
792 .name = "sis5595",
793 .id_table = sis5595_pci_ids,
794 .probe = sis5595_pci_probe,
795};
796
797static int __init sm_sis5595_init(void)
798{
799 return pci_register_driver(&sis5595_pci_driver);
800}
801
802static void __exit sm_sis5595_exit(void)
803{
804 pci_unregister_driver(&sis5595_pci_driver);
805 if (s_bridge != NULL) {
806 i2c_del_driver(&sis5595_driver);
807 pci_dev_put(s_bridge);
808 s_bridge = NULL;
809 }
810}
811
812MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
813MODULE_DESCRIPTION("SiS 5595 Sensor device");
814MODULE_LICENSE("GPL");
815
816module_init(sm_sis5595_init);
817module_exit(sm_sis5595_exit);
diff --git a/drivers/hwmon/smsc47b397.c b/drivers/hwmon/smsc47b397.c
new file mode 100644
index 000000000000..251ac2659554
--- /dev/null
+++ b/drivers/hwmon/smsc47b397.c
@@ -0,0 +1,352 @@
1/*
2 smsc47b397.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
4
5 Supports the SMSC LPC47B397-NC Super-I/O chip.
6
7 Author/Maintainer: Mark M. Hoffman <mhoffman@lightlink.com>
8 Copyright (C) 2004 Utilitek Systems, Inc.
9
10 derived in part from smsc47m1.c:
11 Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
12 Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27*/
28
29#include <linux/module.h>
30#include <linux/slab.h>
31#include <linux/ioport.h>
32#include <linux/jiffies.h>
33#include <linux/i2c.h>
34#include <linux/i2c-sensor.h>
35#include <linux/init.h>
36#include <asm/io.h>
37
38static unsigned short normal_i2c[] = { I2C_CLIENT_END };
39/* Address is autodetected, there is no default value */
40static unsigned int normal_isa[] = { 0x0000, I2C_CLIENT_ISA_END };
41static struct i2c_force_data forces[] = {{NULL}};
42
43enum chips { any_chip, smsc47b397 };
44static struct i2c_address_data addr_data = {
45 .normal_i2c = normal_i2c,
46 .normal_isa = normal_isa,
47 .probe = normal_i2c, /* cheat */
48 .ignore = normal_i2c, /* cheat */
49 .forces = forces,
50};
51
52/* Super-I/0 registers and commands */
53
54#define REG 0x2e /* The register to read/write */
55#define VAL 0x2f /* The value to read/write */
56
57static inline void superio_outb(int reg, int val)
58{
59 outb(reg, REG);
60 outb(val, VAL);
61}
62
63static inline int superio_inb(int reg)
64{
65 outb(reg, REG);
66 return inb(VAL);
67}
68
69/* select superio logical device */
70static inline void superio_select(int ld)
71{
72 superio_outb(0x07, ld);
73}
74
75static inline void superio_enter(void)
76{
77 outb(0x55, REG);
78}
79
80static inline void superio_exit(void)
81{
82 outb(0xAA, REG);
83}
84
85#define SUPERIO_REG_DEVID 0x20
86#define SUPERIO_REG_DEVREV 0x21
87#define SUPERIO_REG_BASE_MSB 0x60
88#define SUPERIO_REG_BASE_LSB 0x61
89#define SUPERIO_REG_LD8 0x08
90
91#define SMSC_EXTENT 0x02
92
93/* 0 <= nr <= 3 */
94static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
95#define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
96
97/* 0 <= nr <= 3 */
98#define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
99#define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
100
101struct smsc47b397_data {
102 struct i2c_client client;
103 struct semaphore lock;
104
105 struct semaphore update_lock;
106 unsigned long last_updated; /* in jiffies */
107 int valid;
108
109 /* register values */
110 u16 fan[4];
111 u8 temp[4];
112};
113
114static int smsc47b397_read_value(struct i2c_client *client, u8 reg)
115{
116 struct smsc47b397_data *data = i2c_get_clientdata(client);
117 int res;
118
119 down(&data->lock);
120 outb(reg, client->addr);
121 res = inb_p(client->addr + 1);
122 up(&data->lock);
123 return res;
124}
125
126static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
127{
128 struct i2c_client *client = to_i2c_client(dev);
129 struct smsc47b397_data *data = i2c_get_clientdata(client);
130 int i;
131
132 down(&data->update_lock);
133
134 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
135 dev_dbg(&client->dev, "starting device update...\n");
136
137 /* 4 temperature inputs, 4 fan inputs */
138 for (i = 0; i < 4; i++) {
139 data->temp[i] = smsc47b397_read_value(client,
140 SMSC47B397_REG_TEMP(i));
141
142 /* must read LSB first */
143 data->fan[i] = smsc47b397_read_value(client,
144 SMSC47B397_REG_FAN_LSB(i));
145 data->fan[i] |= smsc47b397_read_value(client,
146 SMSC47B397_REG_FAN_MSB(i)) << 8;
147 }
148
149 data->last_updated = jiffies;
150 data->valid = 1;
151
152 dev_dbg(&client->dev, "... device update complete\n");
153 }
154
155 up(&data->update_lock);
156
157 return data;
158}
159
160/* TEMP: 0.001C/bit (-128C to +127C)
161 REG: 1C/bit, two's complement */
162static int temp_from_reg(u8 reg)
163{
164 return (s8)reg * 1000;
165}
166
167/* 0 <= nr <= 3 */
168static ssize_t show_temp(struct device *dev, char *buf, int nr)
169{
170 struct smsc47b397_data *data = smsc47b397_update_device(dev);
171 return sprintf(buf, "%d\n", temp_from_reg(data->temp[nr]));
172}
173
174#define sysfs_temp(num) \
175static ssize_t show_temp##num(struct device *dev, struct device_attribute *attr, char *buf) \
176{ \
177 return show_temp(dev, buf, num-1); \
178} \
179static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL)
180
181sysfs_temp(1);
182sysfs_temp(2);
183sysfs_temp(3);
184sysfs_temp(4);
185
186#define device_create_file_temp(client, num) \
187 device_create_file(&client->dev, &dev_attr_temp##num##_input)
188
189/* FAN: 1 RPM/bit
190 REG: count of 90kHz pulses / revolution */
191static int fan_from_reg(u16 reg)
192{
193 return 90000 * 60 / reg;
194}
195
196/* 0 <= nr <= 3 */
197static ssize_t show_fan(struct device *dev, char *buf, int nr)
198{
199 struct smsc47b397_data *data = smsc47b397_update_device(dev);
200 return sprintf(buf, "%d\n", fan_from_reg(data->fan[nr]));
201}
202
203#define sysfs_fan(num) \
204static ssize_t show_fan##num(struct device *dev, struct device_attribute *attr, char *buf) \
205{ \
206 return show_fan(dev, buf, num-1); \
207} \
208static DEVICE_ATTR(fan##num##_input, S_IRUGO, show_fan##num, NULL)
209
210sysfs_fan(1);
211sysfs_fan(2);
212sysfs_fan(3);
213sysfs_fan(4);
214
215#define device_create_file_fan(client, num) \
216 device_create_file(&client->dev, &dev_attr_fan##num##_input)
217
218static int smsc47b397_detect(struct i2c_adapter *adapter, int addr, int kind);
219
220static int smsc47b397_attach_adapter(struct i2c_adapter *adapter)
221{
222 if (!(adapter->class & I2C_CLASS_HWMON))
223 return 0;
224 return i2c_detect(adapter, &addr_data, smsc47b397_detect);
225}
226
227static int smsc47b397_detach_client(struct i2c_client *client)
228{
229 int err;
230
231 if ((err = i2c_detach_client(client))) {
232 dev_err(&client->dev, "Client deregistration failed, "
233 "client not detached.\n");
234 return err;
235 }
236
237 release_region(client->addr, SMSC_EXTENT);
238 kfree(i2c_get_clientdata(client));
239
240 return 0;
241}
242
243static struct i2c_driver smsc47b397_driver = {
244 .owner = THIS_MODULE,
245 .name = "smsc47b397",
246 .id = I2C_DRIVERID_SMSC47B397,
247 .flags = I2C_DF_NOTIFY,
248 .attach_adapter = smsc47b397_attach_adapter,
249 .detach_client = smsc47b397_detach_client,
250};
251
252static int smsc47b397_detect(struct i2c_adapter *adapter, int addr, int kind)
253{
254 struct i2c_client *new_client;
255 struct smsc47b397_data *data;
256 int err = 0;
257
258 if (!i2c_is_isa_adapter(adapter)) {
259 return 0;
260 }
261
262 if (!request_region(addr, SMSC_EXTENT, smsc47b397_driver.name)) {
263 dev_err(&adapter->dev, "Region 0x%x already in use!\n", addr);
264 return -EBUSY;
265 }
266
267 if (!(data = kmalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) {
268 err = -ENOMEM;
269 goto error_release;
270 }
271 memset(data, 0x00, sizeof(struct smsc47b397_data));
272
273 new_client = &data->client;
274 i2c_set_clientdata(new_client, data);
275 new_client->addr = addr;
276 init_MUTEX(&data->lock);
277 new_client->adapter = adapter;
278 new_client->driver = &smsc47b397_driver;
279 new_client->flags = 0;
280
281 strlcpy(new_client->name, "smsc47b397", I2C_NAME_SIZE);
282
283 init_MUTEX(&data->update_lock);
284
285 if ((err = i2c_attach_client(new_client)))
286 goto error_free;
287
288 device_create_file_temp(new_client, 1);
289 device_create_file_temp(new_client, 2);
290 device_create_file_temp(new_client, 3);
291 device_create_file_temp(new_client, 4);
292
293 device_create_file_fan(new_client, 1);
294 device_create_file_fan(new_client, 2);
295 device_create_file_fan(new_client, 3);
296 device_create_file_fan(new_client, 4);
297
298 return 0;
299
300error_free:
301 kfree(new_client);
302error_release:
303 release_region(addr, SMSC_EXTENT);
304 return err;
305}
306
307static int __init smsc47b397_find(unsigned int *addr)
308{
309 u8 id, rev;
310
311 superio_enter();
312 id = superio_inb(SUPERIO_REG_DEVID);
313
314 if (id != 0x6f) {
315 superio_exit();
316 return -ENODEV;
317 }
318
319 rev = superio_inb(SUPERIO_REG_DEVREV);
320
321 superio_select(SUPERIO_REG_LD8);
322 *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
323 | superio_inb(SUPERIO_REG_BASE_LSB);
324
325 printk(KERN_INFO "smsc47b397: found SMSC LPC47B397-NC "
326 "(base address 0x%04x, revision %u)\n", *addr, rev);
327
328 superio_exit();
329 return 0;
330}
331
332static int __init smsc47b397_init(void)
333{
334 int ret;
335
336 if ((ret = smsc47b397_find(normal_isa)))
337 return ret;
338
339 return i2c_add_driver(&smsc47b397_driver);
340}
341
342static void __exit smsc47b397_exit(void)
343{
344 i2c_del_driver(&smsc47b397_driver);
345}
346
347MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
348MODULE_DESCRIPTION("SMSC LPC47B397 driver");
349MODULE_LICENSE("GPL");
350
351module_init(smsc47b397_init);
352module_exit(smsc47b397_exit);
diff --git a/drivers/hwmon/smsc47m1.c b/drivers/hwmon/smsc47m1.c
new file mode 100644
index 000000000000..897117a7213f
--- /dev/null
+++ b/drivers/hwmon/smsc47m1.c
@@ -0,0 +1,593 @@
1/*
2 smsc47m1.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
4
5 Supports the SMSC LPC47B27x, LPC47M10x, LPC47M13x and LPC47M14x
6 Super-I/O chips.
7
8 Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
9 Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
10 Ported to Linux 2.6 by Gabriele Gorla <gorlik@yahoo.com>
11 and Jean Delvare
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26*/
27
28#include <linux/module.h>
29#include <linux/slab.h>
30#include <linux/ioport.h>
31#include <linux/jiffies.h>
32#include <linux/i2c.h>
33#include <linux/i2c-sensor.h>
34#include <linux/init.h>
35#include <asm/io.h>
36
37static unsigned short normal_i2c[] = { I2C_CLIENT_END };
38/* Address is autodetected, there is no default value */
39static unsigned int normal_isa[] = { 0x0000, I2C_CLIENT_ISA_END };
40static struct i2c_force_data forces[] = {{NULL}};
41
42enum chips { any_chip, smsc47m1 };
43static struct i2c_address_data addr_data = {
44 .normal_i2c = normal_i2c,
45 .normal_isa = normal_isa,
46 .forces = forces,
47};
48
49/* Super-I/0 registers and commands */
50
51#define REG 0x2e /* The register to read/write */
52#define VAL 0x2f /* The value to read/write */
53
54static inline void
55superio_outb(int reg, int val)
56{
57 outb(reg, REG);
58 outb(val, VAL);
59}
60
61static inline int
62superio_inb(int reg)
63{
64 outb(reg, REG);
65 return inb(VAL);
66}
67
68/* logical device for fans is 0x0A */
69#define superio_select() superio_outb(0x07, 0x0A)
70
71static inline void
72superio_enter(void)
73{
74 outb(0x55, REG);
75}
76
77static inline void
78superio_exit(void)
79{
80 outb(0xAA, REG);
81}
82
83#define SUPERIO_REG_ACT 0x30
84#define SUPERIO_REG_BASE 0x60
85#define SUPERIO_REG_DEVID 0x20
86
87/* Logical device registers */
88
89#define SMSC_EXTENT 0x80
90
91/* nr is 0 or 1 in the macros below */
92#define SMSC47M1_REG_ALARM 0x04
93#define SMSC47M1_REG_TPIN(nr) (0x34 - (nr))
94#define SMSC47M1_REG_PPIN(nr) (0x36 - (nr))
95#define SMSC47M1_REG_PWM(nr) (0x56 + (nr))
96#define SMSC47M1_REG_FANDIV 0x58
97#define SMSC47M1_REG_FAN(nr) (0x59 + (nr))
98#define SMSC47M1_REG_FAN_PRELOAD(nr) (0x5B + (nr))
99
100#define MIN_FROM_REG(reg,div) ((reg)>=192 ? 0 : \
101 983040/((192-(reg))*(div)))
102#define FAN_FROM_REG(reg,div,preload) ((reg)<=(preload) || (reg)==255 ? 0 : \
103 983040/(((reg)-(preload))*(div)))
104#define DIV_FROM_REG(reg) (1 << (reg))
105#define PWM_FROM_REG(reg) (((reg) & 0x7E) << 1)
106#define PWM_EN_FROM_REG(reg) ((~(reg)) & 0x01)
107#define PWM_TO_REG(reg) (((reg) >> 1) & 0x7E)
108
109struct smsc47m1_data {
110 struct i2c_client client;
111 struct semaphore lock;
112
113 struct semaphore update_lock;
114 unsigned long last_updated; /* In jiffies */
115
116 u8 fan[2]; /* Register value */
117 u8 fan_preload[2]; /* Register value */
118 u8 fan_div[2]; /* Register encoding, shifted right */
119 u8 alarms; /* Register encoding */
120 u8 pwm[2]; /* Register value (bit 7 is enable) */
121};
122
123
124static int smsc47m1_attach_adapter(struct i2c_adapter *adapter);
125static int smsc47m1_find(int *address);
126static int smsc47m1_detect(struct i2c_adapter *adapter, int address, int kind);
127static int smsc47m1_detach_client(struct i2c_client *client);
128
129static int smsc47m1_read_value(struct i2c_client *client, u8 reg);
130static void smsc47m1_write_value(struct i2c_client *client, u8 reg, u8 value);
131
132static struct smsc47m1_data *smsc47m1_update_device(struct device *dev,
133 int init);
134
135
136static struct i2c_driver smsc47m1_driver = {
137 .owner = THIS_MODULE,
138 .name = "smsc47m1",
139 .id = I2C_DRIVERID_SMSC47M1,
140 .flags = I2C_DF_NOTIFY,
141 .attach_adapter = smsc47m1_attach_adapter,
142 .detach_client = smsc47m1_detach_client,
143};
144
145/* nr is 0 or 1 in the callback functions below */
146
147static ssize_t get_fan(struct device *dev, char *buf, int nr)
148{
149 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
150 /* This chip (stupidly) stops monitoring fan speed if PWM is
151 enabled and duty cycle is 0%. This is fine if the monitoring
152 and control concern the same fan, but troublesome if they are
153 not (which could as well happen). */
154 int rpm = (data->pwm[nr] & 0x7F) == 0x00 ? 0 :
155 FAN_FROM_REG(data->fan[nr],
156 DIV_FROM_REG(data->fan_div[nr]),
157 data->fan_preload[nr]);
158 return sprintf(buf, "%d\n", rpm);
159}
160
161static ssize_t get_fan_min(struct device *dev, char *buf, int nr)
162{
163 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
164 int rpm = MIN_FROM_REG(data->fan_preload[nr],
165 DIV_FROM_REG(data->fan_div[nr]));
166 return sprintf(buf, "%d\n", rpm);
167}
168
169static ssize_t get_fan_div(struct device *dev, char *buf, int nr)
170{
171 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
172 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
173}
174
175static ssize_t get_pwm(struct device *dev, char *buf, int nr)
176{
177 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
178 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
179}
180
181static ssize_t get_pwm_en(struct device *dev, char *buf, int nr)
182{
183 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
184 return sprintf(buf, "%d\n", PWM_EN_FROM_REG(data->pwm[nr]));
185}
186
187static ssize_t get_alarms(struct device *dev, struct device_attribute *attr, char *buf)
188{
189 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
190 return sprintf(buf, "%d\n", data->alarms);
191}
192
193static ssize_t set_fan_min(struct device *dev, const char *buf,
194 size_t count, int nr)
195{
196 struct i2c_client *client = to_i2c_client(dev);
197 struct smsc47m1_data *data = i2c_get_clientdata(client);
198 long rpmdiv, val = simple_strtol(buf, NULL, 10);
199
200 down(&data->update_lock);
201 rpmdiv = val * DIV_FROM_REG(data->fan_div[nr]);
202
203 if (983040 > 192 * rpmdiv || 2 * rpmdiv > 983040) {
204 up(&data->update_lock);
205 return -EINVAL;
206 }
207
208 data->fan_preload[nr] = 192 - ((983040 + rpmdiv / 2) / rpmdiv);
209 smsc47m1_write_value(client, SMSC47M1_REG_FAN_PRELOAD(nr),
210 data->fan_preload[nr]);
211 up(&data->update_lock);
212
213 return count;
214}
215
216/* Note: we save and restore the fan minimum here, because its value is
217 determined in part by the fan clock divider. This follows the principle
218 of least suprise; the user doesn't expect the fan minimum to change just
219 because the divider changed. */
220static ssize_t set_fan_div(struct device *dev, const char *buf,
221 size_t count, int nr)
222{
223 struct i2c_client *client = to_i2c_client(dev);
224 struct smsc47m1_data *data = i2c_get_clientdata(client);
225
226 long new_div = simple_strtol(buf, NULL, 10), tmp;
227 u8 old_div = DIV_FROM_REG(data->fan_div[nr]);
228
229 if (new_div == old_div) /* No change */
230 return count;
231
232 down(&data->update_lock);
233 switch (new_div) {
234 case 1: data->fan_div[nr] = 0; break;
235 case 2: data->fan_div[nr] = 1; break;
236 case 4: data->fan_div[nr] = 2; break;
237 case 8: data->fan_div[nr] = 3; break;
238 default:
239 up(&data->update_lock);
240 return -EINVAL;
241 }
242
243 tmp = smsc47m1_read_value(client, SMSC47M1_REG_FANDIV) & 0x0F;
244 tmp |= (data->fan_div[0] << 4) | (data->fan_div[1] << 6);
245 smsc47m1_write_value(client, SMSC47M1_REG_FANDIV, tmp);
246
247 /* Preserve fan min */
248 tmp = 192 - (old_div * (192 - data->fan_preload[nr])
249 + new_div / 2) / new_div;
250 data->fan_preload[nr] = SENSORS_LIMIT(tmp, 0, 191);
251 smsc47m1_write_value(client, SMSC47M1_REG_FAN_PRELOAD(nr),
252 data->fan_preload[nr]);
253 up(&data->update_lock);
254
255 return count;
256}
257
258static ssize_t set_pwm(struct device *dev, const char *buf,
259 size_t count, int nr)
260{
261 struct i2c_client *client = to_i2c_client(dev);
262 struct smsc47m1_data *data = i2c_get_clientdata(client);
263
264 long val = simple_strtol(buf, NULL, 10);
265
266 if (val < 0 || val > 255)
267 return -EINVAL;
268
269 down(&data->update_lock);
270 data->pwm[nr] &= 0x81; /* Preserve additional bits */
271 data->pwm[nr] |= PWM_TO_REG(val);
272 smsc47m1_write_value(client, SMSC47M1_REG_PWM(nr),
273 data->pwm[nr]);
274 up(&data->update_lock);
275
276 return count;
277}
278
279static ssize_t set_pwm_en(struct device *dev, const char *buf,
280 size_t count, int nr)
281{
282 struct i2c_client *client = to_i2c_client(dev);
283 struct smsc47m1_data *data = i2c_get_clientdata(client);
284
285 long val = simple_strtol(buf, NULL, 10);
286
287 if (val != 0 && val != 1)
288 return -EINVAL;
289
290 down(&data->update_lock);
291 data->pwm[nr] &= 0xFE; /* preserve the other bits */
292 data->pwm[nr] |= !val;
293 smsc47m1_write_value(client, SMSC47M1_REG_PWM(nr),
294 data->pwm[nr]);
295 up(&data->update_lock);
296
297 return count;
298}
299
300#define fan_present(offset) \
301static ssize_t get_fan##offset (struct device *dev, struct device_attribute *attr, char *buf) \
302{ \
303 return get_fan(dev, buf, offset - 1); \
304} \
305static ssize_t get_fan##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
306{ \
307 return get_fan_min(dev, buf, offset - 1); \
308} \
309static ssize_t set_fan##offset##_min (struct device *dev, struct device_attribute *attr, \
310 const char *buf, size_t count) \
311{ \
312 return set_fan_min(dev, buf, count, offset - 1); \
313} \
314static ssize_t get_fan##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
315{ \
316 return get_fan_div(dev, buf, offset - 1); \
317} \
318static ssize_t set_fan##offset##_div (struct device *dev, struct device_attribute *attr, \
319 const char *buf, size_t count) \
320{ \
321 return set_fan_div(dev, buf, count, offset - 1); \
322} \
323static ssize_t get_pwm##offset (struct device *dev, struct device_attribute *attr, char *buf) \
324{ \
325 return get_pwm(dev, buf, offset - 1); \
326} \
327static ssize_t set_pwm##offset (struct device *dev, struct device_attribute *attr, \
328 const char *buf, size_t count) \
329{ \
330 return set_pwm(dev, buf, count, offset - 1); \
331} \
332static ssize_t get_pwm##offset##_en (struct device *dev, struct device_attribute *attr, char *buf) \
333{ \
334 return get_pwm_en(dev, buf, offset - 1); \
335} \
336static ssize_t set_pwm##offset##_en (struct device *dev, struct device_attribute *attr, \
337 const char *buf, size_t count) \
338{ \
339 return set_pwm_en(dev, buf, count, offset - 1); \
340} \
341static DEVICE_ATTR(fan##offset##_input, S_IRUGO, get_fan##offset, \
342 NULL); \
343static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
344 get_fan##offset##_min, set_fan##offset##_min); \
345static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
346 get_fan##offset##_div, set_fan##offset##_div); \
347static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
348 get_pwm##offset, set_pwm##offset); \
349static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
350 get_pwm##offset##_en, set_pwm##offset##_en);
351
352fan_present(1);
353fan_present(2);
354
355static DEVICE_ATTR(alarms, S_IRUGO, get_alarms, NULL);
356
357static int smsc47m1_attach_adapter(struct i2c_adapter *adapter)
358{
359 if (!(adapter->class & I2C_CLASS_HWMON))
360 return 0;
361 return i2c_detect(adapter, &addr_data, smsc47m1_detect);
362}
363
364static int smsc47m1_find(int *address)
365{
366 u8 val;
367
368 superio_enter();
369 val = superio_inb(SUPERIO_REG_DEVID);
370
371 /*
372 * SMSC LPC47M10x/LPC47M13x (device id 0x59), LPC47M14x (device id
373 * 0x5F) and LPC47B27x (device id 0x51) have fan control.
374 * The LPC47M15x and LPC47M192 chips "with hardware monitoring block"
375 * can do much more besides (device id 0x60).
376 */
377 if (val == 0x51)
378 printk(KERN_INFO "smsc47m1: Found SMSC LPC47B27x\n");
379 else if (val == 0x59)
380 printk(KERN_INFO "smsc47m1: Found SMSC LPC47M10x/LPC47M13x\n");
381 else if (val == 0x5F)
382 printk(KERN_INFO "smsc47m1: Found SMSC LPC47M14x\n");
383 else if (val == 0x60)
384 printk(KERN_INFO "smsc47m1: Found SMSC LPC47M15x/LPC47M192\n");
385 else {
386 superio_exit();
387 return -ENODEV;
388 }
389
390 superio_select();
391 *address = (superio_inb(SUPERIO_REG_BASE) << 8)
392 | superio_inb(SUPERIO_REG_BASE + 1);
393 val = superio_inb(SUPERIO_REG_ACT);
394 if (*address == 0 || (val & 0x01) == 0) {
395 printk(KERN_INFO "smsc47m1: Device is disabled, will not use\n");
396 superio_exit();
397 return -ENODEV;
398 }
399
400 superio_exit();
401 return 0;
402}
403
404static int smsc47m1_detect(struct i2c_adapter *adapter, int address, int kind)
405{
406 struct i2c_client *new_client;
407 struct smsc47m1_data *data;
408 int err = 0;
409 int fan1, fan2, pwm1, pwm2;
410
411 if (!i2c_is_isa_adapter(adapter)) {
412 return 0;
413 }
414
415 if (!request_region(address, SMSC_EXTENT, smsc47m1_driver.name)) {
416 dev_err(&adapter->dev, "Region 0x%x already in use!\n", address);
417 return -EBUSY;
418 }
419
420 if (!(data = kmalloc(sizeof(struct smsc47m1_data), GFP_KERNEL))) {
421 err = -ENOMEM;
422 goto error_release;
423 }
424 memset(data, 0x00, sizeof(struct smsc47m1_data));
425
426 new_client = &data->client;
427 i2c_set_clientdata(new_client, data);
428 new_client->addr = address;
429 init_MUTEX(&data->lock);
430 new_client->adapter = adapter;
431 new_client->driver = &smsc47m1_driver;
432 new_client->flags = 0;
433
434 strlcpy(new_client->name, "smsc47m1", I2C_NAME_SIZE);
435 init_MUTEX(&data->update_lock);
436
437 /* If no function is properly configured, there's no point in
438 actually registering the chip. */
439 fan1 = (smsc47m1_read_value(new_client, SMSC47M1_REG_TPIN(0)) & 0x05)
440 == 0x05;
441 fan2 = (smsc47m1_read_value(new_client, SMSC47M1_REG_TPIN(1)) & 0x05)
442 == 0x05;
443 pwm1 = (smsc47m1_read_value(new_client, SMSC47M1_REG_PPIN(0)) & 0x05)
444 == 0x04;
445 pwm2 = (smsc47m1_read_value(new_client, SMSC47M1_REG_PPIN(1)) & 0x05)
446 == 0x04;
447 if (!(fan1 || fan2 || pwm1 || pwm2)) {
448 dev_warn(&new_client->dev, "Device is not configured, will not use\n");
449 err = -ENODEV;
450 goto error_free;
451 }
452
453 if ((err = i2c_attach_client(new_client)))
454 goto error_free;
455
456 /* Some values (fan min, clock dividers, pwm registers) may be
457 needed before any update is triggered, so we better read them
458 at least once here. We don't usually do it that way, but in
459 this particular case, manually reading 5 registers out of 8
460 doesn't make much sense and we're better using the existing
461 function. */
462 smsc47m1_update_device(&new_client->dev, 1);
463
464 if (fan1) {
465 device_create_file(&new_client->dev, &dev_attr_fan1_input);
466 device_create_file(&new_client->dev, &dev_attr_fan1_min);
467 device_create_file(&new_client->dev, &dev_attr_fan1_div);
468 } else
469 dev_dbg(&new_client->dev, "Fan 1 not enabled by hardware, "
470 "skipping\n");
471
472 if (fan2) {
473 device_create_file(&new_client->dev, &dev_attr_fan2_input);
474 device_create_file(&new_client->dev, &dev_attr_fan2_min);
475 device_create_file(&new_client->dev, &dev_attr_fan2_div);
476 } else
477 dev_dbg(&new_client->dev, "Fan 2 not enabled by hardware, "
478 "skipping\n");
479
480 if (pwm1) {
481 device_create_file(&new_client->dev, &dev_attr_pwm1);
482 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
483 } else
484 dev_dbg(&new_client->dev, "PWM 1 not enabled by hardware, "
485 "skipping\n");
486 if (pwm2) {
487 device_create_file(&new_client->dev, &dev_attr_pwm2);
488 device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
489 } else
490 dev_dbg(&new_client->dev, "PWM 2 not enabled by hardware, "
491 "skipping\n");
492
493 device_create_file(&new_client->dev, &dev_attr_alarms);
494
495 return 0;
496
497error_free:
498 kfree(new_client);
499error_release:
500 release_region(address, SMSC_EXTENT);
501 return err;
502}
503
504static int smsc47m1_detach_client(struct i2c_client *client)
505{
506 int err;
507
508 if ((err = i2c_detach_client(client))) {
509 dev_err(&client->dev, "Client deregistration failed, "
510 "client not detached.\n");
511 return err;
512 }
513
514 release_region(client->addr, SMSC_EXTENT);
515 kfree(i2c_get_clientdata(client));
516
517 return 0;
518}
519
520static int smsc47m1_read_value(struct i2c_client *client, u8 reg)
521{
522 int res;
523
524 down(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
525 res = inb_p(client->addr + reg);
526 up(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
527 return res;
528}
529
530static void smsc47m1_write_value(struct i2c_client *client, u8 reg, u8 value)
531{
532 down(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
533 outb_p(value, client->addr + reg);
534 up(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
535}
536
537static struct smsc47m1_data *smsc47m1_update_device(struct device *dev,
538 int init)
539{
540 struct i2c_client *client = to_i2c_client(dev);
541 struct smsc47m1_data *data = i2c_get_clientdata(client);
542
543 down(&data->update_lock);
544
545 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) || init) {
546 int i;
547
548 for (i = 0; i < 2; i++) {
549 data->fan[i] = smsc47m1_read_value(client,
550 SMSC47M1_REG_FAN(i));
551 data->fan_preload[i] = smsc47m1_read_value(client,
552 SMSC47M1_REG_FAN_PRELOAD(i));
553 data->pwm[i] = smsc47m1_read_value(client,
554 SMSC47M1_REG_PWM(i));
555 }
556
557 i = smsc47m1_read_value(client, SMSC47M1_REG_FANDIV);
558 data->fan_div[0] = (i >> 4) & 0x03;
559 data->fan_div[1] = i >> 6;
560
561 data->alarms = smsc47m1_read_value(client,
562 SMSC47M1_REG_ALARM) >> 6;
563 /* Clear alarms if needed */
564 if (data->alarms)
565 smsc47m1_write_value(client, SMSC47M1_REG_ALARM, 0xC0);
566
567 data->last_updated = jiffies;
568 }
569
570 up(&data->update_lock);
571 return data;
572}
573
574static int __init sm_smsc47m1_init(void)
575{
576 if (smsc47m1_find(normal_isa)) {
577 return -ENODEV;
578 }
579
580 return i2c_add_driver(&smsc47m1_driver);
581}
582
583static void __exit sm_smsc47m1_exit(void)
584{
585 i2c_del_driver(&smsc47m1_driver);
586}
587
588MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>");
589MODULE_DESCRIPTION("SMSC LPC47M1xx fan sensors driver");
590MODULE_LICENSE("GPL");
591
592module_init(sm_smsc47m1_init);
593module_exit(sm_smsc47m1_exit);
diff --git a/drivers/hwmon/via686a.c b/drivers/hwmon/via686a.c
new file mode 100644
index 000000000000..164d47948390
--- /dev/null
+++ b/drivers/hwmon/via686a.c
@@ -0,0 +1,875 @@
1/*
2 via686a.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
4
5 Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl>,
6 Kyösti Mälkki <kmalkki@cc.hut.fi>,
7 Mark Studebaker <mdsxyz123@yahoo.com>,
8 and Bob Dougherty <bobd@stanford.edu>
9 (Some conversion-factor data were contributed by Jonathan Teh Soon Yew
10 <j.teh@iname.com> and Alex van Kaam <darkside@chello.nl>.)
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25*/
26
27/*
28 Supports the Via VT82C686A, VT82C686B south bridges.
29 Reports all as a 686A.
30 Warning - only supports a single device.
31*/
32
33#include <linux/module.h>
34#include <linux/slab.h>
35#include <linux/pci.h>
36#include <linux/jiffies.h>
37#include <linux/i2c.h>
38#include <linux/i2c-sensor.h>
39#include <linux/init.h>
40#include <asm/io.h>
41
42
43/* If force_addr is set to anything different from 0, we forcibly enable
44 the device at the given address. */
45static unsigned short force_addr = 0;
46module_param(force_addr, ushort, 0);
47MODULE_PARM_DESC(force_addr,
48 "Initialize the base address of the sensors");
49
50/* Addresses to scan.
51 Note that we can't determine the ISA address until we have initialized
52 our module */
53static unsigned short normal_i2c[] = { I2C_CLIENT_END };
54static unsigned int normal_isa[] = { 0x0000, I2C_CLIENT_ISA_END };
55
56/* Insmod parameters */
57SENSORS_INSMOD_1(via686a);
58
59/*
60 The Via 686a southbridge has a LM78-like chip integrated on the same IC.
61 This driver is a customized copy of lm78.c
62*/
63
64/* Many VIA686A constants specified below */
65
66/* Length of ISA address segment */
67#define VIA686A_EXTENT 0x80
68#define VIA686A_BASE_REG 0x70
69#define VIA686A_ENABLE_REG 0x74
70
71/* The VIA686A registers */
72/* ins numbered 0-4 */
73#define VIA686A_REG_IN_MAX(nr) (0x2b + ((nr) * 2))
74#define VIA686A_REG_IN_MIN(nr) (0x2c + ((nr) * 2))
75#define VIA686A_REG_IN(nr) (0x22 + (nr))
76
77/* fans numbered 1-2 */
78#define VIA686A_REG_FAN_MIN(nr) (0x3a + (nr))
79#define VIA686A_REG_FAN(nr) (0x28 + (nr))
80
81/* temps numbered 1-3 */
82static const u8 VIA686A_REG_TEMP[] = { 0x20, 0x21, 0x1f };
83static const u8 VIA686A_REG_TEMP_OVER[] = { 0x39, 0x3d, 0x1d };
84static const u8 VIA686A_REG_TEMP_HYST[] = { 0x3a, 0x3e, 0x1e };
85/* bits 7-6 */
86#define VIA686A_REG_TEMP_LOW1 0x4b
87/* 2 = bits 5-4, 3 = bits 7-6 */
88#define VIA686A_REG_TEMP_LOW23 0x49
89
90#define VIA686A_REG_ALARM1 0x41
91#define VIA686A_REG_ALARM2 0x42
92#define VIA686A_REG_FANDIV 0x47
93#define VIA686A_REG_CONFIG 0x40
94/* The following register sets temp interrupt mode (bits 1-0 for temp1,
95 3-2 for temp2, 5-4 for temp3). Modes are:
96 00 interrupt stays as long as value is out-of-range
97 01 interrupt is cleared once register is read (default)
98 10 comparator mode- like 00, but ignores hysteresis
99 11 same as 00 */
100#define VIA686A_REG_TEMP_MODE 0x4b
101/* We'll just assume that you want to set all 3 simultaneously: */
102#define VIA686A_TEMP_MODE_MASK 0x3F
103#define VIA686A_TEMP_MODE_CONTINUOUS 0x00
104
105/* Conversions. Limit checking is only done on the TO_REG
106 variants.
107
108********* VOLTAGE CONVERSIONS (Bob Dougherty) ********
109 From HWMon.cpp (Copyright 1998-2000 Jonathan Teh Soon Yew):
110 voltagefactor[0]=1.25/2628; (2628/1.25=2102.4) // Vccp
111 voltagefactor[1]=1.25/2628; (2628/1.25=2102.4) // +2.5V
112 voltagefactor[2]=1.67/2628; (2628/1.67=1573.7) // +3.3V
113 voltagefactor[3]=2.6/2628; (2628/2.60=1010.8) // +5V
114 voltagefactor[4]=6.3/2628; (2628/6.30=417.14) // +12V
115 in[i]=(data[i+2]*25.0+133)*voltagefactor[i];
116 That is:
117 volts = (25*regVal+133)*factor
118 regVal = (volts/factor-133)/25
119 (These conversions were contributed by Jonathan Teh Soon Yew
120 <j.teh@iname.com>) */
121static inline u8 IN_TO_REG(long val, int inNum)
122{
123 /* To avoid floating point, we multiply constants by 10 (100 for +12V).
124 Rounding is done (120500 is actually 133000 - 12500).
125 Remember that val is expressed in 0.001V/bit, which is why we divide
126 by an additional 10000 (100000 for +12V): 1000 for val and 10 (100)
127 for the constants. */
128 if (inNum <= 1)
129 return (u8)
130 SENSORS_LIMIT((val * 21024 - 1205000) / 250000, 0, 255);
131 else if (inNum == 2)
132 return (u8)
133 SENSORS_LIMIT((val * 15737 - 1205000) / 250000, 0, 255);
134 else if (inNum == 3)
135 return (u8)
136 SENSORS_LIMIT((val * 10108 - 1205000) / 250000, 0, 255);
137 else
138 return (u8)
139 SENSORS_LIMIT((val * 41714 - 12050000) / 2500000, 0, 255);
140}
141
142static inline long IN_FROM_REG(u8 val, int inNum)
143{
144 /* To avoid floating point, we multiply constants by 10 (100 for +12V).
145 We also multiply them by 1000 because we want 0.001V/bit for the
146 output value. Rounding is done. */
147 if (inNum <= 1)
148 return (long) ((250000 * val + 1330000 + 21024 / 2) / 21024);
149 else if (inNum == 2)
150 return (long) ((250000 * val + 1330000 + 15737 / 2) / 15737);
151 else if (inNum == 3)
152 return (long) ((250000 * val + 1330000 + 10108 / 2) / 10108);
153 else
154 return (long) ((2500000 * val + 13300000 + 41714 / 2) / 41714);
155}
156
157/********* FAN RPM CONVERSIONS ********/
158/* Higher register values = slower fans (the fan's strobe gates a counter).
159 But this chip saturates back at 0, not at 255 like all the other chips.
160 So, 0 means 0 RPM */
161static inline u8 FAN_TO_REG(long rpm, int div)
162{
163 if (rpm == 0)
164 return 0;
165 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
166 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 255);
167}
168
169#define FAN_FROM_REG(val,div) ((val)==0?0:(val)==255?0:1350000/((val)*(div)))
170
171/******** TEMP CONVERSIONS (Bob Dougherty) *********/
172/* linear fits from HWMon.cpp (Copyright 1998-2000 Jonathan Teh Soon Yew)
173 if(temp<169)
174 return double(temp)*0.427-32.08;
175 else if(temp>=169 && temp<=202)
176 return double(temp)*0.582-58.16;
177 else
178 return double(temp)*0.924-127.33;
179
180 A fifth-order polynomial fits the unofficial data (provided by Alex van
181 Kaam <darkside@chello.nl>) a bit better. It also give more reasonable
182 numbers on my machine (ie. they agree with what my BIOS tells me).
183 Here's the fifth-order fit to the 8-bit data:
184 temp = 1.625093e-10*val^5 - 1.001632e-07*val^4 + 2.457653e-05*val^3 -
185 2.967619e-03*val^2 + 2.175144e-01*val - 7.090067e+0.
186
187 (2000-10-25- RFD: thanks to Uwe Andersen <uandersen@mayah.com> for
188 finding my typos in this formula!)
189
190 Alas, none of the elegant function-fit solutions will work because we
191 aren't allowed to use floating point in the kernel and doing it with
192 integers doesn't provide enough precision. So we'll do boring old
193 look-up table stuff. The unofficial data (see below) have effectively
194 7-bit resolution (they are rounded to the nearest degree). I'm assuming
195 that the transfer function of the device is monotonic and smooth, so a
196 smooth function fit to the data will allow us to get better precision.
197 I used the 5th-order poly fit described above and solved for
198 VIA register values 0-255. I *10 before rounding, so we get tenth-degree
199 precision. (I could have done all 1024 values for our 10-bit readings,
200 but the function is very linear in the useful range (0-80 deg C), so
201 we'll just use linear interpolation for 10-bit readings.) So, tempLUT
202 is the temp at via register values 0-255: */
203static const long tempLUT[] =
204{ -709, -688, -667, -646, -627, -607, -589, -570, -553, -536, -519,
205 -503, -487, -471, -456, -442, -428, -414, -400, -387, -375,
206 -362, -350, -339, -327, -316, -305, -295, -285, -275, -265,
207 -255, -246, -237, -229, -220, -212, -204, -196, -188, -180,
208 -173, -166, -159, -152, -145, -139, -132, -126, -120, -114,
209 -108, -102, -96, -91, -85, -80, -74, -69, -64, -59, -54, -49,
210 -44, -39, -34, -29, -25, -20, -15, -11, -6, -2, 3, 7, 12, 16,
211 20, 25, 29, 33, 37, 42, 46, 50, 54, 59, 63, 67, 71, 75, 79, 84,
212 88, 92, 96, 100, 104, 109, 113, 117, 121, 125, 130, 134, 138,
213 142, 146, 151, 155, 159, 163, 168, 172, 176, 181, 185, 189,
214 193, 198, 202, 206, 211, 215, 219, 224, 228, 232, 237, 241,
215 245, 250, 254, 259, 263, 267, 272, 276, 281, 285, 290, 294,
216 299, 303, 307, 312, 316, 321, 325, 330, 334, 339, 344, 348,
217 353, 357, 362, 366, 371, 376, 380, 385, 390, 395, 399, 404,
218 409, 414, 419, 423, 428, 433, 438, 443, 449, 454, 459, 464,
219 469, 475, 480, 486, 491, 497, 502, 508, 514, 520, 526, 532,
220 538, 544, 551, 557, 564, 571, 578, 584, 592, 599, 606, 614,
221 621, 629, 637, 645, 654, 662, 671, 680, 689, 698, 708, 718,
222 728, 738, 749, 759, 770, 782, 793, 805, 818, 830, 843, 856,
223 870, 883, 898, 912, 927, 943, 958, 975, 991, 1008, 1026, 1044,
224 1062, 1081, 1101, 1121, 1141, 1162, 1184, 1206, 1229, 1252,
225 1276, 1301, 1326, 1352, 1378, 1406, 1434, 1462
226};
227
228/* the original LUT values from Alex van Kaam <darkside@chello.nl>
229 (for via register values 12-240):
230{-50,-49,-47,-45,-43,-41,-39,-38,-37,-35,-34,-33,-32,-31,
231-30,-29,-28,-27,-26,-25,-24,-24,-23,-22,-21,-20,-20,-19,-18,-17,-17,-16,-15,
232-15,-14,-14,-13,-12,-12,-11,-11,-10,-9,-9,-8,-8,-7,-7,-6,-6,-5,-5,-4,-4,-3,
233-3,-2,-2,-1,-1,0,0,1,1,1,3,3,3,4,4,4,5,5,5,6,6,7,7,8,8,9,9,9,10,10,11,11,12,
23412,12,13,13,13,14,14,15,15,16,16,16,17,17,18,18,19,19,20,20,21,21,21,22,22,
23522,23,23,24,24,25,25,26,26,26,27,27,27,28,28,29,29,30,30,30,31,31,32,32,33,
23633,34,34,35,35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,43,43,44,44,45,
23745,46,46,47,48,48,49,49,50,51,51,52,52,53,53,54,55,55,56,57,57,58,59,59,60,
23861,62,62,63,64,65,66,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,83,84,
23985,86,88,89,91,92,94,96,97,99,101,103,105,107,109,110};
240
241
242 Here's the reverse LUT. I got it by doing a 6-th order poly fit (needed
243 an extra term for a good fit to these inverse data!) and then
244 solving for each temp value from -50 to 110 (the useable range for
245 this chip). Here's the fit:
246 viaRegVal = -1.160370e-10*val^6 +3.193693e-08*val^5 - 1.464447e-06*val^4
247 - 2.525453e-04*val^3 + 1.424593e-02*val^2 + 2.148941e+00*val +7.275808e+01)
248 Note that n=161: */
249static const u8 viaLUT[] =
250{ 12, 12, 13, 14, 14, 15, 16, 16, 17, 18, 18, 19, 20, 20, 21, 22, 23,
251 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 39, 40,
252 41, 43, 45, 46, 48, 49, 51, 53, 55, 57, 59, 60, 62, 64, 66,
253 69, 71, 73, 75, 77, 79, 82, 84, 86, 88, 91, 93, 95, 98, 100,
254 103, 105, 107, 110, 112, 115, 117, 119, 122, 124, 126, 129,
255 131, 134, 136, 138, 140, 143, 145, 147, 150, 152, 154, 156,
256 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180,
257 182, 183, 185, 187, 188, 190, 192, 193, 195, 196, 198, 199,
258 200, 202, 203, 205, 206, 207, 208, 209, 210, 211, 212, 213,
259 214, 215, 216, 217, 218, 219, 220, 221, 222, 222, 223, 224,
260 225, 226, 226, 227, 228, 228, 229, 230, 230, 231, 232, 232,
261 233, 233, 234, 235, 235, 236, 236, 237, 237, 238, 238, 239,
262 239, 240
263};
264
265/* Converting temps to (8-bit) hyst and over registers
266 No interpolation here.
267 The +50 is because the temps start at -50 */
268static inline u8 TEMP_TO_REG(long val)
269{
270 return viaLUT[val <= -50000 ? 0 : val >= 110000 ? 160 :
271 (val < 0 ? val - 500 : val + 500) / 1000 + 50];
272}
273
274/* for 8-bit temperature hyst and over registers */
275#define TEMP_FROM_REG(val) (tempLUT[(val)] * 100)
276
277/* for 10-bit temperature readings */
278static inline long TEMP_FROM_REG10(u16 val)
279{
280 u16 eightBits = val >> 2;
281 u16 twoBits = val & 3;
282
283 /* no interpolation for these */
284 if (twoBits == 0 || eightBits == 255)
285 return TEMP_FROM_REG(eightBits);
286
287 /* do some linear interpolation */
288 return (tempLUT[eightBits] * (4 - twoBits) +
289 tempLUT[eightBits + 1] * twoBits) * 25;
290}
291
292#define DIV_FROM_REG(val) (1 << (val))
293#define DIV_TO_REG(val) ((val)==8?3:(val)==4?2:(val)==1?0:1)
294
295/* For the VIA686A, we need to keep some data in memory.
296 The structure is dynamically allocated, at the same time when a new
297 via686a client is allocated. */
298struct via686a_data {
299 struct i2c_client client;
300 struct semaphore update_lock;
301 char valid; /* !=0 if following fields are valid */
302 unsigned long last_updated; /* In jiffies */
303
304 u8 in[5]; /* Register value */
305 u8 in_max[5]; /* Register value */
306 u8 in_min[5]; /* Register value */
307 u8 fan[2]; /* Register value */
308 u8 fan_min[2]; /* Register value */
309 u16 temp[3]; /* Register value 10 bit */
310 u8 temp_over[3]; /* Register value */
311 u8 temp_hyst[3]; /* Register value */
312 u8 fan_div[2]; /* Register encoding, shifted right */
313 u16 alarms; /* Register encoding, combined */
314};
315
316static struct pci_dev *s_bridge; /* pointer to the (only) via686a */
317
318static int via686a_attach_adapter(struct i2c_adapter *adapter);
319static int via686a_detect(struct i2c_adapter *adapter, int address, int kind);
320static int via686a_detach_client(struct i2c_client *client);
321
322static inline int via686a_read_value(struct i2c_client *client, u8 reg)
323{
324 return (inb_p(client->addr + reg));
325}
326
327static inline void via686a_write_value(struct i2c_client *client, u8 reg,
328 u8 value)
329{
330 outb_p(value, client->addr + reg);
331}
332
333static struct via686a_data *via686a_update_device(struct device *dev);
334static void via686a_init_client(struct i2c_client *client);
335
336/* following are the sysfs callback functions */
337
338/* 7 voltage sensors */
339static ssize_t show_in(struct device *dev, char *buf, int nr) {
340 struct via686a_data *data = via686a_update_device(dev);
341 return sprintf(buf, "%ld\n", IN_FROM_REG(data->in[nr], nr));
342}
343
344static ssize_t show_in_min(struct device *dev, char *buf, int nr) {
345 struct via686a_data *data = via686a_update_device(dev);
346 return sprintf(buf, "%ld\n", IN_FROM_REG(data->in_min[nr], nr));
347}
348
349static ssize_t show_in_max(struct device *dev, char *buf, int nr) {
350 struct via686a_data *data = via686a_update_device(dev);
351 return sprintf(buf, "%ld\n", IN_FROM_REG(data->in_max[nr], nr));
352}
353
354static ssize_t set_in_min(struct device *dev, const char *buf,
355 size_t count, int nr) {
356 struct i2c_client *client = to_i2c_client(dev);
357 struct via686a_data *data = i2c_get_clientdata(client);
358 unsigned long val = simple_strtoul(buf, NULL, 10);
359
360 down(&data->update_lock);
361 data->in_min[nr] = IN_TO_REG(val, nr);
362 via686a_write_value(client, VIA686A_REG_IN_MIN(nr),
363 data->in_min[nr]);
364 up(&data->update_lock);
365 return count;
366}
367static ssize_t set_in_max(struct device *dev, const char *buf,
368 size_t count, int nr) {
369 struct i2c_client *client = to_i2c_client(dev);
370 struct via686a_data *data = i2c_get_clientdata(client);
371 unsigned long val = simple_strtoul(buf, NULL, 10);
372
373 down(&data->update_lock);
374 data->in_max[nr] = IN_TO_REG(val, nr);
375 via686a_write_value(client, VIA686A_REG_IN_MAX(nr),
376 data->in_max[nr]);
377 up(&data->update_lock);
378 return count;
379}
380#define show_in_offset(offset) \
381static ssize_t \
382 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
383{ \
384 return show_in(dev, buf, offset); \
385} \
386static ssize_t \
387 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
388{ \
389 return show_in_min(dev, buf, offset); \
390} \
391static ssize_t \
392 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
393{ \
394 return show_in_max(dev, buf, offset); \
395} \
396static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
397 const char *buf, size_t count) \
398{ \
399 return set_in_min(dev, buf, count, offset); \
400} \
401static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
402 const char *buf, size_t count) \
403{ \
404 return set_in_max(dev, buf, count, offset); \
405} \
406static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL);\
407static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
408 show_in##offset##_min, set_in##offset##_min); \
409static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
410 show_in##offset##_max, set_in##offset##_max);
411
412show_in_offset(0);
413show_in_offset(1);
414show_in_offset(2);
415show_in_offset(3);
416show_in_offset(4);
417
418/* 3 temperatures */
419static ssize_t show_temp(struct device *dev, char *buf, int nr) {
420 struct via686a_data *data = via686a_update_device(dev);
421 return sprintf(buf, "%ld\n", TEMP_FROM_REG10(data->temp[nr]));
422}
423static ssize_t show_temp_over(struct device *dev, char *buf, int nr) {
424 struct via686a_data *data = via686a_update_device(dev);
425 return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp_over[nr]));
426}
427static ssize_t show_temp_hyst(struct device *dev, char *buf, int nr) {
428 struct via686a_data *data = via686a_update_device(dev);
429 return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp_hyst[nr]));
430}
431static ssize_t set_temp_over(struct device *dev, const char *buf,
432 size_t count, int nr) {
433 struct i2c_client *client = to_i2c_client(dev);
434 struct via686a_data *data = i2c_get_clientdata(client);
435 int val = simple_strtol(buf, NULL, 10);
436
437 down(&data->update_lock);
438 data->temp_over[nr] = TEMP_TO_REG(val);
439 via686a_write_value(client, VIA686A_REG_TEMP_OVER[nr],
440 data->temp_over[nr]);
441 up(&data->update_lock);
442 return count;
443}
444static ssize_t set_temp_hyst(struct device *dev, const char *buf,
445 size_t count, int nr) {
446 struct i2c_client *client = to_i2c_client(dev);
447 struct via686a_data *data = i2c_get_clientdata(client);
448 int val = simple_strtol(buf, NULL, 10);
449
450 down(&data->update_lock);
451 data->temp_hyst[nr] = TEMP_TO_REG(val);
452 via686a_write_value(client, VIA686A_REG_TEMP_HYST[nr],
453 data->temp_hyst[nr]);
454 up(&data->update_lock);
455 return count;
456}
457#define show_temp_offset(offset) \
458static ssize_t show_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
459{ \
460 return show_temp(dev, buf, offset - 1); \
461} \
462static ssize_t \
463show_temp_##offset##_over (struct device *dev, struct device_attribute *attr, char *buf) \
464{ \
465 return show_temp_over(dev, buf, offset - 1); \
466} \
467static ssize_t \
468show_temp_##offset##_hyst (struct device *dev, struct device_attribute *attr, char *buf) \
469{ \
470 return show_temp_hyst(dev, buf, offset - 1); \
471} \
472static ssize_t set_temp_##offset##_over (struct device *dev, struct device_attribute *attr, \
473 const char *buf, size_t count) \
474{ \
475 return set_temp_over(dev, buf, count, offset - 1); \
476} \
477static ssize_t set_temp_##offset##_hyst (struct device *dev, struct device_attribute *attr, \
478 const char *buf, size_t count) \
479{ \
480 return set_temp_hyst(dev, buf, count, offset - 1); \
481} \
482static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, NULL);\
483static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
484 show_temp_##offset##_over, set_temp_##offset##_over); \
485static DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
486 show_temp_##offset##_hyst, set_temp_##offset##_hyst);
487
488show_temp_offset(1);
489show_temp_offset(2);
490show_temp_offset(3);
491
492/* 2 Fans */
493static ssize_t show_fan(struct device *dev, char *buf, int nr) {
494 struct via686a_data *data = via686a_update_device(dev);
495 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
496 DIV_FROM_REG(data->fan_div[nr])) );
497}
498static ssize_t show_fan_min(struct device *dev, char *buf, int nr) {
499 struct via686a_data *data = via686a_update_device(dev);
500 return sprintf(buf, "%d\n",
501 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])) );
502}
503static ssize_t show_fan_div(struct device *dev, char *buf, int nr) {
504 struct via686a_data *data = via686a_update_device(dev);
505 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
506}
507static ssize_t set_fan_min(struct device *dev, const char *buf,
508 size_t count, int nr) {
509 struct i2c_client *client = to_i2c_client(dev);
510 struct via686a_data *data = i2c_get_clientdata(client);
511 int val = simple_strtol(buf, NULL, 10);
512
513 down(&data->update_lock);
514 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
515 via686a_write_value(client, VIA686A_REG_FAN_MIN(nr+1), data->fan_min[nr]);
516 up(&data->update_lock);
517 return count;
518}
519static ssize_t set_fan_div(struct device *dev, const char *buf,
520 size_t count, int nr) {
521 struct i2c_client *client = to_i2c_client(dev);
522 struct via686a_data *data = i2c_get_clientdata(client);
523 int val = simple_strtol(buf, NULL, 10);
524 int old;
525
526 down(&data->update_lock);
527 old = via686a_read_value(client, VIA686A_REG_FANDIV);
528 data->fan_div[nr] = DIV_TO_REG(val);
529 old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
530 via686a_write_value(client, VIA686A_REG_FANDIV, old);
531 up(&data->update_lock);
532 return count;
533}
534
535#define show_fan_offset(offset) \
536static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
537{ \
538 return show_fan(dev, buf, offset - 1); \
539} \
540static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
541{ \
542 return show_fan_min(dev, buf, offset - 1); \
543} \
544static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
545{ \
546 return show_fan_div(dev, buf, offset - 1); \
547} \
548static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
549 const char *buf, size_t count) \
550{ \
551 return set_fan_min(dev, buf, count, offset - 1); \
552} \
553static ssize_t set_fan_##offset##_div (struct device *dev, struct device_attribute *attr, \
554 const char *buf, size_t count) \
555{ \
556 return set_fan_div(dev, buf, count, offset - 1); \
557} \
558static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
559static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
560 show_fan_##offset##_min, set_fan_##offset##_min); \
561static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
562 show_fan_##offset##_div, set_fan_##offset##_div);
563
564show_fan_offset(1);
565show_fan_offset(2);
566
567/* Alarms */
568static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf) {
569 struct via686a_data *data = via686a_update_device(dev);
570 return sprintf(buf, "%u\n", data->alarms);
571}
572static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
573
574/* The driver. I choose to use type i2c_driver, as at is identical to both
575 smbus_driver and isa_driver, and clients could be of either kind */
576static struct i2c_driver via686a_driver = {
577 .owner = THIS_MODULE,
578 .name = "via686a",
579 .id = I2C_DRIVERID_VIA686A,
580 .flags = I2C_DF_NOTIFY,
581 .attach_adapter = via686a_attach_adapter,
582 .detach_client = via686a_detach_client,
583};
584
585
586/* This is called when the module is loaded */
587static int via686a_attach_adapter(struct i2c_adapter *adapter)
588{
589 if (!(adapter->class & I2C_CLASS_HWMON))
590 return 0;
591 return i2c_detect(adapter, &addr_data, via686a_detect);
592}
593
594static int via686a_detect(struct i2c_adapter *adapter, int address, int kind)
595{
596 struct i2c_client *new_client;
597 struct via686a_data *data;
598 int err = 0;
599 const char client_name[] = "via686a";
600 u16 val;
601
602 /* Make sure we are probing the ISA bus!! */
603 if (!i2c_is_isa_adapter(adapter)) {
604 dev_err(&adapter->dev,
605 "via686a_detect called for an I2C bus adapter?!?\n");
606 return 0;
607 }
608
609 /* 8231 requires multiple of 256, we enforce that on 686 as well */
610 if (force_addr)
611 address = force_addr & 0xFF00;
612
613 if (force_addr) {
614 dev_warn(&adapter->dev, "forcing ISA address 0x%04X\n",
615 address);
616 if (PCIBIOS_SUCCESSFUL !=
617 pci_write_config_word(s_bridge, VIA686A_BASE_REG, address))
618 return -ENODEV;
619 }
620 if (PCIBIOS_SUCCESSFUL !=
621 pci_read_config_word(s_bridge, VIA686A_ENABLE_REG, &val))
622 return -ENODEV;
623 if (!(val & 0x0001)) {
624 dev_warn(&adapter->dev, "enabling sensors\n");
625 if (PCIBIOS_SUCCESSFUL !=
626 pci_write_config_word(s_bridge, VIA686A_ENABLE_REG,
627 val | 0x0001))
628 return -ENODEV;
629 }
630
631 /* Reserve the ISA region */
632 if (!request_region(address, VIA686A_EXTENT, via686a_driver.name)) {
633 dev_err(&adapter->dev, "region 0x%x already in use!\n",
634 address);
635 return -ENODEV;
636 }
637
638 if (!(data = kmalloc(sizeof(struct via686a_data), GFP_KERNEL))) {
639 err = -ENOMEM;
640 goto ERROR0;
641 }
642 memset(data, 0, sizeof(struct via686a_data));
643
644 new_client = &data->client;
645 i2c_set_clientdata(new_client, data);
646 new_client->addr = address;
647 new_client->adapter = adapter;
648 new_client->driver = &via686a_driver;
649 new_client->flags = 0;
650
651 /* Fill in the remaining client fields and put into the global list */
652 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
653
654 data->valid = 0;
655 init_MUTEX(&data->update_lock);
656 /* Tell the I2C layer a new client has arrived */
657 if ((err = i2c_attach_client(new_client)))
658 goto ERROR3;
659
660 /* Initialize the VIA686A chip */
661 via686a_init_client(new_client);
662
663 /* Register sysfs hooks */
664 device_create_file(&new_client->dev, &dev_attr_in0_input);
665 device_create_file(&new_client->dev, &dev_attr_in1_input);
666 device_create_file(&new_client->dev, &dev_attr_in2_input);
667 device_create_file(&new_client->dev, &dev_attr_in3_input);
668 device_create_file(&new_client->dev, &dev_attr_in4_input);
669 device_create_file(&new_client->dev, &dev_attr_in0_min);
670 device_create_file(&new_client->dev, &dev_attr_in1_min);
671 device_create_file(&new_client->dev, &dev_attr_in2_min);
672 device_create_file(&new_client->dev, &dev_attr_in3_min);
673 device_create_file(&new_client->dev, &dev_attr_in4_min);
674 device_create_file(&new_client->dev, &dev_attr_in0_max);
675 device_create_file(&new_client->dev, &dev_attr_in1_max);
676 device_create_file(&new_client->dev, &dev_attr_in2_max);
677 device_create_file(&new_client->dev, &dev_attr_in3_max);
678 device_create_file(&new_client->dev, &dev_attr_in4_max);
679 device_create_file(&new_client->dev, &dev_attr_temp1_input);
680 device_create_file(&new_client->dev, &dev_attr_temp2_input);
681 device_create_file(&new_client->dev, &dev_attr_temp3_input);
682 device_create_file(&new_client->dev, &dev_attr_temp1_max);
683 device_create_file(&new_client->dev, &dev_attr_temp2_max);
684 device_create_file(&new_client->dev, &dev_attr_temp3_max);
685 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
686 device_create_file(&new_client->dev, &dev_attr_temp2_max_hyst);
687 device_create_file(&new_client->dev, &dev_attr_temp3_max_hyst);
688 device_create_file(&new_client->dev, &dev_attr_fan1_input);
689 device_create_file(&new_client->dev, &dev_attr_fan2_input);
690 device_create_file(&new_client->dev, &dev_attr_fan1_min);
691 device_create_file(&new_client->dev, &dev_attr_fan2_min);
692 device_create_file(&new_client->dev, &dev_attr_fan1_div);
693 device_create_file(&new_client->dev, &dev_attr_fan2_div);
694 device_create_file(&new_client->dev, &dev_attr_alarms);
695
696 return 0;
697
698ERROR3:
699 kfree(data);
700ERROR0:
701 release_region(address, VIA686A_EXTENT);
702 return err;
703}
704
705static int via686a_detach_client(struct i2c_client *client)
706{
707 int err;
708
709 if ((err = i2c_detach_client(client))) {
710 dev_err(&client->dev,
711 "Client deregistration failed, client not detached.\n");
712 return err;
713 }
714
715 release_region(client->addr, VIA686A_EXTENT);
716 kfree(i2c_get_clientdata(client));
717
718 return 0;
719}
720
721/* Called when we have found a new VIA686A. Set limits, etc. */
722static void via686a_init_client(struct i2c_client *client)
723{
724 u8 reg;
725
726 /* Start monitoring */
727 reg = via686a_read_value(client, VIA686A_REG_CONFIG);
728 via686a_write_value(client, VIA686A_REG_CONFIG, (reg|0x01)&0x7F);
729
730 /* Configure temp interrupt mode for continuous-interrupt operation */
731 via686a_write_value(client, VIA686A_REG_TEMP_MODE,
732 via686a_read_value(client, VIA686A_REG_TEMP_MODE) &
733 !(VIA686A_TEMP_MODE_MASK | VIA686A_TEMP_MODE_CONTINUOUS));
734}
735
736static struct via686a_data *via686a_update_device(struct device *dev)
737{
738 struct i2c_client *client = to_i2c_client(dev);
739 struct via686a_data *data = i2c_get_clientdata(client);
740 int i;
741
742 down(&data->update_lock);
743
744 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
745 || !data->valid) {
746 for (i = 0; i <= 4; i++) {
747 data->in[i] =
748 via686a_read_value(client, VIA686A_REG_IN(i));
749 data->in_min[i] = via686a_read_value(client,
750 VIA686A_REG_IN_MIN
751 (i));
752 data->in_max[i] =
753 via686a_read_value(client, VIA686A_REG_IN_MAX(i));
754 }
755 for (i = 1; i <= 2; i++) {
756 data->fan[i - 1] =
757 via686a_read_value(client, VIA686A_REG_FAN(i));
758 data->fan_min[i - 1] = via686a_read_value(client,
759 VIA686A_REG_FAN_MIN(i));
760 }
761 for (i = 0; i <= 2; i++) {
762 data->temp[i] = via686a_read_value(client,
763 VIA686A_REG_TEMP[i]) << 2;
764 data->temp_over[i] =
765 via686a_read_value(client,
766 VIA686A_REG_TEMP_OVER[i]);
767 data->temp_hyst[i] =
768 via686a_read_value(client,
769 VIA686A_REG_TEMP_HYST[i]);
770 }
771 /* add in lower 2 bits
772 temp1 uses bits 7-6 of VIA686A_REG_TEMP_LOW1
773 temp2 uses bits 5-4 of VIA686A_REG_TEMP_LOW23
774 temp3 uses bits 7-6 of VIA686A_REG_TEMP_LOW23
775 */
776 data->temp[0] |= (via686a_read_value(client,
777 VIA686A_REG_TEMP_LOW1)
778 & 0xc0) >> 6;
779 data->temp[1] |=
780 (via686a_read_value(client, VIA686A_REG_TEMP_LOW23) &
781 0x30) >> 4;
782 data->temp[2] |=
783 (via686a_read_value(client, VIA686A_REG_TEMP_LOW23) &
784 0xc0) >> 6;
785
786 i = via686a_read_value(client, VIA686A_REG_FANDIV);
787 data->fan_div[0] = (i >> 4) & 0x03;
788 data->fan_div[1] = i >> 6;
789 data->alarms =
790 via686a_read_value(client,
791 VIA686A_REG_ALARM1) |
792 (via686a_read_value(client, VIA686A_REG_ALARM2) << 8);
793 data->last_updated = jiffies;
794 data->valid = 1;
795 }
796
797 up(&data->update_lock);
798
799 return data;
800}
801
802static struct pci_device_id via686a_pci_ids[] = {
803 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_4) },
804 { 0, }
805};
806
807MODULE_DEVICE_TABLE(pci, via686a_pci_ids);
808
809static int __devinit via686a_pci_probe(struct pci_dev *dev,
810 const struct pci_device_id *id)
811{
812 u16 val;
813 int addr = 0;
814
815 if (PCIBIOS_SUCCESSFUL !=
816 pci_read_config_word(dev, VIA686A_BASE_REG, &val))
817 return -ENODEV;
818
819 addr = val & ~(VIA686A_EXTENT - 1);
820 if (addr == 0 && force_addr == 0) {
821 dev_err(&dev->dev, "base address not set - upgrade BIOS "
822 "or use force_addr=0xaddr\n");
823 return -ENODEV;
824 }
825 if (force_addr)
826 addr = force_addr; /* so detect will get called */
827
828 if (!addr) {
829 dev_err(&dev->dev, "No Via 686A sensors found.\n");
830 return -ENODEV;
831 }
832 normal_isa[0] = addr;
833
834 s_bridge = pci_dev_get(dev);
835 if (i2c_add_driver(&via686a_driver)) {
836 pci_dev_put(s_bridge);
837 s_bridge = NULL;
838 }
839
840 /* Always return failure here. This is to allow other drivers to bind
841 * to this pci device. We don't really want to have control over the
842 * pci device, we only wanted to read as few register values from it.
843 */
844 return -ENODEV;
845}
846
847static struct pci_driver via686a_pci_driver = {
848 .name = "via686a",
849 .id_table = via686a_pci_ids,
850 .probe = via686a_pci_probe,
851};
852
853static int __init sm_via686a_init(void)
854{
855 return pci_register_driver(&via686a_pci_driver);
856}
857
858static void __exit sm_via686a_exit(void)
859{
860 pci_unregister_driver(&via686a_pci_driver);
861 if (s_bridge != NULL) {
862 i2c_del_driver(&via686a_driver);
863 pci_dev_put(s_bridge);
864 s_bridge = NULL;
865 }
866}
867
868MODULE_AUTHOR("Kyösti Mälkki <kmalkki@cc.hut.fi>, "
869 "Mark Studebaker <mdsxyz123@yahoo.com> "
870 "and Bob Dougherty <bobd@stanford.edu>");
871MODULE_DESCRIPTION("VIA 686A Sensor device");
872MODULE_LICENSE("GPL");
873
874module_init(sm_via686a_init);
875module_exit(sm_via686a_exit);
diff --git a/drivers/hwmon/w83627ehf.c b/drivers/hwmon/w83627ehf.c
new file mode 100644
index 000000000000..8a40b6976e1a
--- /dev/null
+++ b/drivers/hwmon/w83627ehf.c
@@ -0,0 +1,846 @@
1/*
2 w83627ehf - Driver for the hardware monitoring functionality of
3 the Winbond W83627EHF Super-I/O chip
4 Copyright (C) 2005 Jean Delvare <khali@linux-fr.org>
5
6 Shamelessly ripped from the w83627hf driver
7 Copyright (C) 2003 Mark Studebaker
8
9 Thanks to Leon Moonen, Steve Cliffe and Grant Coady for their help
10 in testing and debugging this driver.
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
26
27 Supports the following chips:
28
29 Chip #vin #fan #pwm #temp chip_id man_id
30 w83627ehf - 5 - 3 0x88 0x5ca3
31
32 This is a preliminary version of the driver, only supporting the
33 fan and temperature inputs. The chip does much more than that.
34*/
35
36#include <linux/module.h>
37#include <linux/init.h>
38#include <linux/slab.h>
39#include <linux/i2c.h>
40#include <linux/i2c-sensor.h>
41#include <asm/io.h>
42#include "lm75.h"
43
44/* Addresses to scan
45 The actual ISA address is read from Super-I/O configuration space */
46static unsigned short normal_i2c[] = { I2C_CLIENT_END };
47static unsigned int normal_isa[] = { 0, I2C_CLIENT_ISA_END };
48
49/* Insmod parameters */
50SENSORS_INSMOD_1(w83627ehf);
51
52/*
53 * Super-I/O constants and functions
54 */
55
56static int REG; /* The register to read/write */
57static int VAL; /* The value to read/write */
58
59#define W83627EHF_LD_HWM 0x0b
60
61#define SIO_REG_LDSEL 0x07 /* Logical device select */
62#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
63#define SIO_REG_ENABLE 0x30 /* Logical device enable */
64#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
65
66#define SIO_W83627EHF_ID 0x8840
67#define SIO_ID_MASK 0xFFC0
68
69static inline void
70superio_outb(int reg, int val)
71{
72 outb(reg, REG);
73 outb(val, VAL);
74}
75
76static inline int
77superio_inb(int reg)
78{
79 outb(reg, REG);
80 return inb(VAL);
81}
82
83static inline void
84superio_select(int ld)
85{
86 outb(SIO_REG_LDSEL, REG);
87 outb(ld, VAL);
88}
89
90static inline void
91superio_enter(void)
92{
93 outb(0x87, REG);
94 outb(0x87, REG);
95}
96
97static inline void
98superio_exit(void)
99{
100 outb(0x02, REG);
101 outb(0x02, VAL);
102}
103
104/*
105 * ISA constants
106 */
107
108#define REGION_LENGTH 8
109#define ADDR_REG_OFFSET 5
110#define DATA_REG_OFFSET 6
111
112#define W83627EHF_REG_BANK 0x4E
113#define W83627EHF_REG_CONFIG 0x40
114#define W83627EHF_REG_CHIP_ID 0x49
115#define W83627EHF_REG_MAN_ID 0x4F
116
117static const u16 W83627EHF_REG_FAN[] = { 0x28, 0x29, 0x2a, 0x3f, 0x553 };
118static const u16 W83627EHF_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d, 0x3e, 0x55c };
119
120#define W83627EHF_REG_TEMP1 0x27
121#define W83627EHF_REG_TEMP1_HYST 0x3a
122#define W83627EHF_REG_TEMP1_OVER 0x39
123static const u16 W83627EHF_REG_TEMP[] = { 0x150, 0x250 };
124static const u16 W83627EHF_REG_TEMP_HYST[] = { 0x153, 0x253 };
125static const u16 W83627EHF_REG_TEMP_OVER[] = { 0x155, 0x255 };
126static const u16 W83627EHF_REG_TEMP_CONFIG[] = { 0x152, 0x252 };
127
128/* Fan clock dividers are spread over the following five registers */
129#define W83627EHF_REG_FANDIV1 0x47
130#define W83627EHF_REG_FANDIV2 0x4B
131#define W83627EHF_REG_VBAT 0x5D
132#define W83627EHF_REG_DIODE 0x59
133#define W83627EHF_REG_SMI_OVT 0x4C
134
135/*
136 * Conversions
137 */
138
139static inline unsigned int
140fan_from_reg(u8 reg, unsigned int div)
141{
142 if (reg == 0 || reg == 255)
143 return 0;
144 return 1350000U / (reg * div);
145}
146
147static inline unsigned int
148div_from_reg(u8 reg)
149{
150 return 1 << reg;
151}
152
153static inline int
154temp1_from_reg(s8 reg)
155{
156 return reg * 1000;
157}
158
159static inline s8
160temp1_to_reg(int temp)
161{
162 if (temp <= -128000)
163 return -128;
164 if (temp >= 127000)
165 return 127;
166 if (temp < 0)
167 return (temp - 500) / 1000;
168 return (temp + 500) / 1000;
169}
170
171/*
172 * Data structures and manipulation thereof
173 */
174
175struct w83627ehf_data {
176 struct i2c_client client;
177 struct semaphore lock;
178
179 struct semaphore update_lock;
180 char valid; /* !=0 if following fields are valid */
181 unsigned long last_updated; /* In jiffies */
182
183 /* Register values */
184 u8 fan[5];
185 u8 fan_min[5];
186 u8 fan_div[5];
187 u8 has_fan; /* some fan inputs can be disabled */
188 s8 temp1;
189 s8 temp1_max;
190 s8 temp1_max_hyst;
191 s16 temp[2];
192 s16 temp_max[2];
193 s16 temp_max_hyst[2];
194};
195
196static inline int is_word_sized(u16 reg)
197{
198 return (((reg & 0xff00) == 0x100
199 || (reg & 0xff00) == 0x200)
200 && ((reg & 0x00ff) == 0x50
201 || (reg & 0x00ff) == 0x53
202 || (reg & 0x00ff) == 0x55));
203}
204
205/* We assume that the default bank is 0, thus the following two functions do
206 nothing for registers which live in bank 0. For others, they respectively
207 set the bank register to the correct value (before the register is
208 accessed), and back to 0 (afterwards). */
209static inline void w83627ehf_set_bank(struct i2c_client *client, u16 reg)
210{
211 if (reg & 0xff00) {
212 outb_p(W83627EHF_REG_BANK, client->addr + ADDR_REG_OFFSET);
213 outb_p(reg >> 8, client->addr + DATA_REG_OFFSET);
214 }
215}
216
217static inline void w83627ehf_reset_bank(struct i2c_client *client, u16 reg)
218{
219 if (reg & 0xff00) {
220 outb_p(W83627EHF_REG_BANK, client->addr + ADDR_REG_OFFSET);
221 outb_p(0, client->addr + DATA_REG_OFFSET);
222 }
223}
224
225static u16 w83627ehf_read_value(struct i2c_client *client, u16 reg)
226{
227 struct w83627ehf_data *data = i2c_get_clientdata(client);
228 int res, word_sized = is_word_sized(reg);
229
230 down(&data->lock);
231
232 w83627ehf_set_bank(client, reg);
233 outb_p(reg & 0xff, client->addr + ADDR_REG_OFFSET);
234 res = inb_p(client->addr + DATA_REG_OFFSET);
235 if (word_sized) {
236 outb_p((reg & 0xff) + 1,
237 client->addr + ADDR_REG_OFFSET);
238 res = (res << 8) + inb_p(client->addr + DATA_REG_OFFSET);
239 }
240 w83627ehf_reset_bank(client, reg);
241
242 up(&data->lock);
243
244 return res;
245}
246
247static int w83627ehf_write_value(struct i2c_client *client, u16 reg, u16 value)
248{
249 struct w83627ehf_data *data = i2c_get_clientdata(client);
250 int word_sized = is_word_sized(reg);
251
252 down(&data->lock);
253
254 w83627ehf_set_bank(client, reg);
255 outb_p(reg & 0xff, client->addr + ADDR_REG_OFFSET);
256 if (word_sized) {
257 outb_p(value >> 8, client->addr + DATA_REG_OFFSET);
258 outb_p((reg & 0xff) + 1,
259 client->addr + ADDR_REG_OFFSET);
260 }
261 outb_p(value & 0xff, client->addr + DATA_REG_OFFSET);
262 w83627ehf_reset_bank(client, reg);
263
264 up(&data->lock);
265 return 0;
266}
267
268/* This function assumes that the caller holds data->update_lock */
269static void w83627ehf_write_fan_div(struct i2c_client *client, int nr)
270{
271 struct w83627ehf_data *data = i2c_get_clientdata(client);
272 u8 reg;
273
274 switch (nr) {
275 case 0:
276 reg = (w83627ehf_read_value(client, W83627EHF_REG_FANDIV1) & 0xcf)
277 | ((data->fan_div[0] & 0x03) << 4);
278 w83627ehf_write_value(client, W83627EHF_REG_FANDIV1, reg);
279 reg = (w83627ehf_read_value(client, W83627EHF_REG_VBAT) & 0xdf)
280 | ((data->fan_div[0] & 0x04) << 3);
281 w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg);
282 break;
283 case 1:
284 reg = (w83627ehf_read_value(client, W83627EHF_REG_FANDIV1) & 0x3f)
285 | ((data->fan_div[1] & 0x03) << 6);
286 w83627ehf_write_value(client, W83627EHF_REG_FANDIV1, reg);
287 reg = (w83627ehf_read_value(client, W83627EHF_REG_VBAT) & 0xbf)
288 | ((data->fan_div[1] & 0x04) << 4);
289 w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg);
290 break;
291 case 2:
292 reg = (w83627ehf_read_value(client, W83627EHF_REG_FANDIV2) & 0x3f)
293 | ((data->fan_div[2] & 0x03) << 6);
294 w83627ehf_write_value(client, W83627EHF_REG_FANDIV2, reg);
295 reg = (w83627ehf_read_value(client, W83627EHF_REG_VBAT) & 0x7f)
296 | ((data->fan_div[2] & 0x04) << 5);
297 w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg);
298 break;
299 case 3:
300 reg = (w83627ehf_read_value(client, W83627EHF_REG_DIODE) & 0xfc)
301 | (data->fan_div[3] & 0x03);
302 w83627ehf_write_value(client, W83627EHF_REG_DIODE, reg);
303 reg = (w83627ehf_read_value(client, W83627EHF_REG_SMI_OVT) & 0x7f)
304 | ((data->fan_div[3] & 0x04) << 5);
305 w83627ehf_write_value(client, W83627EHF_REG_SMI_OVT, reg);
306 break;
307 case 4:
308 reg = (w83627ehf_read_value(client, W83627EHF_REG_DIODE) & 0x73)
309 | ((data->fan_div[4] & 0x03) << 3)
310 | ((data->fan_div[4] & 0x04) << 5);
311 w83627ehf_write_value(client, W83627EHF_REG_DIODE, reg);
312 break;
313 }
314}
315
316static struct w83627ehf_data *w83627ehf_update_device(struct device *dev)
317{
318 struct i2c_client *client = to_i2c_client(dev);
319 struct w83627ehf_data *data = i2c_get_clientdata(client);
320 int i;
321
322 down(&data->update_lock);
323
324 if (time_after(jiffies, data->last_updated + HZ)
325 || !data->valid) {
326 /* Fan clock dividers */
327 i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV1);
328 data->fan_div[0] = (i >> 4) & 0x03;
329 data->fan_div[1] = (i >> 6) & 0x03;
330 i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV2);
331 data->fan_div[2] = (i >> 6) & 0x03;
332 i = w83627ehf_read_value(client, W83627EHF_REG_VBAT);
333 data->fan_div[0] |= (i >> 3) & 0x04;
334 data->fan_div[1] |= (i >> 4) & 0x04;
335 data->fan_div[2] |= (i >> 5) & 0x04;
336 if (data->has_fan & ((1 << 3) | (1 << 4))) {
337 i = w83627ehf_read_value(client, W83627EHF_REG_DIODE);
338 data->fan_div[3] = i & 0x03;
339 data->fan_div[4] = ((i >> 2) & 0x03)
340 | ((i >> 5) & 0x04);
341 }
342 if (data->has_fan & (1 << 3)) {
343 i = w83627ehf_read_value(client, W83627EHF_REG_SMI_OVT);
344 data->fan_div[3] |= (i >> 5) & 0x04;
345 }
346
347 /* Measured fan speeds and limits */
348 for (i = 0; i < 5; i++) {
349 if (!(data->has_fan & (1 << i)))
350 continue;
351
352 data->fan[i] = w83627ehf_read_value(client,
353 W83627EHF_REG_FAN[i]);
354 data->fan_min[i] = w83627ehf_read_value(client,
355 W83627EHF_REG_FAN_MIN[i]);
356
357 /* If we failed to measure the fan speed and clock
358 divider can be increased, let's try that for next
359 time */
360 if (data->fan[i] == 0xff
361 && data->fan_div[i] < 0x07) {
362 dev_dbg(&client->dev, "Increasing fan %d "
363 "clock divider from %u to %u\n",
364 i, div_from_reg(data->fan_div[i]),
365 div_from_reg(data->fan_div[i] + 1));
366 data->fan_div[i]++;
367 w83627ehf_write_fan_div(client, i);
368 /* Preserve min limit if possible */
369 if (data->fan_min[i] >= 2
370 && data->fan_min[i] != 255)
371 w83627ehf_write_value(client,
372 W83627EHF_REG_FAN_MIN[i],
373 (data->fan_min[i] /= 2));
374 }
375 }
376
377 /* Measured temperatures and limits */
378 data->temp1 = w83627ehf_read_value(client,
379 W83627EHF_REG_TEMP1);
380 data->temp1_max = w83627ehf_read_value(client,
381 W83627EHF_REG_TEMP1_OVER);
382 data->temp1_max_hyst = w83627ehf_read_value(client,
383 W83627EHF_REG_TEMP1_HYST);
384 for (i = 0; i < 2; i++) {
385 data->temp[i] = w83627ehf_read_value(client,
386 W83627EHF_REG_TEMP[i]);
387 data->temp_max[i] = w83627ehf_read_value(client,
388 W83627EHF_REG_TEMP_OVER[i]);
389 data->temp_max_hyst[i] = w83627ehf_read_value(client,
390 W83627EHF_REG_TEMP_HYST[i]);
391 }
392
393 data->last_updated = jiffies;
394 data->valid = 1;
395 }
396
397 up(&data->update_lock);
398 return data;
399}
400
401/*
402 * Sysfs callback functions
403 */
404
405#define show_fan_reg(reg) \
406static ssize_t \
407show_##reg(struct device *dev, char *buf, int nr) \
408{ \
409 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
410 return sprintf(buf, "%d\n", \
411 fan_from_reg(data->reg[nr], \
412 div_from_reg(data->fan_div[nr]))); \
413}
414show_fan_reg(fan);
415show_fan_reg(fan_min);
416
417static ssize_t
418show_fan_div(struct device *dev, char *buf, int nr)
419{
420 struct w83627ehf_data *data = w83627ehf_update_device(dev);
421 return sprintf(buf, "%u\n",
422 div_from_reg(data->fan_div[nr]));
423}
424
425static ssize_t
426store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
427{
428 struct i2c_client *client = to_i2c_client(dev);
429 struct w83627ehf_data *data = i2c_get_clientdata(client);
430 unsigned int val = simple_strtoul(buf, NULL, 10);
431 unsigned int reg;
432 u8 new_div;
433
434 down(&data->update_lock);
435 if (!val) {
436 /* No min limit, alarm disabled */
437 data->fan_min[nr] = 255;
438 new_div = data->fan_div[nr]; /* No change */
439 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
440 } else if ((reg = 1350000U / val) >= 128 * 255) {
441 /* Speed below this value cannot possibly be represented,
442 even with the highest divider (128) */
443 data->fan_min[nr] = 254;
444 new_div = 7; /* 128 == (1 << 7) */
445 dev_warn(dev, "fan%u low limit %u below minimum %u, set to "
446 "minimum\n", nr + 1, val, fan_from_reg(254, 128));
447 } else if (!reg) {
448 /* Speed above this value cannot possibly be represented,
449 even with the lowest divider (1) */
450 data->fan_min[nr] = 1;
451 new_div = 0; /* 1 == (1 << 0) */
452 dev_warn(dev, "fan%u low limit %u above maximum %u, set to "
453 "maximum\n", nr + 1, val, fan_from_reg(1, 1));
454 } else {
455 /* Automatically pick the best divider, i.e. the one such
456 that the min limit will correspond to a register value
457 in the 96..192 range */
458 new_div = 0;
459 while (reg > 192 && new_div < 7) {
460 reg >>= 1;
461 new_div++;
462 }
463 data->fan_min[nr] = reg;
464 }
465
466 /* Write both the fan clock divider (if it changed) and the new
467 fan min (unconditionally) */
468 if (new_div != data->fan_div[nr]) {
469 if (new_div > data->fan_div[nr])
470 data->fan[nr] >>= (data->fan_div[nr] - new_div);
471 else
472 data->fan[nr] <<= (new_div - data->fan_div[nr]);
473
474 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
475 nr + 1, div_from_reg(data->fan_div[nr]),
476 div_from_reg(new_div));
477 data->fan_div[nr] = new_div;
478 w83627ehf_write_fan_div(client, nr);
479 }
480 w83627ehf_write_value(client, W83627EHF_REG_FAN_MIN[nr],
481 data->fan_min[nr]);
482 up(&data->update_lock);
483
484 return count;
485}
486
487#define sysfs_fan_offset(offset) \
488static ssize_t \
489show_reg_fan_##offset(struct device *dev, struct device_attribute *attr, \
490 char *buf) \
491{ \
492 return show_fan(dev, buf, offset-1); \
493} \
494static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
495 show_reg_fan_##offset, NULL);
496
497#define sysfs_fan_min_offset(offset) \
498static ssize_t \
499show_reg_fan##offset##_min(struct device *dev, struct device_attribute *attr, \
500 char *buf) \
501{ \
502 return show_fan_min(dev, buf, offset-1); \
503} \
504static ssize_t \
505store_reg_fan##offset##_min(struct device *dev, struct device_attribute *attr, \
506 const char *buf, size_t count) \
507{ \
508 return store_fan_min(dev, buf, count, offset-1); \
509} \
510static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
511 show_reg_fan##offset##_min, \
512 store_reg_fan##offset##_min);
513
514#define sysfs_fan_div_offset(offset) \
515static ssize_t \
516show_reg_fan##offset##_div(struct device *dev, struct device_attribute *attr, \
517 char *buf) \
518{ \
519 return show_fan_div(dev, buf, offset - 1); \
520} \
521static DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
522 show_reg_fan##offset##_div, NULL);
523
524sysfs_fan_offset(1);
525sysfs_fan_min_offset(1);
526sysfs_fan_div_offset(1);
527sysfs_fan_offset(2);
528sysfs_fan_min_offset(2);
529sysfs_fan_div_offset(2);
530sysfs_fan_offset(3);
531sysfs_fan_min_offset(3);
532sysfs_fan_div_offset(3);
533sysfs_fan_offset(4);
534sysfs_fan_min_offset(4);
535sysfs_fan_div_offset(4);
536sysfs_fan_offset(5);
537sysfs_fan_min_offset(5);
538sysfs_fan_div_offset(5);
539
540#define show_temp1_reg(reg) \
541static ssize_t \
542show_##reg(struct device *dev, struct device_attribute *attr, \
543 char *buf) \
544{ \
545 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
546 return sprintf(buf, "%d\n", temp1_from_reg(data->reg)); \
547}
548show_temp1_reg(temp1);
549show_temp1_reg(temp1_max);
550show_temp1_reg(temp1_max_hyst);
551
552#define store_temp1_reg(REG, reg) \
553static ssize_t \
554store_temp1_##reg(struct device *dev, struct device_attribute *attr, \
555 const char *buf, size_t count) \
556{ \
557 struct i2c_client *client = to_i2c_client(dev); \
558 struct w83627ehf_data *data = i2c_get_clientdata(client); \
559 u32 val = simple_strtoul(buf, NULL, 10); \
560 \
561 down(&data->update_lock); \
562 data->temp1_##reg = temp1_to_reg(val); \
563 w83627ehf_write_value(client, W83627EHF_REG_TEMP1_##REG, \
564 data->temp1_##reg); \
565 up(&data->update_lock); \
566 return count; \
567}
568store_temp1_reg(OVER, max);
569store_temp1_reg(HYST, max_hyst);
570
571static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp1, NULL);
572static DEVICE_ATTR(temp1_max, S_IRUGO| S_IWUSR,
573 show_temp1_max, store_temp1_max);
574static DEVICE_ATTR(temp1_max_hyst, S_IRUGO| S_IWUSR,
575 show_temp1_max_hyst, store_temp1_max_hyst);
576
577#define show_temp_reg(reg) \
578static ssize_t \
579show_##reg (struct device *dev, char *buf, int nr) \
580{ \
581 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
582 return sprintf(buf, "%d\n", \
583 LM75_TEMP_FROM_REG(data->reg[nr])); \
584}
585show_temp_reg(temp);
586show_temp_reg(temp_max);
587show_temp_reg(temp_max_hyst);
588
589#define store_temp_reg(REG, reg) \
590static ssize_t \
591store_##reg (struct device *dev, const char *buf, size_t count, int nr) \
592{ \
593 struct i2c_client *client = to_i2c_client(dev); \
594 struct w83627ehf_data *data = i2c_get_clientdata(client); \
595 u32 val = simple_strtoul(buf, NULL, 10); \
596 \
597 down(&data->update_lock); \
598 data->reg[nr] = LM75_TEMP_TO_REG(val); \
599 w83627ehf_write_value(client, W83627EHF_REG_TEMP_##REG[nr], \
600 data->reg[nr]); \
601 up(&data->update_lock); \
602 return count; \
603}
604store_temp_reg(OVER, temp_max);
605store_temp_reg(HYST, temp_max_hyst);
606
607#define sysfs_temp_offset(offset) \
608static ssize_t \
609show_reg_temp##offset (struct device *dev, struct device_attribute *attr, \
610 char *buf) \
611{ \
612 return show_temp(dev, buf, offset - 2); \
613} \
614static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
615 show_reg_temp##offset, NULL);
616
617#define sysfs_temp_reg_offset(reg, offset) \
618static ssize_t \
619show_reg_temp##offset##_##reg(struct device *dev, struct device_attribute *attr, \
620 char *buf) \
621{ \
622 return show_temp_##reg(dev, buf, offset - 2); \
623} \
624static ssize_t \
625store_reg_temp##offset##_##reg(struct device *dev, struct device_attribute *attr, \
626 const char *buf, size_t count) \
627{ \
628 return store_temp_##reg(dev, buf, count, offset - 2); \
629} \
630static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, \
631 show_reg_temp##offset##_##reg, \
632 store_reg_temp##offset##_##reg);
633
634sysfs_temp_offset(2);
635sysfs_temp_reg_offset(max, 2);
636sysfs_temp_reg_offset(max_hyst, 2);
637sysfs_temp_offset(3);
638sysfs_temp_reg_offset(max, 3);
639sysfs_temp_reg_offset(max_hyst, 3);
640
641/*
642 * Driver and client management
643 */
644
645static struct i2c_driver w83627ehf_driver;
646
647static void w83627ehf_init_client(struct i2c_client *client)
648{
649 int i;
650 u8 tmp;
651
652 /* Start monitoring is needed */
653 tmp = w83627ehf_read_value(client, W83627EHF_REG_CONFIG);
654 if (!(tmp & 0x01))
655 w83627ehf_write_value(client, W83627EHF_REG_CONFIG,
656 tmp | 0x01);
657
658 /* Enable temp2 and temp3 if needed */
659 for (i = 0; i < 2; i++) {
660 tmp = w83627ehf_read_value(client,
661 W83627EHF_REG_TEMP_CONFIG[i]);
662 if (tmp & 0x01)
663 w83627ehf_write_value(client,
664 W83627EHF_REG_TEMP_CONFIG[i],
665 tmp & 0xfe);
666 }
667}
668
669static int w83627ehf_detect(struct i2c_adapter *adapter, int address, int kind)
670{
671 struct i2c_client *client;
672 struct w83627ehf_data *data;
673 int i, err = 0;
674
675 if (!i2c_is_isa_adapter(adapter))
676 return 0;
677
678 if (!request_region(address, REGION_LENGTH, w83627ehf_driver.name)) {
679 err = -EBUSY;
680 goto exit;
681 }
682
683 if (!(data = kmalloc(sizeof(struct w83627ehf_data), GFP_KERNEL))) {
684 err = -ENOMEM;
685 goto exit_release;
686 }
687 memset(data, 0, sizeof(struct w83627ehf_data));
688
689 client = &data->client;
690 i2c_set_clientdata(client, data);
691 client->addr = address;
692 init_MUTEX(&data->lock);
693 client->adapter = adapter;
694 client->driver = &w83627ehf_driver;
695 client->flags = 0;
696
697 strlcpy(client->name, "w83627ehf", I2C_NAME_SIZE);
698 data->valid = 0;
699 init_MUTEX(&data->update_lock);
700
701 /* Tell the i2c layer a new client has arrived */
702 if ((err = i2c_attach_client(client)))
703 goto exit_free;
704
705 /* Initialize the chip */
706 w83627ehf_init_client(client);
707
708 /* A few vars need to be filled upon startup */
709 for (i = 0; i < 5; i++)
710 data->fan_min[i] = w83627ehf_read_value(client,
711 W83627EHF_REG_FAN_MIN[i]);
712
713 /* It looks like fan4 and fan5 pins can be alternatively used
714 as fan on/off switches */
715 data->has_fan = 0x07; /* fan1, fan2 and fan3 */
716 i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV1);
717 if (i & (1 << 2))
718 data->has_fan |= (1 << 3);
719 if (i & (1 << 0))
720 data->has_fan |= (1 << 4);
721
722 /* Register sysfs hooks */
723 device_create_file(&client->dev, &dev_attr_fan1_input);
724 device_create_file(&client->dev, &dev_attr_fan1_min);
725 device_create_file(&client->dev, &dev_attr_fan1_div);
726 device_create_file(&client->dev, &dev_attr_fan2_input);
727 device_create_file(&client->dev, &dev_attr_fan2_min);
728 device_create_file(&client->dev, &dev_attr_fan2_div);
729 device_create_file(&client->dev, &dev_attr_fan3_input);
730 device_create_file(&client->dev, &dev_attr_fan3_min);
731 device_create_file(&client->dev, &dev_attr_fan3_div);
732
733 if (data->has_fan & (1 << 3)) {
734 device_create_file(&client->dev, &dev_attr_fan4_input);
735 device_create_file(&client->dev, &dev_attr_fan4_min);
736 device_create_file(&client->dev, &dev_attr_fan4_div);
737 }
738 if (data->has_fan & (1 << 4)) {
739 device_create_file(&client->dev, &dev_attr_fan5_input);
740 device_create_file(&client->dev, &dev_attr_fan5_min);
741 device_create_file(&client->dev, &dev_attr_fan5_div);
742 }
743
744 device_create_file(&client->dev, &dev_attr_temp1_input);
745 device_create_file(&client->dev, &dev_attr_temp1_max);
746 device_create_file(&client->dev, &dev_attr_temp1_max_hyst);
747 device_create_file(&client->dev, &dev_attr_temp2_input);
748 device_create_file(&client->dev, &dev_attr_temp2_max);
749 device_create_file(&client->dev, &dev_attr_temp2_max_hyst);
750 device_create_file(&client->dev, &dev_attr_temp3_input);
751 device_create_file(&client->dev, &dev_attr_temp3_max);
752 device_create_file(&client->dev, &dev_attr_temp3_max_hyst);
753
754 return 0;
755
756exit_free:
757 kfree(data);
758exit_release:
759 release_region(address, REGION_LENGTH);
760exit:
761 return err;
762}
763
764static int w83627ehf_attach_adapter(struct i2c_adapter *adapter)
765{
766 if (!(adapter->class & I2C_CLASS_HWMON))
767 return 0;
768 return i2c_detect(adapter, &addr_data, w83627ehf_detect);
769}
770
771static int w83627ehf_detach_client(struct i2c_client *client)
772{
773 int err;
774
775 if ((err = i2c_detach_client(client))) {
776 dev_err(&client->dev, "Client deregistration failed, "
777 "client not detached.\n");
778 return err;
779 }
780 release_region(client->addr, REGION_LENGTH);
781 kfree(i2c_get_clientdata(client));
782
783 return 0;
784}
785
786static struct i2c_driver w83627ehf_driver = {
787 .owner = THIS_MODULE,
788 .name = "w83627ehf",
789 .flags = I2C_DF_NOTIFY,
790 .attach_adapter = w83627ehf_attach_adapter,
791 .detach_client = w83627ehf_detach_client,
792};
793
794static int __init w83627ehf_find(int sioaddr, int *address)
795{
796 u16 val;
797
798 REG = sioaddr;
799 VAL = sioaddr + 1;
800 superio_enter();
801
802 val = (superio_inb(SIO_REG_DEVID) << 8)
803 | superio_inb(SIO_REG_DEVID + 1);
804 if ((val & SIO_ID_MASK) != SIO_W83627EHF_ID) {
805 superio_exit();
806 return -ENODEV;
807 }
808
809 superio_select(W83627EHF_LD_HWM);
810 val = (superio_inb(SIO_REG_ADDR) << 8)
811 | superio_inb(SIO_REG_ADDR + 1);
812 *address = val & ~(REGION_LENGTH - 1);
813 if (*address == 0) {
814 superio_exit();
815 return -ENODEV;
816 }
817
818 /* Activate logical device if needed */
819 val = superio_inb(SIO_REG_ENABLE);
820 if (!(val & 0x01))
821 superio_outb(SIO_REG_ENABLE, val | 0x01);
822
823 superio_exit();
824 return 0;
825}
826
827static int __init sensors_w83627ehf_init(void)
828{
829 if (w83627ehf_find(0x2e, &normal_isa[0])
830 && w83627ehf_find(0x4e, &normal_isa[0]))
831 return -ENODEV;
832
833 return i2c_add_driver(&w83627ehf_driver);
834}
835
836static void __exit sensors_w83627ehf_exit(void)
837{
838 i2c_del_driver(&w83627ehf_driver);
839}
840
841MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
842MODULE_DESCRIPTION("W83627EHF driver");
843MODULE_LICENSE("GPL");
844
845module_init(sensors_w83627ehf_init);
846module_exit(sensors_w83627ehf_exit);
diff --git a/drivers/hwmon/w83627hf.c b/drivers/hwmon/w83627hf.c
new file mode 100644
index 000000000000..bd87a42e068a
--- /dev/null
+++ b/drivers/hwmon/w83627hf.c
@@ -0,0 +1,1511 @@
1/*
2 w83627hf.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998 - 2003 Frodo Looijaard <frodol@dds.nl>,
5 Philip Edelbrock <phil@netroedge.com>,
6 and Mark Studebaker <mdsxyz123@yahoo.com>
7 Ported to 2.6 by Bernhard C. Schrenk <clemy@clemy.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*/
23
24/*
25 Supports following chips:
26
27 Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
28 w83627hf 9 3 2 3 0x20 0x5ca3 no yes(LPC)
29 w83627thf 7 3 3 3 0x90 0x5ca3 no yes(LPC)
30 w83637hf 7 3 3 3 0x80 0x5ca3 no yes(LPC)
31 w83697hf 8 2 2 2 0x60 0x5ca3 no yes(LPC)
32
33 For other winbond chips, and for i2c support in the above chips,
34 use w83781d.c.
35
36 Note: automatic ("cruise") fan control for 697, 637 & 627thf not
37 supported yet.
38*/
39
40#include <linux/module.h>
41#include <linux/init.h>
42#include <linux/slab.h>
43#include <linux/jiffies.h>
44#include <linux/i2c.h>
45#include <linux/i2c-sensor.h>
46#include <linux/i2c-vid.h>
47#include <asm/io.h>
48#include "lm75.h"
49
50static u16 force_addr;
51module_param(force_addr, ushort, 0);
52MODULE_PARM_DESC(force_addr,
53 "Initialize the base address of the sensors");
54static u8 force_i2c = 0x1f;
55module_param(force_i2c, byte, 0);
56MODULE_PARM_DESC(force_i2c,
57 "Initialize the i2c address of the sensors");
58
59/* Addresses to scan */
60static unsigned short normal_i2c[] = { I2C_CLIENT_END };
61static unsigned int normal_isa[] = { 0, I2C_CLIENT_ISA_END };
62
63/* Insmod parameters */
64SENSORS_INSMOD_4(w83627hf, w83627thf, w83697hf, w83637hf);
65
66static int init = 1;
67module_param(init, bool, 0);
68MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
69
70/* modified from kernel/include/traps.c */
71static int REG; /* The register to read/write */
72#define DEV 0x07 /* Register: Logical device select */
73static int VAL; /* The value to read/write */
74
75/* logical device numbers for superio_select (below) */
76#define W83627HF_LD_FDC 0x00
77#define W83627HF_LD_PRT 0x01
78#define W83627HF_LD_UART1 0x02
79#define W83627HF_LD_UART2 0x03
80#define W83627HF_LD_KBC 0x05
81#define W83627HF_LD_CIR 0x06 /* w83627hf only */
82#define W83627HF_LD_GAME 0x07
83#define W83627HF_LD_MIDI 0x07
84#define W83627HF_LD_GPIO1 0x07
85#define W83627HF_LD_GPIO5 0x07 /* w83627thf only */
86#define W83627HF_LD_GPIO2 0x08
87#define W83627HF_LD_GPIO3 0x09
88#define W83627HF_LD_GPIO4 0x09 /* w83627thf only */
89#define W83627HF_LD_ACPI 0x0a
90#define W83627HF_LD_HWM 0x0b
91
92#define DEVID 0x20 /* Register: Device ID */
93
94#define W83627THF_GPIO5_EN 0x30 /* w83627thf only */
95#define W83627THF_GPIO5_IOSR 0xf3 /* w83627thf only */
96#define W83627THF_GPIO5_DR 0xf4 /* w83627thf only */
97
98static inline void
99superio_outb(int reg, int val)
100{
101 outb(reg, REG);
102 outb(val, VAL);
103}
104
105static inline int
106superio_inb(int reg)
107{
108 outb(reg, REG);
109 return inb(VAL);
110}
111
112static inline void
113superio_select(int ld)
114{
115 outb(DEV, REG);
116 outb(ld, VAL);
117}
118
119static inline void
120superio_enter(void)
121{
122 outb(0x87, REG);
123 outb(0x87, REG);
124}
125
126static inline void
127superio_exit(void)
128{
129 outb(0xAA, REG);
130}
131
132#define W627_DEVID 0x52
133#define W627THF_DEVID 0x82
134#define W697_DEVID 0x60
135#define W637_DEVID 0x70
136#define WINB_ACT_REG 0x30
137#define WINB_BASE_REG 0x60
138/* Constants specified below */
139
140/* Length of ISA address segment */
141#define WINB_EXTENT 8
142
143/* Where are the ISA address/data registers relative to the base address */
144#define W83781D_ADDR_REG_OFFSET 5
145#define W83781D_DATA_REG_OFFSET 6
146
147/* The W83781D registers */
148/* The W83782D registers for nr=7,8 are in bank 5 */
149#define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
150 (0x554 + (((nr) - 7) * 2)))
151#define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
152 (0x555 + (((nr) - 7) * 2)))
153#define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
154 (0x550 + (nr) - 7))
155
156#define W83781D_REG_FAN_MIN(nr) (0x3a + (nr))
157#define W83781D_REG_FAN(nr) (0x27 + (nr))
158
159#define W83781D_REG_TEMP2_CONFIG 0x152
160#define W83781D_REG_TEMP3_CONFIG 0x252
161#define W83781D_REG_TEMP(nr) ((nr == 3) ? (0x0250) : \
162 ((nr == 2) ? (0x0150) : \
163 (0x27)))
164#define W83781D_REG_TEMP_HYST(nr) ((nr == 3) ? (0x253) : \
165 ((nr == 2) ? (0x153) : \
166 (0x3A)))
167#define W83781D_REG_TEMP_OVER(nr) ((nr == 3) ? (0x255) : \
168 ((nr == 2) ? (0x155) : \
169 (0x39)))
170
171#define W83781D_REG_BANK 0x4E
172
173#define W83781D_REG_CONFIG 0x40
174#define W83781D_REG_ALARM1 0x41
175#define W83781D_REG_ALARM2 0x42
176#define W83781D_REG_ALARM3 0x450
177
178#define W83781D_REG_IRQ 0x4C
179#define W83781D_REG_BEEP_CONFIG 0x4D
180#define W83781D_REG_BEEP_INTS1 0x56
181#define W83781D_REG_BEEP_INTS2 0x57
182#define W83781D_REG_BEEP_INTS3 0x453
183
184#define W83781D_REG_VID_FANDIV 0x47
185
186#define W83781D_REG_CHIPID 0x49
187#define W83781D_REG_WCHIPID 0x58
188#define W83781D_REG_CHIPMAN 0x4F
189#define W83781D_REG_PIN 0x4B
190
191#define W83781D_REG_VBAT 0x5D
192
193#define W83627HF_REG_PWM1 0x5A
194#define W83627HF_REG_PWM2 0x5B
195#define W83627HF_REG_PWMCLK12 0x5C
196
197#define W83627THF_REG_PWM1 0x01 /* 697HF and 637HF too */
198#define W83627THF_REG_PWM2 0x03 /* 697HF and 637HF too */
199#define W83627THF_REG_PWM3 0x11 /* 637HF too */
200
201#define W83627THF_REG_VRM_OVT_CFG 0x18 /* 637HF too */
202
203static const u8 regpwm_627hf[] = { W83627HF_REG_PWM1, W83627HF_REG_PWM2 };
204static const u8 regpwm[] = { W83627THF_REG_PWM1, W83627THF_REG_PWM2,
205 W83627THF_REG_PWM3 };
206#define W836X7HF_REG_PWM(type, nr) (((type) == w83627hf) ? \
207 regpwm_627hf[(nr) - 1] : regpwm[(nr) - 1])
208
209#define W83781D_REG_I2C_ADDR 0x48
210#define W83781D_REG_I2C_SUBADDR 0x4A
211
212/* Sensor selection */
213#define W83781D_REG_SCFG1 0x5D
214static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
215#define W83781D_REG_SCFG2 0x59
216static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
217#define W83781D_DEFAULT_BETA 3435
218
219/* Conversions. Limit checking is only done on the TO_REG
220 variants. Note that you should be a bit careful with which arguments
221 these macros are called: arguments may be evaluated more than once.
222 Fixing this is just not worth it. */
223#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
224#define IN_FROM_REG(val) ((val) * 16)
225
226static inline u8 FAN_TO_REG(long rpm, int div)
227{
228 if (rpm == 0)
229 return 255;
230 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
231 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
232 254);
233}
234
235#define TEMP_MIN (-128000)
236#define TEMP_MAX ( 127000)
237
238/* TEMP: 0.001C/bit (-128C to +127C)
239 REG: 1C/bit, two's complement */
240static u8 TEMP_TO_REG(int temp)
241{
242 int ntemp = SENSORS_LIMIT(temp, TEMP_MIN, TEMP_MAX);
243 ntemp += (ntemp<0 ? -500 : 500);
244 return (u8)(ntemp / 1000);
245}
246
247static int TEMP_FROM_REG(u8 reg)
248{
249 return (s8)reg * 1000;
250}
251
252#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
253
254#define PWM_TO_REG(val) (SENSORS_LIMIT((val),0,255))
255
256#define BEEP_MASK_FROM_REG(val) (val)
257#define BEEP_MASK_TO_REG(val) ((val) & 0xffffff)
258#define BEEP_ENABLE_TO_REG(val) ((val)?1:0)
259#define BEEP_ENABLE_FROM_REG(val) ((val)?1:0)
260
261#define DIV_FROM_REG(val) (1 << (val))
262
263static inline u8 DIV_TO_REG(long val)
264{
265 int i;
266 val = SENSORS_LIMIT(val, 1, 128) >> 1;
267 for (i = 0; i < 7; i++) {
268 if (val == 0)
269 break;
270 val >>= 1;
271 }
272 return ((u8) i);
273}
274
275/* For each registered chip, we need to keep some data in memory. That
276 data is pointed to by w83627hf_list[NR]->data. The structure itself is
277 dynamically allocated, at the same time when a new client is allocated. */
278struct w83627hf_data {
279 struct i2c_client client;
280 struct semaphore lock;
281 enum chips type;
282
283 struct semaphore update_lock;
284 char valid; /* !=0 if following fields are valid */
285 unsigned long last_updated; /* In jiffies */
286
287 struct i2c_client *lm75; /* for secondary I2C addresses */
288 /* pointer to array of 2 subclients */
289
290 u8 in[9]; /* Register value */
291 u8 in_max[9]; /* Register value */
292 u8 in_min[9]; /* Register value */
293 u8 fan[3]; /* Register value */
294 u8 fan_min[3]; /* Register value */
295 u8 temp;
296 u8 temp_max; /* Register value */
297 u8 temp_max_hyst; /* Register value */
298 u16 temp_add[2]; /* Register value */
299 u16 temp_max_add[2]; /* Register value */
300 u16 temp_max_hyst_add[2]; /* Register value */
301 u8 fan_div[3]; /* Register encoding, shifted right */
302 u8 vid; /* Register encoding, combined */
303 u32 alarms; /* Register encoding, combined */
304 u32 beep_mask; /* Register encoding, combined */
305 u8 beep_enable; /* Boolean */
306 u8 pwm[3]; /* Register value */
307 u16 sens[3]; /* 782D/783S only.
308 1 = pentium diode; 2 = 3904 diode;
309 3000-5000 = thermistor beta.
310 Default = 3435.
311 Other Betas unimplemented */
312 u8 vrm;
313 u8 vrm_ovt; /* Register value, 627thf & 637hf only */
314};
315
316
317static int w83627hf_attach_adapter(struct i2c_adapter *adapter);
318static int w83627hf_detect(struct i2c_adapter *adapter, int address,
319 int kind);
320static int w83627hf_detach_client(struct i2c_client *client);
321
322static int w83627hf_read_value(struct i2c_client *client, u16 register);
323static int w83627hf_write_value(struct i2c_client *client, u16 register,
324 u16 value);
325static struct w83627hf_data *w83627hf_update_device(struct device *dev);
326static void w83627hf_init_client(struct i2c_client *client);
327
328static struct i2c_driver w83627hf_driver = {
329 .owner = THIS_MODULE,
330 .name = "w83627hf",
331 .id = I2C_DRIVERID_W83627HF,
332 .flags = I2C_DF_NOTIFY,
333 .attach_adapter = w83627hf_attach_adapter,
334 .detach_client = w83627hf_detach_client,
335};
336
337/* following are the sysfs callback functions */
338#define show_in_reg(reg) \
339static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
340{ \
341 struct w83627hf_data *data = w83627hf_update_device(dev); \
342 return sprintf(buf,"%ld\n", (long)IN_FROM_REG(data->reg[nr])); \
343}
344show_in_reg(in)
345show_in_reg(in_min)
346show_in_reg(in_max)
347
348#define store_in_reg(REG, reg) \
349static ssize_t \
350store_in_##reg (struct device *dev, const char *buf, size_t count, int nr) \
351{ \
352 struct i2c_client *client = to_i2c_client(dev); \
353 struct w83627hf_data *data = i2c_get_clientdata(client); \
354 u32 val; \
355 \
356 val = simple_strtoul(buf, NULL, 10); \
357 \
358 down(&data->update_lock); \
359 data->in_##reg[nr] = IN_TO_REG(val); \
360 w83627hf_write_value(client, W83781D_REG_IN_##REG(nr), \
361 data->in_##reg[nr]); \
362 \
363 up(&data->update_lock); \
364 return count; \
365}
366store_in_reg(MIN, min)
367store_in_reg(MAX, max)
368
369#define sysfs_in_offset(offset) \
370static ssize_t \
371show_regs_in_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
372{ \
373 return show_in(dev, buf, offset); \
374} \
375static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_regs_in_##offset, NULL);
376
377#define sysfs_in_reg_offset(reg, offset) \
378static ssize_t show_regs_in_##reg##offset (struct device *dev, struct device_attribute *attr, char *buf) \
379{ \
380 return show_in_##reg (dev, buf, offset); \
381} \
382static ssize_t \
383store_regs_in_##reg##offset (struct device *dev, struct device_attribute *attr, \
384 const char *buf, size_t count) \
385{ \
386 return store_in_##reg (dev, buf, count, offset); \
387} \
388static DEVICE_ATTR(in##offset##_##reg, S_IRUGO| S_IWUSR, \
389 show_regs_in_##reg##offset, store_regs_in_##reg##offset);
390
391#define sysfs_in_offsets(offset) \
392sysfs_in_offset(offset) \
393sysfs_in_reg_offset(min, offset) \
394sysfs_in_reg_offset(max, offset)
395
396sysfs_in_offsets(1);
397sysfs_in_offsets(2);
398sysfs_in_offsets(3);
399sysfs_in_offsets(4);
400sysfs_in_offsets(5);
401sysfs_in_offsets(6);
402sysfs_in_offsets(7);
403sysfs_in_offsets(8);
404
405/* use a different set of functions for in0 */
406static ssize_t show_in_0(struct w83627hf_data *data, char *buf, u8 reg)
407{
408 long in0;
409
410 if ((data->vrm_ovt & 0x01) &&
411 (w83627thf == data->type || w83637hf == data->type))
412
413 /* use VRM9 calculation */
414 in0 = (long)((reg * 488 + 70000 + 50) / 100);
415 else
416 /* use VRM8 (standard) calculation */
417 in0 = (long)IN_FROM_REG(reg);
418
419 return sprintf(buf,"%ld\n", in0);
420}
421
422static ssize_t show_regs_in_0(struct device *dev, struct device_attribute *attr, char *buf)
423{
424 struct w83627hf_data *data = w83627hf_update_device(dev);
425 return show_in_0(data, buf, data->in[0]);
426}
427
428static ssize_t show_regs_in_min0(struct device *dev, struct device_attribute *attr, char *buf)
429{
430 struct w83627hf_data *data = w83627hf_update_device(dev);
431 return show_in_0(data, buf, data->in_min[0]);
432}
433
434static ssize_t show_regs_in_max0(struct device *dev, struct device_attribute *attr, char *buf)
435{
436 struct w83627hf_data *data = w83627hf_update_device(dev);
437 return show_in_0(data, buf, data->in_max[0]);
438}
439
440static ssize_t store_regs_in_min0(struct device *dev, struct device_attribute *attr,
441 const char *buf, size_t count)
442{
443 struct i2c_client *client = to_i2c_client(dev);
444 struct w83627hf_data *data = i2c_get_clientdata(client);
445 u32 val;
446
447 val = simple_strtoul(buf, NULL, 10);
448
449 down(&data->update_lock);
450
451 if ((data->vrm_ovt & 0x01) &&
452 (w83627thf == data->type || w83637hf == data->type))
453
454 /* use VRM9 calculation */
455 data->in_min[0] = (u8)(((val * 100) - 70000 + 244) / 488);
456 else
457 /* use VRM8 (standard) calculation */
458 data->in_min[0] = IN_TO_REG(val);
459
460 w83627hf_write_value(client, W83781D_REG_IN_MIN(0), data->in_min[0]);
461 up(&data->update_lock);
462 return count;
463}
464
465static ssize_t store_regs_in_max0(struct device *dev, struct device_attribute *attr,
466 const char *buf, size_t count)
467{
468 struct i2c_client *client = to_i2c_client(dev);
469 struct w83627hf_data *data = i2c_get_clientdata(client);
470 u32 val;
471
472 val = simple_strtoul(buf, NULL, 10);
473
474 down(&data->update_lock);
475
476 if ((data->vrm_ovt & 0x01) &&
477 (w83627thf == data->type || w83637hf == data->type))
478
479 /* use VRM9 calculation */
480 data->in_max[0] = (u8)(((val * 100) - 70000 + 244) / 488);
481 else
482 /* use VRM8 (standard) calculation */
483 data->in_max[0] = IN_TO_REG(val);
484
485 w83627hf_write_value(client, W83781D_REG_IN_MAX(0), data->in_max[0]);
486 up(&data->update_lock);
487 return count;
488}
489
490static DEVICE_ATTR(in0_input, S_IRUGO, show_regs_in_0, NULL);
491static DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR,
492 show_regs_in_min0, store_regs_in_min0);
493static DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR,
494 show_regs_in_max0, store_regs_in_max0);
495
496#define device_create_file_in(client, offset) \
497do { \
498device_create_file(&client->dev, &dev_attr_in##offset##_input); \
499device_create_file(&client->dev, &dev_attr_in##offset##_min); \
500device_create_file(&client->dev, &dev_attr_in##offset##_max); \
501} while (0)
502
503#define show_fan_reg(reg) \
504static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
505{ \
506 struct w83627hf_data *data = w83627hf_update_device(dev); \
507 return sprintf(buf,"%ld\n", \
508 FAN_FROM_REG(data->reg[nr-1], \
509 (long)DIV_FROM_REG(data->fan_div[nr-1]))); \
510}
511show_fan_reg(fan);
512show_fan_reg(fan_min);
513
514static ssize_t
515store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
516{
517 struct i2c_client *client = to_i2c_client(dev);
518 struct w83627hf_data *data = i2c_get_clientdata(client);
519 u32 val;
520
521 val = simple_strtoul(buf, NULL, 10);
522
523 down(&data->update_lock);
524 data->fan_min[nr - 1] =
525 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr - 1]));
526 w83627hf_write_value(client, W83781D_REG_FAN_MIN(nr),
527 data->fan_min[nr - 1]);
528
529 up(&data->update_lock);
530 return count;
531}
532
533#define sysfs_fan_offset(offset) \
534static ssize_t show_regs_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
535{ \
536 return show_fan(dev, buf, offset); \
537} \
538static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_regs_fan_##offset, NULL);
539
540#define sysfs_fan_min_offset(offset) \
541static ssize_t show_regs_fan_min##offset (struct device *dev, struct device_attribute *attr, char *buf) \
542{ \
543 return show_fan_min(dev, buf, offset); \
544} \
545static ssize_t \
546store_regs_fan_min##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
547{ \
548 return store_fan_min(dev, buf, count, offset); \
549} \
550static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
551 show_regs_fan_min##offset, store_regs_fan_min##offset);
552
553sysfs_fan_offset(1);
554sysfs_fan_min_offset(1);
555sysfs_fan_offset(2);
556sysfs_fan_min_offset(2);
557sysfs_fan_offset(3);
558sysfs_fan_min_offset(3);
559
560#define device_create_file_fan(client, offset) \
561do { \
562device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
563device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
564} while (0)
565
566#define show_temp_reg(reg) \
567static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
568{ \
569 struct w83627hf_data *data = w83627hf_update_device(dev); \
570 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
571 return sprintf(buf,"%ld\n", \
572 (long)LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
573 } else { /* TEMP1 */ \
574 return sprintf(buf,"%ld\n", (long)TEMP_FROM_REG(data->reg)); \
575 } \
576}
577show_temp_reg(temp);
578show_temp_reg(temp_max);
579show_temp_reg(temp_max_hyst);
580
581#define store_temp_reg(REG, reg) \
582static ssize_t \
583store_temp_##reg (struct device *dev, const char *buf, size_t count, int nr) \
584{ \
585 struct i2c_client *client = to_i2c_client(dev); \
586 struct w83627hf_data *data = i2c_get_clientdata(client); \
587 u32 val; \
588 \
589 val = simple_strtoul(buf, NULL, 10); \
590 \
591 down(&data->update_lock); \
592 \
593 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
594 data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
595 w83627hf_write_value(client, W83781D_REG_TEMP_##REG(nr), \
596 data->temp_##reg##_add[nr-2]); \
597 } else { /* TEMP1 */ \
598 data->temp_##reg = TEMP_TO_REG(val); \
599 w83627hf_write_value(client, W83781D_REG_TEMP_##REG(nr), \
600 data->temp_##reg); \
601 } \
602 \
603 up(&data->update_lock); \
604 return count; \
605}
606store_temp_reg(OVER, max);
607store_temp_reg(HYST, max_hyst);
608
609#define sysfs_temp_offset(offset) \
610static ssize_t \
611show_regs_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
612{ \
613 return show_temp(dev, buf, offset); \
614} \
615static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_regs_temp_##offset, NULL);
616
617#define sysfs_temp_reg_offset(reg, offset) \
618static ssize_t show_regs_temp_##reg##offset (struct device *dev, struct device_attribute *attr, char *buf) \
619{ \
620 return show_temp_##reg (dev, buf, offset); \
621} \
622static ssize_t \
623store_regs_temp_##reg##offset (struct device *dev, struct device_attribute *attr, \
624 const char *buf, size_t count) \
625{ \
626 return store_temp_##reg (dev, buf, count, offset); \
627} \
628static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, \
629 show_regs_temp_##reg##offset, store_regs_temp_##reg##offset);
630
631#define sysfs_temp_offsets(offset) \
632sysfs_temp_offset(offset) \
633sysfs_temp_reg_offset(max, offset) \
634sysfs_temp_reg_offset(max_hyst, offset)
635
636sysfs_temp_offsets(1);
637sysfs_temp_offsets(2);
638sysfs_temp_offsets(3);
639
640#define device_create_file_temp(client, offset) \
641do { \
642device_create_file(&client->dev, &dev_attr_temp##offset##_input); \
643device_create_file(&client->dev, &dev_attr_temp##offset##_max); \
644device_create_file(&client->dev, &dev_attr_temp##offset##_max_hyst); \
645} while (0)
646
647static ssize_t
648show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
649{
650 struct w83627hf_data *data = w83627hf_update_device(dev);
651 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
652}
653static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
654#define device_create_file_vid(client) \
655device_create_file(&client->dev, &dev_attr_cpu0_vid)
656
657static ssize_t
658show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
659{
660 struct w83627hf_data *data = w83627hf_update_device(dev);
661 return sprintf(buf, "%ld\n", (long) data->vrm);
662}
663static ssize_t
664store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
665{
666 struct i2c_client *client = to_i2c_client(dev);
667 struct w83627hf_data *data = i2c_get_clientdata(client);
668 u32 val;
669
670 val = simple_strtoul(buf, NULL, 10);
671 data->vrm = val;
672
673 return count;
674}
675static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
676#define device_create_file_vrm(client) \
677device_create_file(&client->dev, &dev_attr_vrm)
678
679static ssize_t
680show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
681{
682 struct w83627hf_data *data = w83627hf_update_device(dev);
683 return sprintf(buf, "%ld\n", (long) data->alarms);
684}
685static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
686#define device_create_file_alarms(client) \
687device_create_file(&client->dev, &dev_attr_alarms)
688
689#define show_beep_reg(REG, reg) \
690static ssize_t show_beep_##reg (struct device *dev, struct device_attribute *attr, char *buf) \
691{ \
692 struct w83627hf_data *data = w83627hf_update_device(dev); \
693 return sprintf(buf,"%ld\n", \
694 (long)BEEP_##REG##_FROM_REG(data->beep_##reg)); \
695}
696show_beep_reg(ENABLE, enable)
697show_beep_reg(MASK, mask)
698
699#define BEEP_ENABLE 0 /* Store beep_enable */
700#define BEEP_MASK 1 /* Store beep_mask */
701
702static ssize_t
703store_beep_reg(struct device *dev, const char *buf, size_t count,
704 int update_mask)
705{
706 struct i2c_client *client = to_i2c_client(dev);
707 struct w83627hf_data *data = i2c_get_clientdata(client);
708 u32 val, val2;
709
710 val = simple_strtoul(buf, NULL, 10);
711
712 down(&data->update_lock);
713
714 if (update_mask == BEEP_MASK) { /* We are storing beep_mask */
715 data->beep_mask = BEEP_MASK_TO_REG(val);
716 w83627hf_write_value(client, W83781D_REG_BEEP_INTS1,
717 data->beep_mask & 0xff);
718 w83627hf_write_value(client, W83781D_REG_BEEP_INTS3,
719 ((data->beep_mask) >> 16) & 0xff);
720 val2 = (data->beep_mask >> 8) & 0x7f;
721 } else { /* We are storing beep_enable */
722 val2 =
723 w83627hf_read_value(client, W83781D_REG_BEEP_INTS2) & 0x7f;
724 data->beep_enable = BEEP_ENABLE_TO_REG(val);
725 }
726
727 w83627hf_write_value(client, W83781D_REG_BEEP_INTS2,
728 val2 | data->beep_enable << 7);
729
730 up(&data->update_lock);
731 return count;
732}
733
734#define sysfs_beep(REG, reg) \
735static ssize_t show_regs_beep_##reg (struct device *dev, struct device_attribute *attr, char *buf) \
736{ \
737 return show_beep_##reg(dev, attr, buf); \
738} \
739static ssize_t \
740store_regs_beep_##reg (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
741{ \
742 return store_beep_reg(dev, buf, count, BEEP_##REG); \
743} \
744static DEVICE_ATTR(beep_##reg, S_IRUGO | S_IWUSR, \
745 show_regs_beep_##reg, store_regs_beep_##reg);
746
747sysfs_beep(ENABLE, enable);
748sysfs_beep(MASK, mask);
749
750#define device_create_file_beep(client) \
751do { \
752device_create_file(&client->dev, &dev_attr_beep_enable); \
753device_create_file(&client->dev, &dev_attr_beep_mask); \
754} while (0)
755
756static ssize_t
757show_fan_div_reg(struct device *dev, char *buf, int nr)
758{
759 struct w83627hf_data *data = w83627hf_update_device(dev);
760 return sprintf(buf, "%ld\n",
761 (long) DIV_FROM_REG(data->fan_div[nr - 1]));
762}
763
764/* Note: we save and restore the fan minimum here, because its value is
765 determined in part by the fan divisor. This follows the principle of
766 least suprise; the user doesn't expect the fan minimum to change just
767 because the divisor changed. */
768static ssize_t
769store_fan_div_reg(struct device *dev, const char *buf, size_t count, int nr)
770{
771 struct i2c_client *client = to_i2c_client(dev);
772 struct w83627hf_data *data = i2c_get_clientdata(client);
773 unsigned long min;
774 u8 reg;
775 unsigned long val = simple_strtoul(buf, NULL, 10);
776
777 down(&data->update_lock);
778
779 /* Save fan_min */
780 min = FAN_FROM_REG(data->fan_min[nr],
781 DIV_FROM_REG(data->fan_div[nr]));
782
783 data->fan_div[nr] = DIV_TO_REG(val);
784
785 reg = (w83627hf_read_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
786 & (nr==0 ? 0xcf : 0x3f))
787 | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
788 w83627hf_write_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
789
790 reg = (w83627hf_read_value(client, W83781D_REG_VBAT)
791 & ~(1 << (5 + nr)))
792 | ((data->fan_div[nr] & 0x04) << (3 + nr));
793 w83627hf_write_value(client, W83781D_REG_VBAT, reg);
794
795 /* Restore fan_min */
796 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
797 w83627hf_write_value(client, W83781D_REG_FAN_MIN(nr+1), data->fan_min[nr]);
798
799 up(&data->update_lock);
800 return count;
801}
802
803#define sysfs_fan_div(offset) \
804static ssize_t show_regs_fan_div_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
805{ \
806 return show_fan_div_reg(dev, buf, offset); \
807} \
808static ssize_t \
809store_regs_fan_div_##offset (struct device *dev, struct device_attribute *attr, \
810 const char *buf, size_t count) \
811{ \
812 return store_fan_div_reg(dev, buf, count, offset - 1); \
813} \
814static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
815 show_regs_fan_div_##offset, store_regs_fan_div_##offset);
816
817sysfs_fan_div(1);
818sysfs_fan_div(2);
819sysfs_fan_div(3);
820
821#define device_create_file_fan_div(client, offset) \
822do { \
823device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
824} while (0)
825
826static ssize_t
827show_pwm_reg(struct device *dev, char *buf, int nr)
828{
829 struct w83627hf_data *data = w83627hf_update_device(dev);
830 return sprintf(buf, "%ld\n", (long) data->pwm[nr - 1]);
831}
832
833static ssize_t
834store_pwm_reg(struct device *dev, const char *buf, size_t count, int nr)
835{
836 struct i2c_client *client = to_i2c_client(dev);
837 struct w83627hf_data *data = i2c_get_clientdata(client);
838 u32 val;
839
840 val = simple_strtoul(buf, NULL, 10);
841
842 down(&data->update_lock);
843
844 if (data->type == w83627thf) {
845 /* bits 0-3 are reserved in 627THF */
846 data->pwm[nr - 1] = PWM_TO_REG(val) & 0xf0;
847 w83627hf_write_value(client,
848 W836X7HF_REG_PWM(data->type, nr),
849 data->pwm[nr - 1] |
850 (w83627hf_read_value(client,
851 W836X7HF_REG_PWM(data->type, nr)) & 0x0f));
852 } else {
853 data->pwm[nr - 1] = PWM_TO_REG(val);
854 w83627hf_write_value(client,
855 W836X7HF_REG_PWM(data->type, nr),
856 data->pwm[nr - 1]);
857 }
858
859 up(&data->update_lock);
860 return count;
861}
862
863#define sysfs_pwm(offset) \
864static ssize_t show_regs_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
865{ \
866 return show_pwm_reg(dev, buf, offset); \
867} \
868static ssize_t \
869store_regs_pwm_##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
870{ \
871 return store_pwm_reg(dev, buf, count, offset); \
872} \
873static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
874 show_regs_pwm_##offset, store_regs_pwm_##offset);
875
876sysfs_pwm(1);
877sysfs_pwm(2);
878sysfs_pwm(3);
879
880#define device_create_file_pwm(client, offset) \
881do { \
882device_create_file(&client->dev, &dev_attr_pwm##offset); \
883} while (0)
884
885static ssize_t
886show_sensor_reg(struct device *dev, char *buf, int nr)
887{
888 struct w83627hf_data *data = w83627hf_update_device(dev);
889 return sprintf(buf, "%ld\n", (long) data->sens[nr - 1]);
890}
891
892static ssize_t
893store_sensor_reg(struct device *dev, const char *buf, size_t count, int nr)
894{
895 struct i2c_client *client = to_i2c_client(dev);
896 struct w83627hf_data *data = i2c_get_clientdata(client);
897 u32 val, tmp;
898
899 val = simple_strtoul(buf, NULL, 10);
900
901 down(&data->update_lock);
902
903 switch (val) {
904 case 1: /* PII/Celeron diode */
905 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
906 w83627hf_write_value(client, W83781D_REG_SCFG1,
907 tmp | BIT_SCFG1[nr - 1]);
908 tmp = w83627hf_read_value(client, W83781D_REG_SCFG2);
909 w83627hf_write_value(client, W83781D_REG_SCFG2,
910 tmp | BIT_SCFG2[nr - 1]);
911 data->sens[nr - 1] = val;
912 break;
913 case 2: /* 3904 */
914 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
915 w83627hf_write_value(client, W83781D_REG_SCFG1,
916 tmp | BIT_SCFG1[nr - 1]);
917 tmp = w83627hf_read_value(client, W83781D_REG_SCFG2);
918 w83627hf_write_value(client, W83781D_REG_SCFG2,
919 tmp & ~BIT_SCFG2[nr - 1]);
920 data->sens[nr - 1] = val;
921 break;
922 case W83781D_DEFAULT_BETA: /* thermistor */
923 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
924 w83627hf_write_value(client, W83781D_REG_SCFG1,
925 tmp & ~BIT_SCFG1[nr - 1]);
926 data->sens[nr - 1] = val;
927 break;
928 default:
929 dev_err(&client->dev,
930 "Invalid sensor type %ld; must be 1, 2, or %d\n",
931 (long) val, W83781D_DEFAULT_BETA);
932 break;
933 }
934
935 up(&data->update_lock);
936 return count;
937}
938
939#define sysfs_sensor(offset) \
940static ssize_t show_regs_sensor_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
941{ \
942 return show_sensor_reg(dev, buf, offset); \
943} \
944static ssize_t \
945store_regs_sensor_##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
946{ \
947 return store_sensor_reg(dev, buf, count, offset); \
948} \
949static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
950 show_regs_sensor_##offset, store_regs_sensor_##offset);
951
952sysfs_sensor(1);
953sysfs_sensor(2);
954sysfs_sensor(3);
955
956#define device_create_file_sensor(client, offset) \
957do { \
958device_create_file(&client->dev, &dev_attr_temp##offset##_type); \
959} while (0)
960
961
962/* This function is called when:
963 * w83627hf_driver is inserted (when this module is loaded), for each
964 available adapter
965 * when a new adapter is inserted (and w83627hf_driver is still present) */
966static int w83627hf_attach_adapter(struct i2c_adapter *adapter)
967{
968 return i2c_detect(adapter, &addr_data, w83627hf_detect);
969}
970
971static int w83627hf_find(int sioaddr, int *address)
972{
973 u16 val;
974
975 REG = sioaddr;
976 VAL = sioaddr + 1;
977
978 superio_enter();
979 val= superio_inb(DEVID);
980 if(val != W627_DEVID &&
981 val != W627THF_DEVID &&
982 val != W697_DEVID &&
983 val != W637_DEVID) {
984 superio_exit();
985 return -ENODEV;
986 }
987
988 superio_select(W83627HF_LD_HWM);
989 val = (superio_inb(WINB_BASE_REG) << 8) |
990 superio_inb(WINB_BASE_REG + 1);
991 *address = val & ~(WINB_EXTENT - 1);
992 if (*address == 0 && force_addr == 0) {
993 superio_exit();
994 return -ENODEV;
995 }
996 if (force_addr)
997 *address = force_addr; /* so detect will get called */
998
999 superio_exit();
1000 return 0;
1001}
1002
1003int w83627hf_detect(struct i2c_adapter *adapter, int address,
1004 int kind)
1005{
1006 int val;
1007 struct i2c_client *new_client;
1008 struct w83627hf_data *data;
1009 int err = 0;
1010 const char *client_name = "";
1011
1012 if (!i2c_is_isa_adapter(adapter)) {
1013 err = -ENODEV;
1014 goto ERROR0;
1015 }
1016
1017 if(force_addr)
1018 address = force_addr & ~(WINB_EXTENT - 1);
1019
1020 if (!request_region(address, WINB_EXTENT, w83627hf_driver.name)) {
1021 err = -EBUSY;
1022 goto ERROR0;
1023 }
1024
1025 if(force_addr) {
1026 printk("w83627hf.o: forcing ISA address 0x%04X\n", address);
1027 superio_enter();
1028 superio_select(W83627HF_LD_HWM);
1029 superio_outb(WINB_BASE_REG, address >> 8);
1030 superio_outb(WINB_BASE_REG+1, address & 0xff);
1031 superio_exit();
1032 }
1033
1034 superio_enter();
1035 val= superio_inb(DEVID);
1036 if(val == W627_DEVID)
1037 kind = w83627hf;
1038 else if(val == W697_DEVID)
1039 kind = w83697hf;
1040 else if(val == W627THF_DEVID)
1041 kind = w83627thf;
1042 else if(val == W637_DEVID)
1043 kind = w83637hf;
1044 else {
1045 dev_info(&adapter->dev,
1046 "Unsupported chip (dev_id=0x%02X).\n", val);
1047 goto ERROR1;
1048 }
1049
1050 superio_select(W83627HF_LD_HWM);
1051 if((val = 0x01 & superio_inb(WINB_ACT_REG)) == 0)
1052 superio_outb(WINB_ACT_REG, 1);
1053 superio_exit();
1054
1055 /* OK. For now, we presume we have a valid client. We now create the
1056 client structure, even though we cannot fill it completely yet.
1057 But it allows us to access w83627hf_{read,write}_value. */
1058
1059 if (!(data = kmalloc(sizeof(struct w83627hf_data), GFP_KERNEL))) {
1060 err = -ENOMEM;
1061 goto ERROR1;
1062 }
1063 memset(data, 0, sizeof(struct w83627hf_data));
1064
1065 new_client = &data->client;
1066 i2c_set_clientdata(new_client, data);
1067 new_client->addr = address;
1068 init_MUTEX(&data->lock);
1069 new_client->adapter = adapter;
1070 new_client->driver = &w83627hf_driver;
1071 new_client->flags = 0;
1072
1073
1074 if (kind == w83627hf) {
1075 client_name = "w83627hf";
1076 } else if (kind == w83627thf) {
1077 client_name = "w83627thf";
1078 } else if (kind == w83697hf) {
1079 client_name = "w83697hf";
1080 } else if (kind == w83637hf) {
1081 client_name = "w83637hf";
1082 }
1083
1084 /* Fill in the remaining client fields and put into the global list */
1085 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
1086 data->type = kind;
1087 data->valid = 0;
1088 init_MUTEX(&data->update_lock);
1089
1090 /* Tell the I2C layer a new client has arrived */
1091 if ((err = i2c_attach_client(new_client)))
1092 goto ERROR2;
1093
1094 data->lm75 = NULL;
1095
1096 /* Initialize the chip */
1097 w83627hf_init_client(new_client);
1098
1099 /* A few vars need to be filled upon startup */
1100 data->fan_min[0] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(1));
1101 data->fan_min[1] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(2));
1102 data->fan_min[2] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(3));
1103
1104 /* Register sysfs hooks */
1105 device_create_file_in(new_client, 0);
1106 if (kind != w83697hf)
1107 device_create_file_in(new_client, 1);
1108 device_create_file_in(new_client, 2);
1109 device_create_file_in(new_client, 3);
1110 device_create_file_in(new_client, 4);
1111 if (kind != w83627thf && kind != w83637hf) {
1112 device_create_file_in(new_client, 5);
1113 device_create_file_in(new_client, 6);
1114 }
1115 device_create_file_in(new_client, 7);
1116 device_create_file_in(new_client, 8);
1117
1118 device_create_file_fan(new_client, 1);
1119 device_create_file_fan(new_client, 2);
1120 if (kind != w83697hf)
1121 device_create_file_fan(new_client, 3);
1122
1123 device_create_file_temp(new_client, 1);
1124 device_create_file_temp(new_client, 2);
1125 if (kind != w83697hf)
1126 device_create_file_temp(new_client, 3);
1127
1128 if (kind != w83697hf)
1129 device_create_file_vid(new_client);
1130
1131 if (kind != w83697hf)
1132 device_create_file_vrm(new_client);
1133
1134 device_create_file_fan_div(new_client, 1);
1135 device_create_file_fan_div(new_client, 2);
1136 if (kind != w83697hf)
1137 device_create_file_fan_div(new_client, 3);
1138
1139 device_create_file_alarms(new_client);
1140
1141 device_create_file_beep(new_client);
1142
1143 device_create_file_pwm(new_client, 1);
1144 device_create_file_pwm(new_client, 2);
1145 if (kind == w83627thf || kind == w83637hf)
1146 device_create_file_pwm(new_client, 3);
1147
1148 device_create_file_sensor(new_client, 1);
1149 device_create_file_sensor(new_client, 2);
1150 if (kind != w83697hf)
1151 device_create_file_sensor(new_client, 3);
1152
1153 return 0;
1154
1155 ERROR2:
1156 kfree(data);
1157 ERROR1:
1158 release_region(address, WINB_EXTENT);
1159 ERROR0:
1160 return err;
1161}
1162
1163static int w83627hf_detach_client(struct i2c_client *client)
1164{
1165 int err;
1166
1167 if ((err = i2c_detach_client(client))) {
1168 dev_err(&client->dev,
1169 "Client deregistration failed, client not detached.\n");
1170 return err;
1171 }
1172
1173 release_region(client->addr, WINB_EXTENT);
1174 kfree(i2c_get_clientdata(client));
1175
1176 return 0;
1177}
1178
1179
1180/*
1181 ISA access must always be locked explicitly!
1182 We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
1183 would slow down the W83781D access and should not be necessary.
1184 There are some ugly typecasts here, but the good news is - they should
1185 nowhere else be necessary! */
1186static int w83627hf_read_value(struct i2c_client *client, u16 reg)
1187{
1188 struct w83627hf_data *data = i2c_get_clientdata(client);
1189 int res, word_sized;
1190
1191 down(&data->lock);
1192 word_sized = (((reg & 0xff00) == 0x100)
1193 || ((reg & 0xff00) == 0x200))
1194 && (((reg & 0x00ff) == 0x50)
1195 || ((reg & 0x00ff) == 0x53)
1196 || ((reg & 0x00ff) == 0x55));
1197 if (reg & 0xff00) {
1198 outb_p(W83781D_REG_BANK,
1199 client->addr + W83781D_ADDR_REG_OFFSET);
1200 outb_p(reg >> 8,
1201 client->addr + W83781D_DATA_REG_OFFSET);
1202 }
1203 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1204 res = inb_p(client->addr + W83781D_DATA_REG_OFFSET);
1205 if (word_sized) {
1206 outb_p((reg & 0xff) + 1,
1207 client->addr + W83781D_ADDR_REG_OFFSET);
1208 res =
1209 (res << 8) + inb_p(client->addr +
1210 W83781D_DATA_REG_OFFSET);
1211 }
1212 if (reg & 0xff00) {
1213 outb_p(W83781D_REG_BANK,
1214 client->addr + W83781D_ADDR_REG_OFFSET);
1215 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1216 }
1217 up(&data->lock);
1218 return res;
1219}
1220
1221static int w83627thf_read_gpio5(struct i2c_client *client)
1222{
1223 int res = 0xff, sel;
1224
1225 superio_enter();
1226 superio_select(W83627HF_LD_GPIO5);
1227
1228 /* Make sure these GPIO pins are enabled */
1229 if (!(superio_inb(W83627THF_GPIO5_EN) & (1<<3))) {
1230 dev_dbg(&client->dev, "GPIO5 disabled, no VID function\n");
1231 goto exit;
1232 }
1233
1234 /* Make sure the pins are configured for input
1235 There must be at least five (VRM 9), and possibly 6 (VRM 10) */
1236 sel = superio_inb(W83627THF_GPIO5_IOSR);
1237 if ((sel & 0x1f) != 0x1f) {
1238 dev_dbg(&client->dev, "GPIO5 not configured for VID "
1239 "function\n");
1240 goto exit;
1241 }
1242
1243 dev_info(&client->dev, "Reading VID from GPIO5\n");
1244 res = superio_inb(W83627THF_GPIO5_DR) & sel;
1245
1246exit:
1247 superio_exit();
1248 return res;
1249}
1250
1251static int w83627hf_write_value(struct i2c_client *client, u16 reg, u16 value)
1252{
1253 struct w83627hf_data *data = i2c_get_clientdata(client);
1254 int word_sized;
1255
1256 down(&data->lock);
1257 word_sized = (((reg & 0xff00) == 0x100)
1258 || ((reg & 0xff00) == 0x200))
1259 && (((reg & 0x00ff) == 0x53)
1260 || ((reg & 0x00ff) == 0x55));
1261 if (reg & 0xff00) {
1262 outb_p(W83781D_REG_BANK,
1263 client->addr + W83781D_ADDR_REG_OFFSET);
1264 outb_p(reg >> 8,
1265 client->addr + W83781D_DATA_REG_OFFSET);
1266 }
1267 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1268 if (word_sized) {
1269 outb_p(value >> 8,
1270 client->addr + W83781D_DATA_REG_OFFSET);
1271 outb_p((reg & 0xff) + 1,
1272 client->addr + W83781D_ADDR_REG_OFFSET);
1273 }
1274 outb_p(value & 0xff,
1275 client->addr + W83781D_DATA_REG_OFFSET);
1276 if (reg & 0xff00) {
1277 outb_p(W83781D_REG_BANK,
1278 client->addr + W83781D_ADDR_REG_OFFSET);
1279 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1280 }
1281 up(&data->lock);
1282 return 0;
1283}
1284
1285/* Called when we have found a new W83781D. It should set limits, etc. */
1286static void w83627hf_init_client(struct i2c_client *client)
1287{
1288 struct w83627hf_data *data = i2c_get_clientdata(client);
1289 int i;
1290 int type = data->type;
1291 u8 tmp;
1292
1293 if(init) {
1294 /* save this register */
1295 i = w83627hf_read_value(client, W83781D_REG_BEEP_CONFIG);
1296 /* Reset all except Watchdog values and last conversion values
1297 This sets fan-divs to 2, among others */
1298 w83627hf_write_value(client, W83781D_REG_CONFIG, 0x80);
1299 /* Restore the register and disable power-on abnormal beep.
1300 This saves FAN 1/2/3 input/output values set by BIOS. */
1301 w83627hf_write_value(client, W83781D_REG_BEEP_CONFIG, i | 0x80);
1302 /* Disable master beep-enable (reset turns it on).
1303 Individual beeps should be reset to off but for some reason
1304 disabling this bit helps some people not get beeped */
1305 w83627hf_write_value(client, W83781D_REG_BEEP_INTS2, 0);
1306 }
1307
1308 /* Minimize conflicts with other winbond i2c-only clients... */
1309 /* disable i2c subclients... how to disable main i2c client?? */
1310 /* force i2c address to relatively uncommon address */
1311 w83627hf_write_value(client, W83781D_REG_I2C_SUBADDR, 0x89);
1312 w83627hf_write_value(client, W83781D_REG_I2C_ADDR, force_i2c);
1313
1314 /* Read VID only once */
1315 if (w83627hf == data->type || w83637hf == data->type) {
1316 int lo = w83627hf_read_value(client, W83781D_REG_VID_FANDIV);
1317 int hi = w83627hf_read_value(client, W83781D_REG_CHIPID);
1318 data->vid = (lo & 0x0f) | ((hi & 0x01) << 4);
1319 } else if (w83627thf == data->type) {
1320 data->vid = w83627thf_read_gpio5(client) & 0x3f;
1321 }
1322
1323 /* Read VRM & OVT Config only once */
1324 if (w83627thf == data->type || w83637hf == data->type) {
1325 data->vrm_ovt =
1326 w83627hf_read_value(client, W83627THF_REG_VRM_OVT_CFG);
1327 data->vrm = (data->vrm_ovt & 0x01) ? 90 : 82;
1328 } else {
1329 /* Convert VID to voltage based on default VRM */
1330 data->vrm = i2c_which_vrm();
1331 }
1332
1333 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
1334 for (i = 1; i <= 3; i++) {
1335 if (!(tmp & BIT_SCFG1[i - 1])) {
1336 data->sens[i - 1] = W83781D_DEFAULT_BETA;
1337 } else {
1338 if (w83627hf_read_value
1339 (client,
1340 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1341 data->sens[i - 1] = 1;
1342 else
1343 data->sens[i - 1] = 2;
1344 }
1345 if ((type == w83697hf) && (i == 2))
1346 break;
1347 }
1348
1349 if(init) {
1350 /* Enable temp2 */
1351 tmp = w83627hf_read_value(client, W83781D_REG_TEMP2_CONFIG);
1352 if (tmp & 0x01) {
1353 dev_warn(&client->dev, "Enabling temp2, readings "
1354 "might not make sense\n");
1355 w83627hf_write_value(client, W83781D_REG_TEMP2_CONFIG,
1356 tmp & 0xfe);
1357 }
1358
1359 /* Enable temp3 */
1360 if (type != w83697hf) {
1361 tmp = w83627hf_read_value(client,
1362 W83781D_REG_TEMP3_CONFIG);
1363 if (tmp & 0x01) {
1364 dev_warn(&client->dev, "Enabling temp3, "
1365 "readings might not make sense\n");
1366 w83627hf_write_value(client,
1367 W83781D_REG_TEMP3_CONFIG, tmp & 0xfe);
1368 }
1369 }
1370
1371 if (type == w83627hf) {
1372 /* enable PWM2 control (can't hurt since PWM reg
1373 should have been reset to 0xff) */
1374 w83627hf_write_value(client, W83627HF_REG_PWMCLK12,
1375 0x19);
1376 }
1377 /* enable comparator mode for temp2 and temp3 so
1378 alarm indication will work correctly */
1379 i = w83627hf_read_value(client, W83781D_REG_IRQ);
1380 if (!(i & 0x40))
1381 w83627hf_write_value(client, W83781D_REG_IRQ,
1382 i | 0x40);
1383 }
1384
1385 /* Start monitoring */
1386 w83627hf_write_value(client, W83781D_REG_CONFIG,
1387 (w83627hf_read_value(client,
1388 W83781D_REG_CONFIG) & 0xf7)
1389 | 0x01);
1390}
1391
1392static struct w83627hf_data *w83627hf_update_device(struct device *dev)
1393{
1394 struct i2c_client *client = to_i2c_client(dev);
1395 struct w83627hf_data *data = i2c_get_clientdata(client);
1396 int i;
1397
1398 down(&data->update_lock);
1399
1400 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1401 || !data->valid) {
1402 for (i = 0; i <= 8; i++) {
1403 /* skip missing sensors */
1404 if (((data->type == w83697hf) && (i == 1)) ||
1405 ((data->type == w83627thf || data->type == w83637hf)
1406 && (i == 4 || i == 5)))
1407 continue;
1408 data->in[i] =
1409 w83627hf_read_value(client, W83781D_REG_IN(i));
1410 data->in_min[i] =
1411 w83627hf_read_value(client,
1412 W83781D_REG_IN_MIN(i));
1413 data->in_max[i] =
1414 w83627hf_read_value(client,
1415 W83781D_REG_IN_MAX(i));
1416 }
1417 for (i = 1; i <= 3; i++) {
1418 data->fan[i - 1] =
1419 w83627hf_read_value(client, W83781D_REG_FAN(i));
1420 data->fan_min[i - 1] =
1421 w83627hf_read_value(client,
1422 W83781D_REG_FAN_MIN(i));
1423 }
1424 for (i = 1; i <= 3; i++) {
1425 u8 tmp = w83627hf_read_value(client,
1426 W836X7HF_REG_PWM(data->type, i));
1427 /* bits 0-3 are reserved in 627THF */
1428 if (data->type == w83627thf)
1429 tmp &= 0xf0;
1430 data->pwm[i - 1] = tmp;
1431 if(i == 2 &&
1432 (data->type == w83627hf || data->type == w83697hf))
1433 break;
1434 }
1435
1436 data->temp = w83627hf_read_value(client, W83781D_REG_TEMP(1));
1437 data->temp_max =
1438 w83627hf_read_value(client, W83781D_REG_TEMP_OVER(1));
1439 data->temp_max_hyst =
1440 w83627hf_read_value(client, W83781D_REG_TEMP_HYST(1));
1441 data->temp_add[0] =
1442 w83627hf_read_value(client, W83781D_REG_TEMP(2));
1443 data->temp_max_add[0] =
1444 w83627hf_read_value(client, W83781D_REG_TEMP_OVER(2));
1445 data->temp_max_hyst_add[0] =
1446 w83627hf_read_value(client, W83781D_REG_TEMP_HYST(2));
1447 if (data->type != w83697hf) {
1448 data->temp_add[1] =
1449 w83627hf_read_value(client, W83781D_REG_TEMP(3));
1450 data->temp_max_add[1] =
1451 w83627hf_read_value(client, W83781D_REG_TEMP_OVER(3));
1452 data->temp_max_hyst_add[1] =
1453 w83627hf_read_value(client, W83781D_REG_TEMP_HYST(3));
1454 }
1455
1456 i = w83627hf_read_value(client, W83781D_REG_VID_FANDIV);
1457 data->fan_div[0] = (i >> 4) & 0x03;
1458 data->fan_div[1] = (i >> 6) & 0x03;
1459 if (data->type != w83697hf) {
1460 data->fan_div[2] = (w83627hf_read_value(client,
1461 W83781D_REG_PIN) >> 6) & 0x03;
1462 }
1463 i = w83627hf_read_value(client, W83781D_REG_VBAT);
1464 data->fan_div[0] |= (i >> 3) & 0x04;
1465 data->fan_div[1] |= (i >> 4) & 0x04;
1466 if (data->type != w83697hf)
1467 data->fan_div[2] |= (i >> 5) & 0x04;
1468 data->alarms =
1469 w83627hf_read_value(client, W83781D_REG_ALARM1) |
1470 (w83627hf_read_value(client, W83781D_REG_ALARM2) << 8) |
1471 (w83627hf_read_value(client, W83781D_REG_ALARM3) << 16);
1472 i = w83627hf_read_value(client, W83781D_REG_BEEP_INTS2);
1473 data->beep_enable = i >> 7;
1474 data->beep_mask = ((i & 0x7f) << 8) |
1475 w83627hf_read_value(client, W83781D_REG_BEEP_INTS1) |
1476 w83627hf_read_value(client, W83781D_REG_BEEP_INTS3) << 16;
1477 data->last_updated = jiffies;
1478 data->valid = 1;
1479 }
1480
1481 up(&data->update_lock);
1482
1483 return data;
1484}
1485
1486static int __init sensors_w83627hf_init(void)
1487{
1488 int addr;
1489
1490 if (w83627hf_find(0x2e, &addr)
1491 && w83627hf_find(0x4e, &addr)) {
1492 return -ENODEV;
1493 }
1494 normal_isa[0] = addr;
1495
1496 return i2c_add_driver(&w83627hf_driver);
1497}
1498
1499static void __exit sensors_w83627hf_exit(void)
1500{
1501 i2c_del_driver(&w83627hf_driver);
1502}
1503
1504MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
1505 "Philip Edelbrock <phil@netroedge.com>, "
1506 "and Mark Studebaker <mdsxyz123@yahoo.com>");
1507MODULE_DESCRIPTION("W83627HF driver");
1508MODULE_LICENSE("GPL");
1509
1510module_init(sensors_w83627hf_init);
1511module_exit(sensors_w83627hf_exit);
diff --git a/drivers/hwmon/w83781d.c b/drivers/hwmon/w83781d.c
new file mode 100644
index 000000000000..0bb131ce09eb
--- /dev/null
+++ b/drivers/hwmon/w83781d.c
@@ -0,0 +1,1632 @@
1/*
2 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
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23/*
24 Supports following chips:
25
26 Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
27 as99127f 7 3 0 3 0x31 0x12c3 yes no
28 as99127f rev.2 (type_name = as99127f) 0x31 0x5ca3 yes no
29 w83781d 7 3 0 3 0x10-1 0x5ca3 yes yes
30 w83627hf 9 3 2 3 0x21 0x5ca3 yes yes(LPC)
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*/
35
36#include <linux/module.h>
37#include <linux/init.h>
38#include <linux/slab.h>
39#include <linux/jiffies.h>
40#include <linux/i2c.h>
41#include <linux/i2c-sensor.h>
42#include <linux/i2c-vid.h>
43#include <asm/io.h>
44#include "lm75.h"
45
46/* Addresses to scan */
47static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
48 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
49 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
50static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
51
52/* Insmod parameters */
53SENSORS_INSMOD_5(w83781d, w83782d, w83783s, w83627hf, as99127f);
54I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
55 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
56
57static int init = 1;
58module_param(init, bool, 0);
59MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
60
61/* Constants specified below */
62
63/* Length of ISA address segment */
64#define W83781D_EXTENT 8
65
66/* Where are the ISA address/data registers relative to the base address */
67#define W83781D_ADDR_REG_OFFSET 5
68#define W83781D_DATA_REG_OFFSET 6
69
70/* The W83781D registers */
71/* The W83782D registers for nr=7,8 are in bank 5 */
72#define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
73 (0x554 + (((nr) - 7) * 2)))
74#define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
75 (0x555 + (((nr) - 7) * 2)))
76#define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
77 (0x550 + (nr) - 7))
78
79#define W83781D_REG_FAN_MIN(nr) (0x3a + (nr))
80#define W83781D_REG_FAN(nr) (0x27 + (nr))
81
82#define W83781D_REG_BANK 0x4E
83#define W83781D_REG_TEMP2_CONFIG 0x152
84#define W83781D_REG_TEMP3_CONFIG 0x252
85#define W83781D_REG_TEMP(nr) ((nr == 3) ? (0x0250) : \
86 ((nr == 2) ? (0x0150) : \
87 (0x27)))
88#define W83781D_REG_TEMP_HYST(nr) ((nr == 3) ? (0x253) : \
89 ((nr == 2) ? (0x153) : \
90 (0x3A)))
91#define W83781D_REG_TEMP_OVER(nr) ((nr == 3) ? (0x255) : \
92 ((nr == 2) ? (0x155) : \
93 (0x39)))
94
95#define W83781D_REG_CONFIG 0x40
96#define W83781D_REG_ALARM1 0x41
97#define W83781D_REG_ALARM2 0x42
98#define W83781D_REG_ALARM3 0x450 /* not on W83781D */
99
100#define W83781D_REG_IRQ 0x4C
101#define W83781D_REG_BEEP_CONFIG 0x4D
102#define W83781D_REG_BEEP_INTS1 0x56
103#define W83781D_REG_BEEP_INTS2 0x57
104#define W83781D_REG_BEEP_INTS3 0x453 /* not on W83781D */
105
106#define W83781D_REG_VID_FANDIV 0x47
107
108#define W83781D_REG_CHIPID 0x49
109#define W83781D_REG_WCHIPID 0x58
110#define W83781D_REG_CHIPMAN 0x4F
111#define W83781D_REG_PIN 0x4B
112
113/* 782D/783S only */
114#define W83781D_REG_VBAT 0x5D
115
116/* PWM 782D (1-4) and 783S (1-2) only */
117#define W83781D_REG_PWM1 0x5B /* 782d and 783s/627hf datasheets disagree */
118 /* on which is which; */
119#define W83781D_REG_PWM2 0x5A /* We follow the 782d convention here, */
120 /* However 782d is probably wrong. */
121#define W83781D_REG_PWM3 0x5E
122#define W83781D_REG_PWM4 0x5F
123#define W83781D_REG_PWMCLK12 0x5C
124#define W83781D_REG_PWMCLK34 0x45C
125static const u8 regpwm[] = { W83781D_REG_PWM1, W83781D_REG_PWM2,
126 W83781D_REG_PWM3, W83781D_REG_PWM4
127};
128
129#define W83781D_REG_PWM(nr) (regpwm[(nr) - 1])
130
131#define W83781D_REG_I2C_ADDR 0x48
132#define W83781D_REG_I2C_SUBADDR 0x4A
133
134/* The following are undocumented in the data sheets however we
135 received the information in an email from Winbond tech support */
136/* Sensor selection - not on 781d */
137#define W83781D_REG_SCFG1 0x5D
138static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
139
140#define W83781D_REG_SCFG2 0x59
141static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
142
143#define W83781D_DEFAULT_BETA 3435
144
145/* RT Table registers */
146#define W83781D_REG_RT_IDX 0x50
147#define W83781D_REG_RT_VAL 0x51
148
149/* Conversions. Rounding and limit checking is only done on the TO_REG
150 variants. Note that you should be a bit careful with which arguments
151 these macros are called: arguments may be evaluated more than once.
152 Fixing this is just not worth it. */
153#define IN_TO_REG(val) (SENSORS_LIMIT((((val) * 10 + 8)/16),0,255))
154#define IN_FROM_REG(val) (((val) * 16) / 10)
155
156static inline u8
157FAN_TO_REG(long rpm, int div)
158{
159 if (rpm == 0)
160 return 255;
161 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
162 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
163}
164
165#define FAN_FROM_REG(val,div) ((val) == 0 ? -1 : \
166 ((val) == 255 ? 0 : \
167 1350000 / ((val) * (div))))
168
169#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (val)+0x100*1000 \
170 : (val)) / 1000, 0, 0xff))
171#define TEMP_FROM_REG(val) (((val) & 0x80 ? (val)-0x100 : (val)) * 1000)
172
173#define PWM_FROM_REG(val) (val)
174#define PWM_TO_REG(val) (SENSORS_LIMIT((val),0,255))
175#define BEEP_MASK_FROM_REG(val,type) ((type) == as99127f ? \
176 (val) ^ 0x7fff : (val))
177#define BEEP_MASK_TO_REG(val,type) ((type) == as99127f ? \
178 (~(val)) & 0x7fff : (val) & 0xffffff)
179
180#define BEEP_ENABLE_TO_REG(val) ((val) ? 1 : 0)
181#define BEEP_ENABLE_FROM_REG(val) ((val) ? 1 : 0)
182
183#define DIV_FROM_REG(val) (1 << (val))
184
185static inline u8
186DIV_TO_REG(long val, enum chips type)
187{
188 int i;
189 val = SENSORS_LIMIT(val, 1,
190 ((type == w83781d
191 || type == as99127f) ? 8 : 128)) >> 1;
192 for (i = 0; i < 7; i++) {
193 if (val == 0)
194 break;
195 val >>= 1;
196 }
197 return ((u8) i);
198}
199
200/* There are some complications in a module like this. First off, W83781D chips
201 may be both present on the SMBus and the ISA bus, and we have to handle
202 those cases separately at some places. Second, there might be several
203 W83781D chips available (well, actually, that is probably never done; but
204 it is a clean illustration of how to handle a case like that). Finally,
205 a specific chip may be attached to *both* ISA and SMBus, and we would
206 not like to detect it double. Fortunately, in the case of the W83781D at
207 least, a register tells us what SMBus address we are on, so that helps
208 a bit - except if there could be more than one SMBus. Groan. No solution
209 for this yet. */
210
211/* This module may seem overly long and complicated. In fact, it is not so
212 bad. Quite a lot of bookkeeping is done. A real driver can often cut
213 some corners. */
214
215/* For each registered W83781D, we need to keep some data in memory. That
216 data is pointed to by w83781d_list[NR]->data. The structure itself is
217 dynamically allocated, at the same time when a new w83781d client is
218 allocated. */
219struct w83781d_data {
220 struct i2c_client client;
221 struct semaphore lock;
222 enum chips type;
223
224 struct semaphore update_lock;
225 char valid; /* !=0 if following fields are valid */
226 unsigned long last_updated; /* In jiffies */
227
228 struct i2c_client *lm75[2]; /* for secondary I2C addresses */
229 /* array of 2 pointers to subclients */
230
231 u8 in[9]; /* Register value - 8 & 9 for 782D only */
232 u8 in_max[9]; /* Register value - 8 & 9 for 782D only */
233 u8 in_min[9]; /* Register value - 8 & 9 for 782D only */
234 u8 fan[3]; /* Register value */
235 u8 fan_min[3]; /* Register value */
236 u8 temp;
237 u8 temp_max; /* Register value */
238 u8 temp_max_hyst; /* Register value */
239 u16 temp_add[2]; /* Register value */
240 u16 temp_max_add[2]; /* Register value */
241 u16 temp_max_hyst_add[2]; /* Register value */
242 u8 fan_div[3]; /* Register encoding, shifted right */
243 u8 vid; /* Register encoding, combined */
244 u32 alarms; /* Register encoding, combined */
245 u32 beep_mask; /* Register encoding, combined */
246 u8 beep_enable; /* Boolean */
247 u8 pwm[4]; /* Register value */
248 u8 pwmenable[4]; /* Boolean */
249 u16 sens[3]; /* 782D/783S only.
250 1 = pentium diode; 2 = 3904 diode;
251 3000-5000 = thermistor beta.
252 Default = 3435.
253 Other Betas unimplemented */
254 u8 vrm;
255};
256
257static int w83781d_attach_adapter(struct i2c_adapter *adapter);
258static int w83781d_detect(struct i2c_adapter *adapter, int address, int kind);
259static int w83781d_detach_client(struct i2c_client *client);
260
261static int w83781d_read_value(struct i2c_client *client, u16 register);
262static int w83781d_write_value(struct i2c_client *client, u16 register,
263 u16 value);
264static struct w83781d_data *w83781d_update_device(struct device *dev);
265static void w83781d_init_client(struct i2c_client *client);
266
267static struct i2c_driver w83781d_driver = {
268 .owner = THIS_MODULE,
269 .name = "w83781d",
270 .id = I2C_DRIVERID_W83781D,
271 .flags = I2C_DF_NOTIFY,
272 .attach_adapter = w83781d_attach_adapter,
273 .detach_client = w83781d_detach_client,
274};
275
276/* following are the sysfs callback functions */
277#define show_in_reg(reg) \
278static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
279{ \
280 struct w83781d_data *data = w83781d_update_device(dev); \
281 return sprintf(buf,"%ld\n", (long)IN_FROM_REG(data->reg[nr] * 10)); \
282}
283show_in_reg(in);
284show_in_reg(in_min);
285show_in_reg(in_max);
286
287#define store_in_reg(REG, reg) \
288static ssize_t store_in_##reg (struct device *dev, const char *buf, size_t count, int nr) \
289{ \
290 struct i2c_client *client = to_i2c_client(dev); \
291 struct w83781d_data *data = i2c_get_clientdata(client); \
292 u32 val; \
293 \
294 val = simple_strtoul(buf, NULL, 10) / 10; \
295 \
296 down(&data->update_lock); \
297 data->in_##reg[nr] = IN_TO_REG(val); \
298 w83781d_write_value(client, W83781D_REG_IN_##REG(nr), data->in_##reg[nr]); \
299 \
300 up(&data->update_lock); \
301 return count; \
302}
303store_in_reg(MIN, min);
304store_in_reg(MAX, max);
305
306#define sysfs_in_offset(offset) \
307static ssize_t \
308show_regs_in_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
309{ \
310 return show_in(dev, buf, offset); \
311} \
312static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_regs_in_##offset, NULL);
313
314#define sysfs_in_reg_offset(reg, offset) \
315static ssize_t show_regs_in_##reg##offset (struct device *dev, struct device_attribute *attr, char *buf) \
316{ \
317 return show_in_##reg (dev, buf, offset); \
318} \
319static ssize_t store_regs_in_##reg##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
320{ \
321 return store_in_##reg (dev, buf, count, offset); \
322} \
323static DEVICE_ATTR(in##offset##_##reg, S_IRUGO| S_IWUSR, show_regs_in_##reg##offset, store_regs_in_##reg##offset);
324
325#define sysfs_in_offsets(offset) \
326sysfs_in_offset(offset); \
327sysfs_in_reg_offset(min, offset); \
328sysfs_in_reg_offset(max, offset);
329
330sysfs_in_offsets(0);
331sysfs_in_offsets(1);
332sysfs_in_offsets(2);
333sysfs_in_offsets(3);
334sysfs_in_offsets(4);
335sysfs_in_offsets(5);
336sysfs_in_offsets(6);
337sysfs_in_offsets(7);
338sysfs_in_offsets(8);
339
340#define device_create_file_in(client, offset) \
341do { \
342device_create_file(&client->dev, &dev_attr_in##offset##_input); \
343device_create_file(&client->dev, &dev_attr_in##offset##_min); \
344device_create_file(&client->dev, &dev_attr_in##offset##_max); \
345} while (0)
346
347#define show_fan_reg(reg) \
348static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
349{ \
350 struct w83781d_data *data = w83781d_update_device(dev); \
351 return sprintf(buf,"%ld\n", \
352 FAN_FROM_REG(data->reg[nr-1], (long)DIV_FROM_REG(data->fan_div[nr-1]))); \
353}
354show_fan_reg(fan);
355show_fan_reg(fan_min);
356
357static ssize_t
358store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
359{
360 struct i2c_client *client = to_i2c_client(dev);
361 struct w83781d_data *data = i2c_get_clientdata(client);
362 u32 val;
363
364 val = simple_strtoul(buf, NULL, 10);
365
366 down(&data->update_lock);
367 data->fan_min[nr - 1] =
368 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr - 1]));
369 w83781d_write_value(client, W83781D_REG_FAN_MIN(nr),
370 data->fan_min[nr - 1]);
371
372 up(&data->update_lock);
373 return count;
374}
375
376#define sysfs_fan_offset(offset) \
377static ssize_t show_regs_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
378{ \
379 return show_fan(dev, buf, offset); \
380} \
381static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_regs_fan_##offset, NULL);
382
383#define sysfs_fan_min_offset(offset) \
384static ssize_t show_regs_fan_min##offset (struct device *dev, struct device_attribute *attr, char *buf) \
385{ \
386 return show_fan_min(dev, buf, offset); \
387} \
388static ssize_t store_regs_fan_min##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
389{ \
390 return store_fan_min(dev, buf, count, offset); \
391} \
392static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, show_regs_fan_min##offset, store_regs_fan_min##offset);
393
394sysfs_fan_offset(1);
395sysfs_fan_min_offset(1);
396sysfs_fan_offset(2);
397sysfs_fan_min_offset(2);
398sysfs_fan_offset(3);
399sysfs_fan_min_offset(3);
400
401#define device_create_file_fan(client, offset) \
402do { \
403device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
404device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
405} while (0)
406
407#define show_temp_reg(reg) \
408static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
409{ \
410 struct w83781d_data *data = w83781d_update_device(dev); \
411 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
412 return sprintf(buf,"%d\n", \
413 LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
414 } else { /* TEMP1 */ \
415 return sprintf(buf,"%ld\n", (long)TEMP_FROM_REG(data->reg)); \
416 } \
417}
418show_temp_reg(temp);
419show_temp_reg(temp_max);
420show_temp_reg(temp_max_hyst);
421
422#define store_temp_reg(REG, reg) \
423static ssize_t store_temp_##reg (struct device *dev, const char *buf, size_t count, int nr) \
424{ \
425 struct i2c_client *client = to_i2c_client(dev); \
426 struct w83781d_data *data = i2c_get_clientdata(client); \
427 s32 val; \
428 \
429 val = simple_strtol(buf, NULL, 10); \
430 \
431 down(&data->update_lock); \
432 \
433 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
434 data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
435 w83781d_write_value(client, W83781D_REG_TEMP_##REG(nr), \
436 data->temp_##reg##_add[nr-2]); \
437 } else { /* TEMP1 */ \
438 data->temp_##reg = TEMP_TO_REG(val); \
439 w83781d_write_value(client, W83781D_REG_TEMP_##REG(nr), \
440 data->temp_##reg); \
441 } \
442 \
443 up(&data->update_lock); \
444 return count; \
445}
446store_temp_reg(OVER, max);
447store_temp_reg(HYST, max_hyst);
448
449#define sysfs_temp_offset(offset) \
450static ssize_t \
451show_regs_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
452{ \
453 return show_temp(dev, buf, offset); \
454} \
455static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_regs_temp_##offset, NULL);
456
457#define sysfs_temp_reg_offset(reg, offset) \
458static ssize_t show_regs_temp_##reg##offset (struct device *dev, struct device_attribute *attr, char *buf) \
459{ \
460 return show_temp_##reg (dev, buf, offset); \
461} \
462static ssize_t store_regs_temp_##reg##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
463{ \
464 return store_temp_##reg (dev, buf, count, offset); \
465} \
466static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, show_regs_temp_##reg##offset, store_regs_temp_##reg##offset);
467
468#define sysfs_temp_offsets(offset) \
469sysfs_temp_offset(offset); \
470sysfs_temp_reg_offset(max, offset); \
471sysfs_temp_reg_offset(max_hyst, offset);
472
473sysfs_temp_offsets(1);
474sysfs_temp_offsets(2);
475sysfs_temp_offsets(3);
476
477#define device_create_file_temp(client, offset) \
478do { \
479device_create_file(&client->dev, &dev_attr_temp##offset##_input); \
480device_create_file(&client->dev, &dev_attr_temp##offset##_max); \
481device_create_file(&client->dev, &dev_attr_temp##offset##_max_hyst); \
482} while (0)
483
484static ssize_t
485show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
486{
487 struct w83781d_data *data = w83781d_update_device(dev);
488 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
489}
490
491static
492DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
493#define device_create_file_vid(client) \
494device_create_file(&client->dev, &dev_attr_cpu0_vid);
495static ssize_t
496show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
497{
498 struct w83781d_data *data = w83781d_update_device(dev);
499 return sprintf(buf, "%ld\n", (long) data->vrm);
500}
501
502static ssize_t
503store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
504{
505 struct i2c_client *client = to_i2c_client(dev);
506 struct w83781d_data *data = i2c_get_clientdata(client);
507 u32 val;
508
509 val = simple_strtoul(buf, NULL, 10);
510 data->vrm = val;
511
512 return count;
513}
514
515static
516DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
517#define device_create_file_vrm(client) \
518device_create_file(&client->dev, &dev_attr_vrm);
519static ssize_t
520show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
521{
522 struct w83781d_data *data = w83781d_update_device(dev);
523 return sprintf(buf, "%u\n", data->alarms);
524}
525
526static
527DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
528#define device_create_file_alarms(client) \
529device_create_file(&client->dev, &dev_attr_alarms);
530static ssize_t show_beep_mask (struct device *dev, struct device_attribute *attr, char *buf)
531{
532 struct w83781d_data *data = w83781d_update_device(dev);
533 return sprintf(buf, "%ld\n",
534 (long)BEEP_MASK_FROM_REG(data->beep_mask, data->type));
535}
536static ssize_t show_beep_enable (struct device *dev, struct device_attribute *attr, char *buf)
537{
538 struct w83781d_data *data = w83781d_update_device(dev);
539 return sprintf(buf, "%ld\n",
540 (long)BEEP_ENABLE_FROM_REG(data->beep_enable));
541}
542
543#define BEEP_ENABLE 0 /* Store beep_enable */
544#define BEEP_MASK 1 /* Store beep_mask */
545
546static ssize_t
547store_beep_reg(struct device *dev, const char *buf, size_t count,
548 int update_mask)
549{
550 struct i2c_client *client = to_i2c_client(dev);
551 struct w83781d_data *data = i2c_get_clientdata(client);
552 u32 val, val2;
553
554 val = simple_strtoul(buf, NULL, 10);
555
556 down(&data->update_lock);
557
558 if (update_mask == BEEP_MASK) { /* We are storing beep_mask */
559 data->beep_mask = BEEP_MASK_TO_REG(val, data->type);
560 w83781d_write_value(client, W83781D_REG_BEEP_INTS1,
561 data->beep_mask & 0xff);
562
563 if ((data->type != w83781d) && (data->type != as99127f)) {
564 w83781d_write_value(client, W83781D_REG_BEEP_INTS3,
565 ((data->beep_mask) >> 16) & 0xff);
566 }
567
568 val2 = (data->beep_mask >> 8) & 0x7f;
569 } else { /* We are storing beep_enable */
570 val2 = w83781d_read_value(client, W83781D_REG_BEEP_INTS2) & 0x7f;
571 data->beep_enable = BEEP_ENABLE_TO_REG(val);
572 }
573
574 w83781d_write_value(client, W83781D_REG_BEEP_INTS2,
575 val2 | data->beep_enable << 7);
576
577 up(&data->update_lock);
578 return count;
579}
580
581#define sysfs_beep(REG, reg) \
582static ssize_t show_regs_beep_##reg (struct device *dev, struct device_attribute *attr, char *buf) \
583{ \
584 return show_beep_##reg(dev, attr, buf); \
585} \
586static ssize_t store_regs_beep_##reg (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
587{ \
588 return store_beep_reg(dev, buf, count, BEEP_##REG); \
589} \
590static DEVICE_ATTR(beep_##reg, S_IRUGO | S_IWUSR, show_regs_beep_##reg, store_regs_beep_##reg);
591
592sysfs_beep(ENABLE, enable);
593sysfs_beep(MASK, mask);
594
595#define device_create_file_beep(client) \
596do { \
597device_create_file(&client->dev, &dev_attr_beep_enable); \
598device_create_file(&client->dev, &dev_attr_beep_mask); \
599} while (0)
600
601static ssize_t
602show_fan_div_reg(struct device *dev, char *buf, int nr)
603{
604 struct w83781d_data *data = w83781d_update_device(dev);
605 return sprintf(buf, "%ld\n",
606 (long) DIV_FROM_REG(data->fan_div[nr - 1]));
607}
608
609/* Note: we save and restore the fan minimum here, because its value is
610 determined in part by the fan divisor. This follows the principle of
611 least suprise; the user doesn't expect the fan minimum to change just
612 because the divisor changed. */
613static ssize_t
614store_fan_div_reg(struct device *dev, const char *buf, size_t count, int nr)
615{
616 struct i2c_client *client = to_i2c_client(dev);
617 struct w83781d_data *data = i2c_get_clientdata(client);
618 unsigned long min;
619 u8 reg;
620 unsigned long val = simple_strtoul(buf, NULL, 10);
621
622 down(&data->update_lock);
623
624 /* Save fan_min */
625 min = FAN_FROM_REG(data->fan_min[nr],
626 DIV_FROM_REG(data->fan_div[nr]));
627
628 data->fan_div[nr] = DIV_TO_REG(val, data->type);
629
630 reg = (w83781d_read_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
631 & (nr==0 ? 0xcf : 0x3f))
632 | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
633 w83781d_write_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
634
635 /* w83781d and as99127f don't have extended divisor bits */
636 if (data->type != w83781d && data->type != as99127f) {
637 reg = (w83781d_read_value(client, W83781D_REG_VBAT)
638 & ~(1 << (5 + nr)))
639 | ((data->fan_div[nr] & 0x04) << (3 + nr));
640 w83781d_write_value(client, W83781D_REG_VBAT, reg);
641 }
642
643 /* Restore fan_min */
644 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
645 w83781d_write_value(client, W83781D_REG_FAN_MIN(nr+1), data->fan_min[nr]);
646
647 up(&data->update_lock);
648 return count;
649}
650
651#define sysfs_fan_div(offset) \
652static ssize_t show_regs_fan_div_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
653{ \
654 return show_fan_div_reg(dev, buf, offset); \
655} \
656static ssize_t store_regs_fan_div_##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
657{ \
658 return store_fan_div_reg(dev, buf, count, offset - 1); \
659} \
660static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, show_regs_fan_div_##offset, store_regs_fan_div_##offset);
661
662sysfs_fan_div(1);
663sysfs_fan_div(2);
664sysfs_fan_div(3);
665
666#define device_create_file_fan_div(client, offset) \
667do { \
668device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
669} while (0)
670
671static ssize_t
672show_pwm_reg(struct device *dev, char *buf, int nr)
673{
674 struct w83781d_data *data = w83781d_update_device(dev);
675 return sprintf(buf, "%ld\n", (long) PWM_FROM_REG(data->pwm[nr - 1]));
676}
677
678static ssize_t
679show_pwmenable_reg(struct device *dev, char *buf, int nr)
680{
681 struct w83781d_data *data = w83781d_update_device(dev);
682 return sprintf(buf, "%ld\n", (long) data->pwmenable[nr - 1]);
683}
684
685static ssize_t
686store_pwm_reg(struct device *dev, const char *buf, size_t count, int nr)
687{
688 struct i2c_client *client = to_i2c_client(dev);
689 struct w83781d_data *data = i2c_get_clientdata(client);
690 u32 val;
691
692 val = simple_strtoul(buf, NULL, 10);
693
694 down(&data->update_lock);
695 data->pwm[nr - 1] = PWM_TO_REG(val);
696 w83781d_write_value(client, W83781D_REG_PWM(nr), data->pwm[nr - 1]);
697 up(&data->update_lock);
698 return count;
699}
700
701static ssize_t
702store_pwmenable_reg(struct device *dev, const char *buf, size_t count, int nr)
703{
704 struct i2c_client *client = to_i2c_client(dev);
705 struct w83781d_data *data = i2c_get_clientdata(client);
706 u32 val, reg;
707
708 val = simple_strtoul(buf, NULL, 10);
709
710 down(&data->update_lock);
711
712 switch (val) {
713 case 0:
714 case 1:
715 reg = w83781d_read_value(client, W83781D_REG_PWMCLK12);
716 w83781d_write_value(client, W83781D_REG_PWMCLK12,
717 (reg & 0xf7) | (val << 3));
718
719 reg = w83781d_read_value(client, W83781D_REG_BEEP_CONFIG);
720 w83781d_write_value(client, W83781D_REG_BEEP_CONFIG,
721 (reg & 0xef) | (!val << 4));
722
723 data->pwmenable[nr - 1] = val;
724 break;
725
726 default:
727 up(&data->update_lock);
728 return -EINVAL;
729 }
730
731 up(&data->update_lock);
732 return count;
733}
734
735#define sysfs_pwm(offset) \
736static ssize_t show_regs_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
737{ \
738 return show_pwm_reg(dev, buf, offset); \
739} \
740static ssize_t store_regs_pwm_##offset (struct device *dev, struct device_attribute *attr, \
741 const char *buf, size_t count) \
742{ \
743 return store_pwm_reg(dev, buf, count, offset); \
744} \
745static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
746 show_regs_pwm_##offset, store_regs_pwm_##offset);
747
748#define sysfs_pwmenable(offset) \
749static ssize_t show_regs_pwmenable_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
750{ \
751 return show_pwmenable_reg(dev, buf, offset); \
752} \
753static ssize_t store_regs_pwmenable_##offset (struct device *dev, struct device_attribute *attr, \
754 const char *buf, size_t count) \
755{ \
756 return store_pwmenable_reg(dev, buf, count, offset); \
757} \
758static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
759 show_regs_pwmenable_##offset, store_regs_pwmenable_##offset);
760
761sysfs_pwm(1);
762sysfs_pwm(2);
763sysfs_pwmenable(2); /* only PWM2 can be enabled/disabled */
764sysfs_pwm(3);
765sysfs_pwm(4);
766
767#define device_create_file_pwm(client, offset) \
768do { \
769device_create_file(&client->dev, &dev_attr_pwm##offset); \
770} while (0)
771
772#define device_create_file_pwmenable(client, offset) \
773do { \
774device_create_file(&client->dev, &dev_attr_pwm##offset##_enable); \
775} while (0)
776
777static ssize_t
778show_sensor_reg(struct device *dev, char *buf, int nr)
779{
780 struct w83781d_data *data = w83781d_update_device(dev);
781 return sprintf(buf, "%ld\n", (long) data->sens[nr - 1]);
782}
783
784static ssize_t
785store_sensor_reg(struct device *dev, const char *buf, size_t count, int nr)
786{
787 struct i2c_client *client = to_i2c_client(dev);
788 struct w83781d_data *data = i2c_get_clientdata(client);
789 u32 val, tmp;
790
791 val = simple_strtoul(buf, NULL, 10);
792
793 down(&data->update_lock);
794
795 switch (val) {
796 case 1: /* PII/Celeron diode */
797 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
798 w83781d_write_value(client, W83781D_REG_SCFG1,
799 tmp | BIT_SCFG1[nr - 1]);
800 tmp = w83781d_read_value(client, W83781D_REG_SCFG2);
801 w83781d_write_value(client, W83781D_REG_SCFG2,
802 tmp | BIT_SCFG2[nr - 1]);
803 data->sens[nr - 1] = val;
804 break;
805 case 2: /* 3904 */
806 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
807 w83781d_write_value(client, W83781D_REG_SCFG1,
808 tmp | BIT_SCFG1[nr - 1]);
809 tmp = w83781d_read_value(client, W83781D_REG_SCFG2);
810 w83781d_write_value(client, W83781D_REG_SCFG2,
811 tmp & ~BIT_SCFG2[nr - 1]);
812 data->sens[nr - 1] = val;
813 break;
814 case W83781D_DEFAULT_BETA: /* thermistor */
815 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
816 w83781d_write_value(client, W83781D_REG_SCFG1,
817 tmp & ~BIT_SCFG1[nr - 1]);
818 data->sens[nr - 1] = val;
819 break;
820 default:
821 dev_err(dev, "Invalid sensor type %ld; must be 1, 2, or %d\n",
822 (long) val, W83781D_DEFAULT_BETA);
823 break;
824 }
825
826 up(&data->update_lock);
827 return count;
828}
829
830#define sysfs_sensor(offset) \
831static ssize_t show_regs_sensor_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
832{ \
833 return show_sensor_reg(dev, buf, offset); \
834} \
835static ssize_t store_regs_sensor_##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
836{ \
837 return store_sensor_reg(dev, buf, count, offset); \
838} \
839static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, show_regs_sensor_##offset, store_regs_sensor_##offset);
840
841sysfs_sensor(1);
842sysfs_sensor(2);
843sysfs_sensor(3);
844
845#define device_create_file_sensor(client, offset) \
846do { \
847device_create_file(&client->dev, &dev_attr_temp##offset##_type); \
848} while (0)
849
850/* This function is called when:
851 * w83781d_driver is inserted (when this module is loaded), for each
852 available adapter
853 * when a new adapter is inserted (and w83781d_driver is still present) */
854static int
855w83781d_attach_adapter(struct i2c_adapter *adapter)
856{
857 if (!(adapter->class & I2C_CLASS_HWMON))
858 return 0;
859 return i2c_detect(adapter, &addr_data, w83781d_detect);
860}
861
862/* Assumes that adapter is of I2C, not ISA variety.
863 * OTHERWISE DON'T CALL THIS
864 */
865static int
866w83781d_detect_subclients(struct i2c_adapter *adapter, int address, int kind,
867 struct i2c_client *new_client)
868{
869 int i, val1 = 0, id;
870 int err;
871 const char *client_name = "";
872 struct w83781d_data *data = i2c_get_clientdata(new_client);
873
874 data->lm75[0] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
875 if (!(data->lm75[0])) {
876 err = -ENOMEM;
877 goto ERROR_SC_0;
878 }
879 memset(data->lm75[0], 0x00, sizeof (struct i2c_client));
880
881 id = i2c_adapter_id(adapter);
882
883 if (force_subclients[0] == id && force_subclients[1] == address) {
884 for (i = 2; i <= 3; i++) {
885 if (force_subclients[i] < 0x48 ||
886 force_subclients[i] > 0x4f) {
887 dev_err(&new_client->dev, "Invalid subclient "
888 "address %d; must be 0x48-0x4f\n",
889 force_subclients[i]);
890 err = -EINVAL;
891 goto ERROR_SC_1;
892 }
893 }
894 w83781d_write_value(new_client, W83781D_REG_I2C_SUBADDR,
895 (force_subclients[2] & 0x07) |
896 ((force_subclients[3] & 0x07) << 4));
897 data->lm75[0]->addr = force_subclients[2];
898 } else {
899 val1 = w83781d_read_value(new_client, W83781D_REG_I2C_SUBADDR);
900 data->lm75[0]->addr = 0x48 + (val1 & 0x07);
901 }
902
903 if (kind != w83783s) {
904
905 data->lm75[1] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
906 if (!(data->lm75[1])) {
907 err = -ENOMEM;
908 goto ERROR_SC_1;
909 }
910 memset(data->lm75[1], 0x0, sizeof(struct i2c_client));
911
912 if (force_subclients[0] == id &&
913 force_subclients[1] == address) {
914 data->lm75[1]->addr = force_subclients[3];
915 } else {
916 data->lm75[1]->addr = 0x48 + ((val1 >> 4) & 0x07);
917 }
918 if (data->lm75[0]->addr == data->lm75[1]->addr) {
919 dev_err(&new_client->dev,
920 "Duplicate addresses 0x%x for subclients.\n",
921 data->lm75[0]->addr);
922 err = -EBUSY;
923 goto ERROR_SC_2;
924 }
925 }
926
927 if (kind == w83781d)
928 client_name = "w83781d subclient";
929 else if (kind == w83782d)
930 client_name = "w83782d subclient";
931 else if (kind == w83783s)
932 client_name = "w83783s subclient";
933 else if (kind == w83627hf)
934 client_name = "w83627hf subclient";
935 else if (kind == as99127f)
936 client_name = "as99127f subclient";
937
938 for (i = 0; i <= 1; i++) {
939 /* store all data in w83781d */
940 i2c_set_clientdata(data->lm75[i], NULL);
941 data->lm75[i]->adapter = adapter;
942 data->lm75[i]->driver = &w83781d_driver;
943 data->lm75[i]->flags = 0;
944 strlcpy(data->lm75[i]->name, client_name,
945 I2C_NAME_SIZE);
946 if ((err = i2c_attach_client(data->lm75[i]))) {
947 dev_err(&new_client->dev, "Subclient %d "
948 "registration at address 0x%x "
949 "failed.\n", i, data->lm75[i]->addr);
950 if (i == 1)
951 goto ERROR_SC_3;
952 goto ERROR_SC_2;
953 }
954 if (kind == w83783s)
955 break;
956 }
957
958 return 0;
959
960/* Undo inits in case of errors */
961ERROR_SC_3:
962 i2c_detach_client(data->lm75[0]);
963ERROR_SC_2:
964 if (NULL != data->lm75[1])
965 kfree(data->lm75[1]);
966ERROR_SC_1:
967 if (NULL != data->lm75[0])
968 kfree(data->lm75[0]);
969ERROR_SC_0:
970 return err;
971}
972
973static int
974w83781d_detect(struct i2c_adapter *adapter, int address, int kind)
975{
976 int i = 0, val1 = 0, val2;
977 struct i2c_client *new_client;
978 struct w83781d_data *data;
979 int err;
980 const char *client_name = "";
981 int is_isa = i2c_is_isa_adapter(adapter);
982 enum vendor { winbond, asus } vendid;
983
984 if (!is_isa
985 && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
986 err = -EINVAL;
987 goto ERROR0;
988 }
989
990 /* Prevent users from forcing a kind for a bus it isn't supposed
991 to possibly be on */
992 if (is_isa && (kind == as99127f || kind == w83783s)) {
993 dev_err(&adapter->dev,
994 "Cannot force I2C-only chip for ISA address 0x%02x.\n",
995 address);
996 err = -EINVAL;
997 goto ERROR0;
998 }
999
1000 if (is_isa)
1001 if (!request_region(address, W83781D_EXTENT,
1002 w83781d_driver.name)) {
1003 dev_dbg(&adapter->dev, "Request of region "
1004 "0x%x-0x%x for w83781d failed\n", address,
1005 address + W83781D_EXTENT - 1);
1006 err = -EBUSY;
1007 goto ERROR0;
1008 }
1009
1010 /* Probe whether there is anything available on this address. Already
1011 done for SMBus clients */
1012 if (kind < 0) {
1013 if (is_isa) {
1014
1015#define REALLY_SLOW_IO
1016 /* We need the timeouts for at least some LM78-like
1017 chips. But only if we read 'undefined' registers. */
1018 i = inb_p(address + 1);
1019 if (inb_p(address + 2) != i
1020 || inb_p(address + 3) != i
1021 || inb_p(address + 7) != i) {
1022 dev_dbg(&adapter->dev, "Detection of w83781d "
1023 "chip failed at step 1\n");
1024 err = -ENODEV;
1025 goto ERROR1;
1026 }
1027#undef REALLY_SLOW_IO
1028
1029 /* Let's just hope nothing breaks here */
1030 i = inb_p(address + 5) & 0x7f;
1031 outb_p(~i & 0x7f, address + 5);
1032 val2 = inb_p(address + 5) & 0x7f;
1033 if (val2 != (~i & 0x7f)) {
1034 outb_p(i, address + 5);
1035 dev_dbg(&adapter->dev, "Detection of w83781d "
1036 "chip failed at step 2 (0x%x != "
1037 "0x%x at 0x%x)\n", val2, ~i & 0x7f,
1038 address + 5);
1039 err = -ENODEV;
1040 goto ERROR1;
1041 }
1042 }
1043 }
1044
1045 /* OK. For now, we presume we have a valid client. We now create the
1046 client structure, even though we cannot fill it completely yet.
1047 But it allows us to access w83781d_{read,write}_value. */
1048
1049 if (!(data = kmalloc(sizeof(struct w83781d_data), GFP_KERNEL))) {
1050 err = -ENOMEM;
1051 goto ERROR1;
1052 }
1053 memset(data, 0, sizeof(struct w83781d_data));
1054
1055 new_client = &data->client;
1056 i2c_set_clientdata(new_client, data);
1057 new_client->addr = address;
1058 init_MUTEX(&data->lock);
1059 new_client->adapter = adapter;
1060 new_client->driver = &w83781d_driver;
1061 new_client->flags = 0;
1062
1063 /* Now, we do the remaining detection. */
1064
1065 /* The w8378?d may be stuck in some other bank than bank 0. This may
1066 make reading other information impossible. Specify a force=... or
1067 force_*=... parameter, and the Winbond will be reset to the right
1068 bank. */
1069 if (kind < 0) {
1070 if (w83781d_read_value(new_client, W83781D_REG_CONFIG) & 0x80) {
1071 dev_dbg(&new_client->dev, "Detection failed at step "
1072 "3\n");
1073 err = -ENODEV;
1074 goto ERROR2;
1075 }
1076 val1 = w83781d_read_value(new_client, W83781D_REG_BANK);
1077 val2 = w83781d_read_value(new_client, W83781D_REG_CHIPMAN);
1078 /* Check for Winbond or Asus ID if in bank 0 */
1079 if ((!(val1 & 0x07)) &&
1080 (((!(val1 & 0x80)) && (val2 != 0xa3) && (val2 != 0xc3))
1081 || ((val1 & 0x80) && (val2 != 0x5c) && (val2 != 0x12)))) {
1082 dev_dbg(&new_client->dev, "Detection failed at step "
1083 "4\n");
1084 err = -ENODEV;
1085 goto ERROR2;
1086 }
1087 /* If Winbond SMBus, check address at 0x48.
1088 Asus doesn't support, except for as99127f rev.2 */
1089 if ((!is_isa) && (((!(val1 & 0x80)) && (val2 == 0xa3)) ||
1090 ((val1 & 0x80) && (val2 == 0x5c)))) {
1091 if (w83781d_read_value
1092 (new_client, W83781D_REG_I2C_ADDR) != address) {
1093 dev_dbg(&new_client->dev, "Detection failed "
1094 "at step 5\n");
1095 err = -ENODEV;
1096 goto ERROR2;
1097 }
1098 }
1099 }
1100
1101 /* We have either had a force parameter, or we have already detected the
1102 Winbond. Put it now into bank 0 and Vendor ID High Byte */
1103 w83781d_write_value(new_client, W83781D_REG_BANK,
1104 (w83781d_read_value(new_client,
1105 W83781D_REG_BANK) & 0x78) |
1106 0x80);
1107
1108 /* Determine the chip type. */
1109 if (kind <= 0) {
1110 /* get vendor ID */
1111 val2 = w83781d_read_value(new_client, W83781D_REG_CHIPMAN);
1112 if (val2 == 0x5c)
1113 vendid = winbond;
1114 else if (val2 == 0x12)
1115 vendid = asus;
1116 else {
1117 dev_dbg(&new_client->dev, "Chip was made by neither "
1118 "Winbond nor Asus?\n");
1119 err = -ENODEV;
1120 goto ERROR2;
1121 }
1122
1123 val1 = w83781d_read_value(new_client, W83781D_REG_WCHIPID);
1124 if ((val1 == 0x10 || val1 == 0x11) && vendid == winbond)
1125 kind = w83781d;
1126 else if (val1 == 0x30 && vendid == winbond)
1127 kind = w83782d;
1128 else if (val1 == 0x40 && vendid == winbond && !is_isa
1129 && address == 0x2d)
1130 kind = w83783s;
1131 else if (val1 == 0x21 && vendid == winbond)
1132 kind = w83627hf;
1133 else if (val1 == 0x31 && !is_isa && address >= 0x28)
1134 kind = as99127f;
1135 else {
1136 if (kind == 0)
1137 dev_warn(&new_client->dev, "Ignoring 'force' "
1138 "parameter for unknown chip at "
1139 "adapter %d, address 0x%02x\n",
1140 i2c_adapter_id(adapter), address);
1141 err = -EINVAL;
1142 goto ERROR2;
1143 }
1144 }
1145
1146 if (kind == w83781d) {
1147 client_name = "w83781d";
1148 } else if (kind == w83782d) {
1149 client_name = "w83782d";
1150 } else if (kind == w83783s) {
1151 client_name = "w83783s";
1152 } else if (kind == w83627hf) {
1153 client_name = "w83627hf";
1154 } else if (kind == as99127f) {
1155 client_name = "as99127f";
1156 }
1157
1158 /* Fill in the remaining client fields and put into the global list */
1159 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
1160 data->type = kind;
1161
1162 data->valid = 0;
1163 init_MUTEX(&data->update_lock);
1164
1165 /* Tell the I2C layer a new client has arrived */
1166 if ((err = i2c_attach_client(new_client)))
1167 goto ERROR2;
1168
1169 /* attach secondary i2c lm75-like clients */
1170 if (!is_isa) {
1171 if ((err = w83781d_detect_subclients(adapter, address,
1172 kind, new_client)))
1173 goto ERROR3;
1174 } else {
1175 data->lm75[0] = NULL;
1176 data->lm75[1] = NULL;
1177 }
1178
1179 /* Initialize the chip */
1180 w83781d_init_client(new_client);
1181
1182 /* A few vars need to be filled upon startup */
1183 for (i = 1; i <= 3; i++) {
1184 data->fan_min[i - 1] = w83781d_read_value(new_client,
1185 W83781D_REG_FAN_MIN(i));
1186 }
1187 if (kind != w83781d && kind != as99127f)
1188 for (i = 0; i < 4; i++)
1189 data->pwmenable[i] = 1;
1190
1191 /* Register sysfs hooks */
1192 device_create_file_in(new_client, 0);
1193 if (kind != w83783s)
1194 device_create_file_in(new_client, 1);
1195 device_create_file_in(new_client, 2);
1196 device_create_file_in(new_client, 3);
1197 device_create_file_in(new_client, 4);
1198 device_create_file_in(new_client, 5);
1199 device_create_file_in(new_client, 6);
1200 if (kind != as99127f && kind != w83781d && kind != w83783s) {
1201 device_create_file_in(new_client, 7);
1202 device_create_file_in(new_client, 8);
1203 }
1204
1205 device_create_file_fan(new_client, 1);
1206 device_create_file_fan(new_client, 2);
1207 device_create_file_fan(new_client, 3);
1208
1209 device_create_file_temp(new_client, 1);
1210 device_create_file_temp(new_client, 2);
1211 if (kind != w83783s)
1212 device_create_file_temp(new_client, 3);
1213
1214 device_create_file_vid(new_client);
1215 device_create_file_vrm(new_client);
1216
1217 device_create_file_fan_div(new_client, 1);
1218 device_create_file_fan_div(new_client, 2);
1219 device_create_file_fan_div(new_client, 3);
1220
1221 device_create_file_alarms(new_client);
1222
1223 device_create_file_beep(new_client);
1224
1225 if (kind != w83781d && kind != as99127f) {
1226 device_create_file_pwm(new_client, 1);
1227 device_create_file_pwm(new_client, 2);
1228 device_create_file_pwmenable(new_client, 2);
1229 }
1230 if (kind == w83782d && !is_isa) {
1231 device_create_file_pwm(new_client, 3);
1232 device_create_file_pwm(new_client, 4);
1233 }
1234
1235 if (kind != as99127f && kind != w83781d) {
1236 device_create_file_sensor(new_client, 1);
1237 device_create_file_sensor(new_client, 2);
1238 if (kind != w83783s)
1239 device_create_file_sensor(new_client, 3);
1240 }
1241
1242 return 0;
1243
1244ERROR3:
1245 i2c_detach_client(new_client);
1246ERROR2:
1247 kfree(data);
1248ERROR1:
1249 if (is_isa)
1250 release_region(address, W83781D_EXTENT);
1251ERROR0:
1252 return err;
1253}
1254
1255static int
1256w83781d_detach_client(struct i2c_client *client)
1257{
1258 int err;
1259
1260 if (i2c_is_isa_client(client))
1261 release_region(client->addr, W83781D_EXTENT);
1262
1263 if ((err = i2c_detach_client(client))) {
1264 dev_err(&client->dev,
1265 "Client deregistration failed, client not detached.\n");
1266 return err;
1267 }
1268
1269 if (i2c_get_clientdata(client)==NULL) {
1270 /* subclients */
1271 kfree(client);
1272 } else {
1273 /* main client */
1274 kfree(i2c_get_clientdata(client));
1275 }
1276
1277 return 0;
1278}
1279
1280/* The SMBus locks itself, usually, but nothing may access the Winbond between
1281 bank switches. ISA access must always be locked explicitly!
1282 We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
1283 would slow down the W83781D access and should not be necessary.
1284 There are some ugly typecasts here, but the good news is - they should
1285 nowhere else be necessary! */
1286static int
1287w83781d_read_value(struct i2c_client *client, u16 reg)
1288{
1289 struct w83781d_data *data = i2c_get_clientdata(client);
1290 int res, word_sized, bank;
1291 struct i2c_client *cl;
1292
1293 down(&data->lock);
1294 if (i2c_is_isa_client(client)) {
1295 word_sized = (((reg & 0xff00) == 0x100)
1296 || ((reg & 0xff00) == 0x200))
1297 && (((reg & 0x00ff) == 0x50)
1298 || ((reg & 0x00ff) == 0x53)
1299 || ((reg & 0x00ff) == 0x55));
1300 if (reg & 0xff00) {
1301 outb_p(W83781D_REG_BANK,
1302 client->addr + W83781D_ADDR_REG_OFFSET);
1303 outb_p(reg >> 8,
1304 client->addr + W83781D_DATA_REG_OFFSET);
1305 }
1306 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1307 res = inb_p(client->addr + W83781D_DATA_REG_OFFSET);
1308 if (word_sized) {
1309 outb_p((reg & 0xff) + 1,
1310 client->addr + W83781D_ADDR_REG_OFFSET);
1311 res =
1312 (res << 8) + inb_p(client->addr +
1313 W83781D_DATA_REG_OFFSET);
1314 }
1315 if (reg & 0xff00) {
1316 outb_p(W83781D_REG_BANK,
1317 client->addr + W83781D_ADDR_REG_OFFSET);
1318 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1319 }
1320 } else {
1321 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 res = i2c_smbus_read_byte_data(client, reg & 0xff);
1328 } else {
1329 /* switch to subclient */
1330 cl = data->lm75[bank - 1];
1331 /* convert from ISA to LM75 I2C addresses */
1332 switch (reg & 0xff) {
1333 case 0x50: /* TEMP */
1334 res = swab16(i2c_smbus_read_word_data(cl, 0));
1335 break;
1336 case 0x52: /* CONFIG */
1337 res = i2c_smbus_read_byte_data(cl, 1);
1338 break;
1339 case 0x53: /* HYST */
1340 res = swab16(i2c_smbus_read_word_data(cl, 2));
1341 break;
1342 case 0x55: /* OVER */
1343 default:
1344 res = swab16(i2c_smbus_read_word_data(cl, 3));
1345 break;
1346 }
1347 }
1348 if (bank > 2)
1349 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1350 }
1351 up(&data->lock);
1352 return res;
1353}
1354
1355static int
1356w83781d_write_value(struct i2c_client *client, u16 reg, u16 value)
1357{
1358 struct w83781d_data *data = i2c_get_clientdata(client);
1359 int word_sized, bank;
1360 struct i2c_client *cl;
1361
1362 down(&data->lock);
1363 if (i2c_is_isa_client(client)) {
1364 word_sized = (((reg & 0xff00) == 0x100)
1365 || ((reg & 0xff00) == 0x200))
1366 && (((reg & 0x00ff) == 0x53)
1367 || ((reg & 0x00ff) == 0x55));
1368 if (reg & 0xff00) {
1369 outb_p(W83781D_REG_BANK,
1370 client->addr + W83781D_ADDR_REG_OFFSET);
1371 outb_p(reg >> 8,
1372 client->addr + W83781D_DATA_REG_OFFSET);
1373 }
1374 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1375 if (word_sized) {
1376 outb_p(value >> 8,
1377 client->addr + W83781D_DATA_REG_OFFSET);
1378 outb_p((reg & 0xff) + 1,
1379 client->addr + W83781D_ADDR_REG_OFFSET);
1380 }
1381 outb_p(value & 0xff, client->addr + W83781D_DATA_REG_OFFSET);
1382 if (reg & 0xff00) {
1383 outb_p(W83781D_REG_BANK,
1384 client->addr + W83781D_ADDR_REG_OFFSET);
1385 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1386 }
1387 } else {
1388 bank = (reg >> 8) & 0x0f;
1389 if (bank > 2)
1390 /* switch banks */
1391 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1392 bank);
1393 if (bank == 0 || bank > 2) {
1394 i2c_smbus_write_byte_data(client, reg & 0xff,
1395 value & 0xff);
1396 } else {
1397 /* switch to subclient */
1398 cl = data->lm75[bank - 1];
1399 /* convert from ISA to LM75 I2C addresses */
1400 switch (reg & 0xff) {
1401 case 0x52: /* CONFIG */
1402 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
1403 break;
1404 case 0x53: /* HYST */
1405 i2c_smbus_write_word_data(cl, 2, swab16(value));
1406 break;
1407 case 0x55: /* OVER */
1408 i2c_smbus_write_word_data(cl, 3, swab16(value));
1409 break;
1410 }
1411 }
1412 if (bank > 2)
1413 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1414 }
1415 up(&data->lock);
1416 return 0;
1417}
1418
1419/* Called when we have found a new W83781D. It should set limits, etc. */
1420static void
1421w83781d_init_client(struct i2c_client *client)
1422{
1423 struct w83781d_data *data = i2c_get_clientdata(client);
1424 int i, p;
1425 int type = data->type;
1426 u8 tmp;
1427
1428 if (init && type != as99127f) { /* this resets registers we don't have
1429 documentation for on the as99127f */
1430 /* save these registers */
1431 i = w83781d_read_value(client, W83781D_REG_BEEP_CONFIG);
1432 p = w83781d_read_value(client, W83781D_REG_PWMCLK12);
1433 /* Reset all except Watchdog values and last conversion values
1434 This sets fan-divs to 2, among others */
1435 w83781d_write_value(client, W83781D_REG_CONFIG, 0x80);
1436 /* Restore the registers and disable power-on abnormal beep.
1437 This saves FAN 1/2/3 input/output values set by BIOS. */
1438 w83781d_write_value(client, W83781D_REG_BEEP_CONFIG, i | 0x80);
1439 w83781d_write_value(client, W83781D_REG_PWMCLK12, p);
1440 /* Disable master beep-enable (reset turns it on).
1441 Individual beep_mask should be reset to off but for some reason
1442 disabling this bit helps some people not get beeped */
1443 w83781d_write_value(client, W83781D_REG_BEEP_INTS2, 0);
1444 }
1445
1446 data->vrm = i2c_which_vrm();
1447
1448 if ((type != w83781d) && (type != as99127f)) {
1449 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
1450 for (i = 1; i <= 3; i++) {
1451 if (!(tmp & BIT_SCFG1[i - 1])) {
1452 data->sens[i - 1] = W83781D_DEFAULT_BETA;
1453 } else {
1454 if (w83781d_read_value
1455 (client,
1456 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1457 data->sens[i - 1] = 1;
1458 else
1459 data->sens[i - 1] = 2;
1460 }
1461 if (type == w83783s && i == 2)
1462 break;
1463 }
1464 }
1465
1466 if (init && type != as99127f) {
1467 /* Enable temp2 */
1468 tmp = w83781d_read_value(client, W83781D_REG_TEMP2_CONFIG);
1469 if (tmp & 0x01) {
1470 dev_warn(&client->dev, "Enabling temp2, readings "
1471 "might not make sense\n");
1472 w83781d_write_value(client, W83781D_REG_TEMP2_CONFIG,
1473 tmp & 0xfe);
1474 }
1475
1476 /* Enable temp3 */
1477 if (type != w83783s) {
1478 tmp = w83781d_read_value(client,
1479 W83781D_REG_TEMP3_CONFIG);
1480 if (tmp & 0x01) {
1481 dev_warn(&client->dev, "Enabling temp3, "
1482 "readings might not make sense\n");
1483 w83781d_write_value(client,
1484 W83781D_REG_TEMP3_CONFIG, tmp & 0xfe);
1485 }
1486 }
1487
1488 if (type != w83781d) {
1489 /* enable comparator mode for temp2 and temp3 so
1490 alarm indication will work correctly */
1491 i = w83781d_read_value(client, W83781D_REG_IRQ);
1492 if (!(i & 0x40))
1493 w83781d_write_value(client, W83781D_REG_IRQ,
1494 i | 0x40);
1495 }
1496 }
1497
1498 /* Start monitoring */
1499 w83781d_write_value(client, W83781D_REG_CONFIG,
1500 (w83781d_read_value(client,
1501 W83781D_REG_CONFIG) & 0xf7)
1502 | 0x01);
1503}
1504
1505static struct w83781d_data *w83781d_update_device(struct device *dev)
1506{
1507 struct i2c_client *client = to_i2c_client(dev);
1508 struct w83781d_data *data = i2c_get_clientdata(client);
1509 int i;
1510
1511 down(&data->update_lock);
1512
1513 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1514 || !data->valid) {
1515 dev_dbg(dev, "Starting device update\n");
1516
1517 for (i = 0; i <= 8; i++) {
1518 if (data->type == w83783s && i == 1)
1519 continue; /* 783S has no in1 */
1520 data->in[i] =
1521 w83781d_read_value(client, W83781D_REG_IN(i));
1522 data->in_min[i] =
1523 w83781d_read_value(client, W83781D_REG_IN_MIN(i));
1524 data->in_max[i] =
1525 w83781d_read_value(client, W83781D_REG_IN_MAX(i));
1526 if ((data->type != w83782d)
1527 && (data->type != w83627hf) && (i == 6))
1528 break;
1529 }
1530 for (i = 1; i <= 3; i++) {
1531 data->fan[i - 1] =
1532 w83781d_read_value(client, W83781D_REG_FAN(i));
1533 data->fan_min[i - 1] =
1534 w83781d_read_value(client, W83781D_REG_FAN_MIN(i));
1535 }
1536 if (data->type != w83781d && data->type != as99127f) {
1537 for (i = 1; i <= 4; i++) {
1538 data->pwm[i - 1] =
1539 w83781d_read_value(client,
1540 W83781D_REG_PWM(i));
1541 if ((data->type != w83782d
1542 || i2c_is_isa_client(client))
1543 && i == 2)
1544 break;
1545 }
1546 /* Only PWM2 can be disabled */
1547 data->pwmenable[1] = (w83781d_read_value(client,
1548 W83781D_REG_PWMCLK12) & 0x08) >> 3;
1549 }
1550
1551 data->temp = w83781d_read_value(client, W83781D_REG_TEMP(1));
1552 data->temp_max =
1553 w83781d_read_value(client, W83781D_REG_TEMP_OVER(1));
1554 data->temp_max_hyst =
1555 w83781d_read_value(client, W83781D_REG_TEMP_HYST(1));
1556 data->temp_add[0] =
1557 w83781d_read_value(client, W83781D_REG_TEMP(2));
1558 data->temp_max_add[0] =
1559 w83781d_read_value(client, W83781D_REG_TEMP_OVER(2));
1560 data->temp_max_hyst_add[0] =
1561 w83781d_read_value(client, W83781D_REG_TEMP_HYST(2));
1562 if (data->type != w83783s) {
1563 data->temp_add[1] =
1564 w83781d_read_value(client, W83781D_REG_TEMP(3));
1565 data->temp_max_add[1] =
1566 w83781d_read_value(client,
1567 W83781D_REG_TEMP_OVER(3));
1568 data->temp_max_hyst_add[1] =
1569 w83781d_read_value(client,
1570 W83781D_REG_TEMP_HYST(3));
1571 }
1572 i = w83781d_read_value(client, W83781D_REG_VID_FANDIV);
1573 data->vid = i & 0x0f;
1574 data->vid |= (w83781d_read_value(client,
1575 W83781D_REG_CHIPID) & 0x01) << 4;
1576 data->fan_div[0] = (i >> 4) & 0x03;
1577 data->fan_div[1] = (i >> 6) & 0x03;
1578 data->fan_div[2] = (w83781d_read_value(client,
1579 W83781D_REG_PIN) >> 6) & 0x03;
1580 if ((data->type != w83781d) && (data->type != as99127f)) {
1581 i = w83781d_read_value(client, W83781D_REG_VBAT);
1582 data->fan_div[0] |= (i >> 3) & 0x04;
1583 data->fan_div[1] |= (i >> 4) & 0x04;
1584 data->fan_div[2] |= (i >> 5) & 0x04;
1585 }
1586 data->alarms =
1587 w83781d_read_value(client,
1588 W83781D_REG_ALARM1) +
1589 (w83781d_read_value(client, W83781D_REG_ALARM2) << 8);
1590 if ((data->type == w83782d) || (data->type == w83627hf)) {
1591 data->alarms |=
1592 w83781d_read_value(client,
1593 W83781D_REG_ALARM3) << 16;
1594 }
1595 i = w83781d_read_value(client, W83781D_REG_BEEP_INTS2);
1596 data->beep_enable = i >> 7;
1597 data->beep_mask = ((i & 0x7f) << 8) +
1598 w83781d_read_value(client, W83781D_REG_BEEP_INTS1);
1599 if ((data->type != w83781d) && (data->type != as99127f)) {
1600 data->beep_mask |=
1601 w83781d_read_value(client,
1602 W83781D_REG_BEEP_INTS3) << 16;
1603 }
1604 data->last_updated = jiffies;
1605 data->valid = 1;
1606 }
1607
1608 up(&data->update_lock);
1609
1610 return data;
1611}
1612
1613static int __init
1614sensors_w83781d_init(void)
1615{
1616 return i2c_add_driver(&w83781d_driver);
1617}
1618
1619static void __exit
1620sensors_w83781d_exit(void)
1621{
1622 i2c_del_driver(&w83781d_driver);
1623}
1624
1625MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
1626 "Philip Edelbrock <phil@netroedge.com>, "
1627 "and Mark Studebaker <mdsxyz123@yahoo.com>");
1628MODULE_DESCRIPTION("W83781D driver");
1629MODULE_LICENSE("GPL");
1630
1631module_init(sensors_w83781d_init);
1632module_exit(sensors_w83781d_exit);
diff --git a/drivers/hwmon/w83l785ts.c b/drivers/hwmon/w83l785ts.c
new file mode 100644
index 000000000000..4469d52aba4c
--- /dev/null
+++ b/drivers/hwmon/w83l785ts.c
@@ -0,0 +1,328 @@
1/*
2 * w83l785ts.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 2003-2004 Jean Delvare <khali@linux-fr.org>
5 *
6 * Inspired from the lm83 driver. The W83L785TS-S is a sensor chip made
7 * by Winbond. It reports a single external temperature with a 1 deg
8 * resolution and a 3 deg accuracy. Datasheet can be obtained from
9 * Winbond's website at:
10 * http://www.winbond-usa.com/products/winbond_products/pdfs/PCIC/W83L785TS-S.pdf
11 *
12 * Ported to Linux 2.6 by Wolfgang Ziegler <nuppla@gmx.at> and Jean Delvare
13 * <khali@linux-fr.org>.
14 *
15 * Thanks to James Bolt <james@evilpenguin.com> for benchmarking the read
16 * error handling mechanism.
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 */
32
33#include <linux/module.h>
34#include <linux/delay.h>
35#include <linux/init.h>
36#include <linux/slab.h>
37#include <linux/jiffies.h>
38#include <linux/i2c.h>
39#include <linux/i2c-sensor.h>
40
41/* How many retries on register read error */
42#define MAX_RETRIES 5
43
44/*
45 * Address to scan
46 * Address is fully defined internally and cannot be changed.
47 */
48
49static unsigned short normal_i2c[] = { 0x2e, I2C_CLIENT_END };
50static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
51
52/*
53 * Insmod parameters
54 */
55
56SENSORS_INSMOD_1(w83l785ts);
57
58/*
59 * The W83L785TS-S registers
60 * Manufacturer ID is 0x5CA3 for Winbond.
61 */
62
63#define W83L785TS_REG_MAN_ID1 0x4D
64#define W83L785TS_REG_MAN_ID2 0x4C
65#define W83L785TS_REG_CHIP_ID 0x4E
66#define W83L785TS_REG_CONFIG 0x40
67#define W83L785TS_REG_TYPE 0x52
68#define W83L785TS_REG_TEMP 0x27
69#define W83L785TS_REG_TEMP_OVER 0x53 /* not sure about this one */
70
71/*
72 * Conversions
73 * The W83L785TS-S uses signed 8-bit values.
74 */
75
76#define TEMP_FROM_REG(val) ((val & 0x80 ? val-0x100 : val) * 1000)
77
78/*
79 * Functions declaration
80 */
81
82static int w83l785ts_attach_adapter(struct i2c_adapter *adapter);
83static int w83l785ts_detect(struct i2c_adapter *adapter, int address,
84 int kind);
85static int w83l785ts_detach_client(struct i2c_client *client);
86static u8 w83l785ts_read_value(struct i2c_client *client, u8 reg, u8 defval);
87static struct w83l785ts_data *w83l785ts_update_device(struct device *dev);
88
89/*
90 * Driver data (common to all clients)
91 */
92
93static struct i2c_driver w83l785ts_driver = {
94 .owner = THIS_MODULE,
95 .name = "w83l785ts",
96 .id = I2C_DRIVERID_W83L785TS,
97 .flags = I2C_DF_NOTIFY,
98 .attach_adapter = w83l785ts_attach_adapter,
99 .detach_client = w83l785ts_detach_client,
100};
101
102/*
103 * Client data (each client gets its own)
104 */
105
106struct w83l785ts_data {
107 struct i2c_client client;
108 struct semaphore update_lock;
109 char valid; /* zero until following fields are valid */
110 unsigned long last_updated; /* in jiffies */
111
112 /* registers values */
113 u8 temp, temp_over;
114};
115
116/*
117 * Sysfs stuff
118 */
119
120static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
121{
122 struct w83l785ts_data *data = w83l785ts_update_device(dev);
123 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
124}
125
126static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr, char *buf)
127{
128 struct w83l785ts_data *data = w83l785ts_update_device(dev);
129 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
130}
131
132static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
133static DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_over, NULL);
134
135/*
136 * Real code
137 */
138
139static int w83l785ts_attach_adapter(struct i2c_adapter *adapter)
140{
141 if (!(adapter->class & I2C_CLASS_HWMON))
142 return 0;
143 return i2c_detect(adapter, &addr_data, w83l785ts_detect);
144}
145
146/*
147 * The following function does more than just detection. If detection
148 * succeeds, it also registers the new chip.
149 */
150static int w83l785ts_detect(struct i2c_adapter *adapter, int address, int kind)
151{
152 struct i2c_client *new_client;
153 struct w83l785ts_data *data;
154 int err = 0;
155
156
157 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
158 goto exit;
159
160 if (!(data = kmalloc(sizeof(struct w83l785ts_data), GFP_KERNEL))) {
161 err = -ENOMEM;
162 goto exit;
163 }
164 memset(data, 0, sizeof(struct w83l785ts_data));
165
166
167 /* The common I2C client data is placed right before the
168 * W83L785TS-specific data. */
169 new_client = &data->client;
170 i2c_set_clientdata(new_client, data);
171 new_client->addr = address;
172 new_client->adapter = adapter;
173 new_client->driver = &w83l785ts_driver;
174 new_client->flags = 0;
175
176 /*
177 * Now we do the remaining detection. A negative kind means that
178 * the driver was loaded with no force parameter (default), so we
179 * must both detect and identify the chip (actually there is only
180 * one possible kind of chip for now, W83L785TS-S). A zero kind means
181 * that the driver was loaded with the force parameter, the detection
182 * step shall be skipped. A positive kind means that the driver
183 * was loaded with the force parameter and a given kind of chip is
184 * requested, so both the detection and the identification steps
185 * are skipped.
186 */
187 if (kind < 0) { /* detection */
188 if (((w83l785ts_read_value(new_client,
189 W83L785TS_REG_CONFIG, 0) & 0x80) != 0x00)
190 || ((w83l785ts_read_value(new_client,
191 W83L785TS_REG_TYPE, 0) & 0xFC) != 0x00)) {
192 dev_dbg(&adapter->dev,
193 "W83L785TS-S detection failed at 0x%02x.\n",
194 address);
195 goto exit_free;
196 }
197 }
198
199 if (kind <= 0) { /* identification */
200 u16 man_id;
201 u8 chip_id;
202
203 man_id = (w83l785ts_read_value(new_client,
204 W83L785TS_REG_MAN_ID1, 0) << 8) +
205 w83l785ts_read_value(new_client,
206 W83L785TS_REG_MAN_ID2, 0);
207 chip_id = w83l785ts_read_value(new_client,
208 W83L785TS_REG_CHIP_ID, 0);
209
210 if (man_id == 0x5CA3) { /* Winbond */
211 if (chip_id == 0x70) { /* W83L785TS-S */
212 kind = w83l785ts;
213 }
214 }
215
216 if (kind <= 0) { /* identification failed */
217 dev_info(&adapter->dev,
218 "Unsupported chip (man_id=0x%04X, "
219 "chip_id=0x%02X).\n", man_id, chip_id);
220 goto exit_free;
221 }
222 }
223
224 /* We can fill in the remaining client fields. */
225 strlcpy(new_client->name, "w83l785ts", I2C_NAME_SIZE);
226 data->valid = 0;
227 init_MUTEX(&data->update_lock);
228
229 /* Default values in case the first read fails (unlikely). */
230 data->temp_over = data->temp = 0;
231
232 /* Tell the I2C layer a new client has arrived. */
233 if ((err = i2c_attach_client(new_client)))
234 goto exit_free;
235
236 /*
237 * Initialize the W83L785TS chip
238 * Nothing yet, assume it is already started.
239 */
240
241 /* Register sysfs hooks */
242 device_create_file(&new_client->dev, &dev_attr_temp1_input);
243 device_create_file(&new_client->dev, &dev_attr_temp1_max);
244
245 return 0;
246
247exit_free:
248 kfree(data);
249exit:
250 return err;
251}
252
253static int w83l785ts_detach_client(struct i2c_client *client)
254{
255 int err;
256
257 if ((err = i2c_detach_client(client))) {
258 dev_err(&client->dev, "Client deregistration failed, "
259 "client not detached.\n");
260 return err;
261 }
262
263 kfree(i2c_get_clientdata(client));
264 return 0;
265}
266
267static u8 w83l785ts_read_value(struct i2c_client *client, u8 reg, u8 defval)
268{
269 int value, i;
270
271 /* Frequent read errors have been reported on Asus boards, so we
272 * retry on read errors. If it still fails (unlikely), return the
273 * default value requested by the caller. */
274 for (i = 1; i <= MAX_RETRIES; i++) {
275 value = i2c_smbus_read_byte_data(client, reg);
276 if (value >= 0) {
277 dev_dbg(&client->dev, "Read 0x%02x from register "
278 "0x%02x.\n", value, reg);
279 return value;
280 }
281 dev_dbg(&client->dev, "Read failed, will retry in %d.\n", i);
282 msleep(i);
283 }
284
285 dev_err(&client->dev, "Couldn't read value from register 0x%02x. "
286 "Please report.\n", reg);
287 return defval;
288}
289
290static struct w83l785ts_data *w83l785ts_update_device(struct device *dev)
291{
292 struct i2c_client *client = to_i2c_client(dev);
293 struct w83l785ts_data *data = i2c_get_clientdata(client);
294
295 down(&data->update_lock);
296
297 if (!data->valid || time_after(jiffies, data->last_updated + HZ * 2)) {
298 dev_dbg(&client->dev, "Updating w83l785ts data.\n");
299 data->temp = w83l785ts_read_value(client,
300 W83L785TS_REG_TEMP, data->temp);
301 data->temp_over = w83l785ts_read_value(client,
302 W83L785TS_REG_TEMP_OVER, data->temp_over);
303
304 data->last_updated = jiffies;
305 data->valid = 1;
306 }
307
308 up(&data->update_lock);
309
310 return data;
311}
312
313static int __init sensors_w83l785ts_init(void)
314{
315 return i2c_add_driver(&w83l785ts_driver);
316}
317
318static void __exit sensors_w83l785ts_exit(void)
319{
320 i2c_del_driver(&w83l785ts_driver);
321}
322
323MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
324MODULE_DESCRIPTION("W83L785TS-S driver");
325MODULE_LICENSE("GPL");
326
327module_init(sensors_w83l785ts_init);
328module_exit(sensors_w83l785ts_exit);