blob: 9460dba4cf74ff0bd0dbc1575f516d89d34c7d9d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040042#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020043#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040044#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/init.h>
Dominik Hacklff324092005-05-16 18:12:18 +020046#include <linux/jiffies.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010047#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "lm75.h"
49
50/*
51 HISTORY:
52 2003-12-29 1.0.0 Ported from lm_sensors project for kernel 2.6
53*/
54#define ASB100_VERSION "1.0.0"
55
56/* I2C addresses to scan */
57static unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020060I2C_CLIENT_INSMOD_1(asb100);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
62 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
63
64/* Voltage IN registers 0-6 */
65#define ASB100_REG_IN(nr) (0x20 + (nr))
66#define ASB100_REG_IN_MAX(nr) (0x2b + (nr * 2))
67#define ASB100_REG_IN_MIN(nr) (0x2c + (nr * 2))
68
69/* FAN IN registers 1-3 */
70#define ASB100_REG_FAN(nr) (0x28 + (nr))
71#define ASB100_REG_FAN_MIN(nr) (0x3b + (nr))
72
73/* TEMPERATURE registers 1-4 */
74static const u16 asb100_reg_temp[] = {0, 0x27, 0x150, 0x250, 0x17};
75static const u16 asb100_reg_temp_max[] = {0, 0x39, 0x155, 0x255, 0x18};
76static const u16 asb100_reg_temp_hyst[] = {0, 0x3a, 0x153, 0x253, 0x19};
77
78#define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])
79#define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])
80#define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])
81
82#define ASB100_REG_TEMP2_CONFIG 0x0152
83#define ASB100_REG_TEMP3_CONFIG 0x0252
84
85
86#define ASB100_REG_CONFIG 0x40
87#define ASB100_REG_ALARM1 0x41
88#define ASB100_REG_ALARM2 0x42
89#define ASB100_REG_SMIM1 0x43
90#define ASB100_REG_SMIM2 0x44
91#define ASB100_REG_VID_FANDIV 0x47
92#define ASB100_REG_I2C_ADDR 0x48
93#define ASB100_REG_CHIPID 0x49
94#define ASB100_REG_I2C_SUBADDR 0x4a
95#define ASB100_REG_PIN 0x4b
96#define ASB100_REG_IRQ 0x4c
97#define ASB100_REG_BANK 0x4e
98#define ASB100_REG_CHIPMAN 0x4f
99
100#define ASB100_REG_WCHIPID 0x58
101
102/* bit 7 -> enable, bits 0-3 -> duty cycle */
103#define ASB100_REG_PWM1 0x59
104
105/* CONVERSIONS
106 Rounding and limit checking is only done on the TO_REG variants. */
107
108/* These constants are a guess, consistent w/ w83781d */
109#define ASB100_IN_MIN ( 0)
110#define ASB100_IN_MAX (4080)
111
112/* IN: 1/1000 V (0V to 4.08V)
113 REG: 16mV/bit */
114static u8 IN_TO_REG(unsigned val)
115{
116 unsigned nval = SENSORS_LIMIT(val, ASB100_IN_MIN, ASB100_IN_MAX);
117 return (nval + 8) / 16;
118}
119
120static unsigned IN_FROM_REG(u8 reg)
121{
122 return reg * 16;
123}
124
125static u8 FAN_TO_REG(long rpm, int div)
126{
127 if (rpm == -1)
128 return 0;
129 if (rpm == 0)
130 return 255;
131 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
132 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
133}
134
135static int FAN_FROM_REG(u8 val, int div)
136{
137 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
138}
139
140/* These constants are a guess, consistent w/ w83781d */
141#define ASB100_TEMP_MIN (-128000)
142#define ASB100_TEMP_MAX ( 127000)
143
144/* TEMP: 0.001C/bit (-128C to +127C)
145 REG: 1C/bit, two's complement */
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +0200146static u8 TEMP_TO_REG(long temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 int ntemp = SENSORS_LIMIT(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX);
149 ntemp += (ntemp<0 ? -500 : 500);
150 return (u8)(ntemp / 1000);
151}
152
153static int TEMP_FROM_REG(u8 reg)
154{
155 return (s8)reg * 1000;
156}
157
158/* PWM: 0 - 255 per sensors documentation
159 REG: (6.25% duty cycle per bit) */
160static u8 ASB100_PWM_TO_REG(int pwm)
161{
162 pwm = SENSORS_LIMIT(pwm, 0, 255);
163 return (u8)(pwm / 16);
164}
165
166static int ASB100_PWM_FROM_REG(u8 reg)
167{
168 return reg * 16;
169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#define DIV_FROM_REG(val) (1 << (val))
172
173/* FAN DIV: 1, 2, 4, or 8 (defaults to 2)
174 REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */
175static u8 DIV_TO_REG(long val)
176{
177 return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1;
178}
179
180/* For each registered client, we need to keep some data in memory. That
181 data is pointed to by client->data. The structure itself is
182 dynamically allocated, at the same time the client itself is allocated. */
183struct asb100_data {
184 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700185 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100186 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 enum chips type;
188
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100189 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 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 = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100221 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100222 .name = "asb100",
223 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 .id = I2C_DRIVERID_ASB100,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 .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 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100249 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 data->in_##reg[nr] = IN_TO_REG(val); \
251 asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
252 data->in_##reg[nr]); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100253 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return count; \
255}
256
257set_in_reg(MIN, min)
258set_in_reg(MAX, max)
259
260#define sysfs_in(offset) \
261static ssize_t \
Yani Ioannou30f74292005-05-17 06:41:35 -0400262 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{ \
264 return show_in(dev, buf, offset); \
265} \
266static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
267 show_in##offset, NULL); \
268static ssize_t \
Yani Ioannou30f74292005-05-17 06:41:35 -0400269 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{ \
271 return show_in_min(dev, buf, offset); \
272} \
273static ssize_t \
Yani Ioannou30f74292005-05-17 06:41:35 -0400274 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{ \
276 return show_in_max(dev, buf, offset); \
277} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400278static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 const char *buf, size_t count) \
280{ \
281 return set_in_min(dev, buf, count, offset); \
282} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400283static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/* 3 Fans */
302static ssize_t show_fan(struct device *dev, char *buf, int nr)
303{
304 struct asb100_data *data = asb100_update_device(dev);
305 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
306 DIV_FROM_REG(data->fan_div[nr])));
307}
308
309static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
310{
311 struct asb100_data *data = asb100_update_device(dev);
312 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
313 DIV_FROM_REG(data->fan_div[nr])));
314}
315
316static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
317{
318 struct asb100_data *data = asb100_update_device(dev);
319 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
320}
321
322static ssize_t set_fan_min(struct device *dev, const char *buf,
323 size_t count, int nr)
324{
325 struct i2c_client *client = to_i2c_client(dev);
326 struct asb100_data *data = i2c_get_clientdata(client);
327 u32 val = simple_strtoul(buf, NULL, 10);
328
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100329 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
331 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100332 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return count;
334}
335
336/* Note: we save and restore the fan minimum here, because its value is
337 determined in part by the fan divisor. This follows the principle of
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200338 least surprise; the user doesn't expect the fan minimum to change just
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 because the divisor changed. */
340static ssize_t set_fan_div(struct device *dev, const char *buf,
341 size_t count, int nr)
342{
343 struct i2c_client *client = to_i2c_client(dev);
344 struct asb100_data *data = i2c_get_clientdata(client);
345 unsigned long min;
346 unsigned long val = simple_strtoul(buf, NULL, 10);
347 int reg;
348
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100349 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 min = FAN_FROM_REG(data->fan_min[nr],
352 DIV_FROM_REG(data->fan_div[nr]));
353 data->fan_div[nr] = DIV_TO_REG(val);
354
355 switch(nr) {
356 case 0: /* fan 1 */
357 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
358 reg = (reg & 0xcf) | (data->fan_div[0] << 4);
359 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
360 break;
361
362 case 1: /* fan 2 */
363 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
364 reg = (reg & 0x3f) | (data->fan_div[1] << 6);
365 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
366 break;
367
368 case 2: /* fan 3 */
369 reg = asb100_read_value(client, ASB100_REG_PIN);
370 reg = (reg & 0x3f) | (data->fan_div[2] << 6);
371 asb100_write_value(client, ASB100_REG_PIN, reg);
372 break;
373 }
374
375 data->fan_min[nr] =
376 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
377 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
378
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100379 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 return count;
382}
383
384#define sysfs_fan(offset) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400385static ssize_t show_fan##offset(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{ \
387 return show_fan(dev, buf, offset - 1); \
388} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400389static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{ \
391 return show_fan_min(dev, buf, offset - 1); \
392} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400393static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{ \
395 return show_fan_div(dev, buf, offset - 1); \
396} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400397static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 size_t count) \
399{ \
400 return set_fan_min(dev, buf, count, offset - 1); \
401} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400402static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 size_t count) \
404{ \
405 return set_fan_div(dev, buf, count, offset - 1); \
406} \
407static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
408 show_fan##offset, NULL); \
409static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
410 show_fan##offset##_min, set_fan##offset##_min); \
411static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
412 show_fan##offset##_div, set_fan##offset##_div);
413
414sysfs_fan(1);
415sysfs_fan(2);
416sysfs_fan(3);
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/* 4 Temp. Sensors */
419static int sprintf_temp_from_reg(u16 reg, char *buf, int nr)
420{
421 int ret = 0;
422
423 switch (nr) {
424 case 1: case 2:
425 ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg));
426 break;
427 case 0: case 3: default:
428 ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
429 break;
430 }
431 return ret;
432}
433
434#define show_temp_reg(reg) \
435static ssize_t show_##reg(struct device *dev, char *buf, int nr) \
436{ \
437 struct asb100_data *data = asb100_update_device(dev); \
438 return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
439}
440
441show_temp_reg(temp);
442show_temp_reg(temp_max);
443show_temp_reg(temp_hyst);
444
445#define set_temp_reg(REG, reg) \
446static ssize_t set_##reg(struct device *dev, const char *buf, \
447 size_t count, int nr) \
448{ \
449 struct i2c_client *client = to_i2c_client(dev); \
450 struct asb100_data *data = i2c_get_clientdata(client); \
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +0200451 long val = simple_strtol(buf, NULL, 10); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100453 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 switch (nr) { \
455 case 1: case 2: \
456 data->reg[nr] = LM75_TEMP_TO_REG(val); \
457 break; \
458 case 0: case 3: default: \
459 data->reg[nr] = TEMP_TO_REG(val); \
460 break; \
461 } \
462 asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
463 data->reg[nr]); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100464 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return count; \
466}
467
468set_temp_reg(MAX, temp_max);
469set_temp_reg(HYST, temp_hyst);
470
471#define sysfs_temp(num) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400472static ssize_t show_temp##num(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{ \
474 return show_temp(dev, buf, num-1); \
475} \
476static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL); \
Yani Ioannou30f74292005-05-17 06:41:35 -0400477static ssize_t show_temp_max##num(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{ \
479 return show_temp_max(dev, buf, num-1); \
480} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400481static ssize_t set_temp_max##num(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 size_t count) \
483{ \
484 return set_temp_max(dev, buf, count, num-1); \
485} \
486static DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
487 show_temp_max##num, set_temp_max##num); \
Yani Ioannou30f74292005-05-17 06:41:35 -0400488static ssize_t show_temp_hyst##num(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{ \
490 return show_temp_hyst(dev, buf, num-1); \
491} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400492static ssize_t set_temp_hyst##num(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 size_t count) \
494{ \
495 return set_temp_hyst(dev, buf, count, num-1); \
496} \
497static DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
498 show_temp_hyst##num, set_temp_hyst##num);
499
500sysfs_temp(1);
501sysfs_temp(2);
502sysfs_temp(3);
503sysfs_temp(4);
504
505/* VID */
Yani Ioannou30f74292005-05-17 06:41:35 -0400506static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 struct asb100_data *data = asb100_update_device(dev);
509 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
510}
511
512static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514/* VRM */
Yani Ioannou30f74292005-05-17 06:41:35 -0400515static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Jean Delvare90d66192007-10-08 18:24:35 +0200517 struct asb100_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return sprintf(buf, "%d\n", data->vrm);
519}
520
Yani Ioannou30f74292005-05-17 06:41:35 -0400521static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 struct i2c_client *client = to_i2c_client(dev);
524 struct asb100_data *data = i2c_get_clientdata(client);
525 unsigned long val = simple_strtoul(buf, NULL, 10);
526 data->vrm = val;
527 return count;
528}
529
530/* Alarms */
531static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Yani Ioannou30f74292005-05-17 06:41:35 -0400533static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 struct asb100_data *data = asb100_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200536 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541/* 1 PWM */
Yani Ioannou30f74292005-05-17 06:41:35 -0400542static ssize_t show_pwm1(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 struct asb100_data *data = asb100_update_device(dev);
545 return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
546}
547
Yani Ioannou30f74292005-05-17 06:41:35 -0400548static ssize_t set_pwm1(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 struct i2c_client *client = to_i2c_client(dev);
551 struct asb100_data *data = i2c_get_clientdata(client);
552 unsigned long val = simple_strtoul(buf, NULL, 10);
553
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100554 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 data->pwm &= 0x80; /* keep the enable bit */
556 data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
557 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100558 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return count;
560}
561
Yani Ioannou30f74292005-05-17 06:41:35 -0400562static ssize_t show_pwm_enable1(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 struct asb100_data *data = asb100_update_device(dev);
565 return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
566}
567
Yani Ioannou30f74292005-05-17 06:41:35 -0400568static ssize_t set_pwm_enable1(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 size_t count)
570{
571 struct i2c_client *client = to_i2c_client(dev);
572 struct asb100_data *data = i2c_get_clientdata(client);
573 unsigned long val = simple_strtoul(buf, NULL, 10);
574
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100575 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 data->pwm &= 0x0f; /* keep the duty cycle bits */
577 data->pwm |= (val ? 0x80 : 0x00);
578 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100579 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return count;
581}
582
583static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm1, set_pwm1);
584static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
585 show_pwm_enable1, set_pwm_enable1);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200586
587static struct attribute *asb100_attributes[] = {
588 &dev_attr_in0_input.attr,
589 &dev_attr_in0_min.attr,
590 &dev_attr_in0_max.attr,
591 &dev_attr_in1_input.attr,
592 &dev_attr_in1_min.attr,
593 &dev_attr_in1_max.attr,
594 &dev_attr_in2_input.attr,
595 &dev_attr_in2_min.attr,
596 &dev_attr_in2_max.attr,
597 &dev_attr_in3_input.attr,
598 &dev_attr_in3_min.attr,
599 &dev_attr_in3_max.attr,
600 &dev_attr_in4_input.attr,
601 &dev_attr_in4_min.attr,
602 &dev_attr_in4_max.attr,
603 &dev_attr_in5_input.attr,
604 &dev_attr_in5_min.attr,
605 &dev_attr_in5_max.attr,
606 &dev_attr_in6_input.attr,
607 &dev_attr_in6_min.attr,
608 &dev_attr_in6_max.attr,
609
610 &dev_attr_fan1_input.attr,
611 &dev_attr_fan1_min.attr,
612 &dev_attr_fan1_div.attr,
613 &dev_attr_fan2_input.attr,
614 &dev_attr_fan2_min.attr,
615 &dev_attr_fan2_div.attr,
616 &dev_attr_fan3_input.attr,
617 &dev_attr_fan3_min.attr,
618 &dev_attr_fan3_div.attr,
619
620 &dev_attr_temp1_input.attr,
621 &dev_attr_temp1_max.attr,
622 &dev_attr_temp1_max_hyst.attr,
623 &dev_attr_temp2_input.attr,
624 &dev_attr_temp2_max.attr,
625 &dev_attr_temp2_max_hyst.attr,
626 &dev_attr_temp3_input.attr,
627 &dev_attr_temp3_max.attr,
628 &dev_attr_temp3_max_hyst.attr,
629 &dev_attr_temp4_input.attr,
630 &dev_attr_temp4_max.attr,
631 &dev_attr_temp4_max_hyst.attr,
632
633 &dev_attr_cpu0_vid.attr,
634 &dev_attr_vrm.attr,
635 &dev_attr_alarms.attr,
636 &dev_attr_pwm1.attr,
637 &dev_attr_pwm1_enable.attr,
638
639 NULL
640};
641
642static const struct attribute_group asb100_group = {
643 .attrs = asb100_attributes,
644};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646/* This function is called when:
647 asb100_driver is inserted (when this module is loaded), for each
648 available adapter
649 when a new adapter is inserted (and asb100_driver is still present)
650 */
651static int asb100_attach_adapter(struct i2c_adapter *adapter)
652{
653 if (!(adapter->class & I2C_CLASS_HWMON))
654 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200655 return i2c_probe(adapter, &addr_data, asb100_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658static int asb100_detect_subclients(struct i2c_adapter *adapter, int address,
659 int kind, struct i2c_client *new_client)
660{
661 int i, id, err;
662 struct asb100_data *data = i2c_get_clientdata(new_client);
663
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200664 data->lm75[0] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (!(data->lm75[0])) {
666 err = -ENOMEM;
667 goto ERROR_SC_0;
668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200670 data->lm75[1] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (!(data->lm75[1])) {
672 err = -ENOMEM;
673 goto ERROR_SC_1;
674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 id = i2c_adapter_id(adapter);
677
678 if (force_subclients[0] == id && force_subclients[1] == address) {
679 for (i = 2; i <= 3; i++) {
680 if (force_subclients[i] < 0x48 ||
681 force_subclients[i] > 0x4f) {
682 dev_err(&new_client->dev, "invalid subclient "
683 "address %d; must be 0x48-0x4f\n",
684 force_subclients[i]);
685 err = -ENODEV;
686 goto ERROR_SC_2;
687 }
688 }
689 asb100_write_value(new_client, ASB100_REG_I2C_SUBADDR,
690 (force_subclients[2] & 0x07) |
691 ((force_subclients[3] & 0x07) <<4));
692 data->lm75[0]->addr = force_subclients[2];
693 data->lm75[1]->addr = force_subclients[3];
694 } else {
695 int val = asb100_read_value(new_client, ASB100_REG_I2C_SUBADDR);
696 data->lm75[0]->addr = 0x48 + (val & 0x07);
697 data->lm75[1]->addr = 0x48 + ((val >> 4) & 0x07);
698 }
699
700 if(data->lm75[0]->addr == data->lm75[1]->addr) {
701 dev_err(&new_client->dev, "duplicate addresses 0x%x "
702 "for subclients\n", data->lm75[0]->addr);
703 err = -ENODEV;
704 goto ERROR_SC_2;
705 }
706
707 for (i = 0; i <= 1; i++) {
708 i2c_set_clientdata(data->lm75[i], NULL);
709 data->lm75[i]->adapter = adapter;
710 data->lm75[i]->driver = &asb100_driver;
711 data->lm75[i]->flags = 0;
712 strlcpy(data->lm75[i]->name, "asb100 subclient", I2C_NAME_SIZE);
713 }
714
715 if ((err = i2c_attach_client(data->lm75[0]))) {
716 dev_err(&new_client->dev, "subclient %d registration "
717 "at address 0x%x failed.\n", i, data->lm75[0]->addr);
718 goto ERROR_SC_2;
719 }
720
721 if ((err = i2c_attach_client(data->lm75[1]))) {
722 dev_err(&new_client->dev, "subclient %d registration "
723 "at address 0x%x failed.\n", i, data->lm75[1]->addr);
724 goto ERROR_SC_3;
725 }
726
727 return 0;
728
729/* Undo inits in case of errors */
730ERROR_SC_3:
731 i2c_detach_client(data->lm75[0]);
732ERROR_SC_2:
733 kfree(data->lm75[1]);
734ERROR_SC_1:
735 kfree(data->lm75[0]);
736ERROR_SC_0:
737 return err;
738}
739
740static int asb100_detect(struct i2c_adapter *adapter, int address, int kind)
741{
742 int err;
743 struct i2c_client *new_client;
744 struct asb100_data *data;
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
747 pr_debug("asb100.o: detect failed, "
748 "smbus byte data not supported!\n");
749 err = -ENODEV;
750 goto ERROR0;
751 }
752
753 /* OK. For now, we presume we have a valid client. We now create the
754 client structure, even though we cannot fill it completely yet.
755 But it allows us to access asb100_{read,write}_value. */
756
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200757 if (!(data = kzalloc(sizeof(struct asb100_data), GFP_KERNEL))) {
758 pr_debug("asb100.o: detect failed, kzalloc failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 err = -ENOMEM;
760 goto ERROR0;
761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 new_client = &data->client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100764 mutex_init(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 i2c_set_clientdata(new_client, data);
766 new_client->addr = address;
767 new_client->adapter = adapter;
768 new_client->driver = &asb100_driver;
769 new_client->flags = 0;
770
771 /* Now, we do the remaining detection. */
772
773 /* The chip may be stuck in some other bank than bank 0. This may
774 make reading other information impossible. Specify a force=... or
775 force_*=... parameter, and the chip will be reset to the right
776 bank. */
777 if (kind < 0) {
778
779 int val1 = asb100_read_value(new_client, ASB100_REG_BANK);
780 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
781
782 /* If we're in bank 0 */
783 if ( (!(val1 & 0x07)) &&
784 /* Check for ASB100 ID (low byte) */
785 ( ((!(val1 & 0x80)) && (val2 != 0x94)) ||
786 /* Check for ASB100 ID (high byte ) */
787 ((val1 & 0x80) && (val2 != 0x06)) ) ) {
788 pr_debug("asb100.o: detect failed, "
789 "bad chip id 0x%02x!\n", val2);
790 err = -ENODEV;
791 goto ERROR1;
792 }
793
794 } /* kind < 0 */
795
796 /* We have either had a force parameter, or we have already detected
797 Winbond. Put it now into bank 0 and Vendor ID High Byte */
798 asb100_write_value(new_client, ASB100_REG_BANK,
799 (asb100_read_value(new_client, ASB100_REG_BANK) & 0x78) | 0x80);
800
801 /* Determine the chip type. */
802 if (kind <= 0) {
803 int val1 = asb100_read_value(new_client, ASB100_REG_WCHIPID);
804 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
805
806 if ((val1 == 0x31) && (val2 == 0x06))
807 kind = asb100;
808 else {
809 if (kind == 0)
810 dev_warn(&new_client->dev, "ignoring "
811 "'force' parameter for unknown chip "
812 "at adapter %d, address 0x%02x.\n",
813 i2c_adapter_id(adapter), address);
814 err = -ENODEV;
815 goto ERROR1;
816 }
817 }
818
819 /* Fill in remaining client fields and put it into the global list */
820 strlcpy(new_client->name, "asb100", I2C_NAME_SIZE);
821 data->type = kind;
822
823 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100824 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 /* Tell the I2C layer a new client has arrived */
827 if ((err = i2c_attach_client(new_client)))
828 goto ERROR1;
829
830 /* Attach secondary lm75 clients */
831 if ((err = asb100_detect_subclients(adapter, address, kind,
832 new_client)))
833 goto ERROR2;
834
835 /* Initialize the chip */
836 asb100_init_client(new_client);
837
838 /* A few vars need to be filled upon startup */
839 data->fan_min[0] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(0));
840 data->fan_min[1] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(1));
841 data->fan_min[2] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(2));
842
843 /* Register sysfs hooks */
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200844 if ((err = sysfs_create_group(&new_client->dev.kobj, &asb100_group)))
845 goto ERROR3;
846
Tony Jones1beeffe2007-08-20 13:46:20 -0700847 data->hwmon_dev = hwmon_device_register(&new_client->dev);
848 if (IS_ERR(data->hwmon_dev)) {
849 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200850 goto ERROR4;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400851 }
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return 0;
854
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200855ERROR4:
856 sysfs_remove_group(&new_client->dev.kobj, &asb100_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400857ERROR3:
858 i2c_detach_client(data->lm75[1]);
859 i2c_detach_client(data->lm75[0]);
860 kfree(data->lm75[1]);
861 kfree(data->lm75[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862ERROR2:
863 i2c_detach_client(new_client);
864ERROR1:
865 kfree(data);
866ERROR0:
867 return err;
868}
869
870static int asb100_detach_client(struct i2c_client *client)
871{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400872 struct asb100_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 int err;
874
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400875 /* main client */
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200876 if (data) {
Tony Jones1beeffe2007-08-20 13:46:20 -0700877 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200878 sysfs_remove_group(&client->dev.kobj, &asb100_group);
879 }
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400880
Jean Delvare7bef5592005-07-27 22:14:49 +0200881 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400884 /* main client */
885 if (data)
886 kfree(data);
887
888 /* subclient */
889 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 return 0;
893}
894
895/* The SMBus locks itself, usually, but nothing may access the chip between
896 bank switches. */
897static int asb100_read_value(struct i2c_client *client, u16 reg)
898{
899 struct asb100_data *data = i2c_get_clientdata(client);
900 struct i2c_client *cl;
901 int res, bank;
902
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100903 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 bank = (reg >> 8) & 0x0f;
906 if (bank > 2)
907 /* switch banks */
908 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
909
910 if (bank == 0 || bank > 2) {
911 res = i2c_smbus_read_byte_data(client, reg & 0xff);
912 } else {
913 /* switch to subclient */
914 cl = data->lm75[bank - 1];
915
916 /* convert from ISA to LM75 I2C addresses */
917 switch (reg & 0xff) {
918 case 0x50: /* TEMP */
919 res = swab16(i2c_smbus_read_word_data (cl, 0));
920 break;
921 case 0x52: /* CONFIG */
922 res = i2c_smbus_read_byte_data(cl, 1);
923 break;
924 case 0x53: /* HYST */
925 res = swab16(i2c_smbus_read_word_data (cl, 2));
926 break;
927 case 0x55: /* MAX */
928 default:
929 res = swab16(i2c_smbus_read_word_data (cl, 3));
930 break;
931 }
932 }
933
934 if (bank > 2)
935 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
936
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100937 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 return res;
940}
941
942static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
943{
944 struct asb100_data *data = i2c_get_clientdata(client);
945 struct i2c_client *cl;
946 int bank;
947
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100948 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 bank = (reg >> 8) & 0x0f;
951 if (bank > 2)
952 /* switch banks */
953 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
954
955 if (bank == 0 || bank > 2) {
956 i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
957 } else {
958 /* switch to subclient */
959 cl = data->lm75[bank - 1];
960
961 /* convert from ISA to LM75 I2C addresses */
962 switch (reg & 0xff) {
963 case 0x52: /* CONFIG */
964 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
965 break;
966 case 0x53: /* HYST */
967 i2c_smbus_write_word_data(cl, 2, swab16(value));
968 break;
969 case 0x55: /* MAX */
970 i2c_smbus_write_word_data(cl, 3, swab16(value));
971 break;
972 }
973 }
974
975 if (bank > 2)
976 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
977
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100978 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
981static void asb100_init_client(struct i2c_client *client)
982{
983 struct asb100_data *data = i2c_get_clientdata(client);
984 int vid = 0;
985
986 vid = asb100_read_value(client, ASB100_REG_VID_FANDIV) & 0x0f;
987 vid |= (asb100_read_value(client, ASB100_REG_CHIPID) & 0x01) << 4;
Jean Delvare303760b2005-07-31 21:52:01 +0200988 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 vid = vid_from_reg(vid, data->vrm);
990
991 /* Start monitoring */
992 asb100_write_value(client, ASB100_REG_CONFIG,
993 (asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
994}
995
996static struct asb100_data *asb100_update_device(struct device *dev)
997{
998 struct i2c_client *client = to_i2c_client(dev);
999 struct asb100_data *data = i2c_get_clientdata(client);
1000 int i;
1001
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001002 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1005 || !data->valid) {
1006
1007 dev_dbg(&client->dev, "starting device update...\n");
1008
1009 /* 7 voltage inputs */
1010 for (i = 0; i < 7; i++) {
1011 data->in[i] = asb100_read_value(client,
1012 ASB100_REG_IN(i));
1013 data->in_min[i] = asb100_read_value(client,
1014 ASB100_REG_IN_MIN(i));
1015 data->in_max[i] = asb100_read_value(client,
1016 ASB100_REG_IN_MAX(i));
1017 }
1018
1019 /* 3 fan inputs */
1020 for (i = 0; i < 3; i++) {
1021 data->fan[i] = asb100_read_value(client,
1022 ASB100_REG_FAN(i));
1023 data->fan_min[i] = asb100_read_value(client,
1024 ASB100_REG_FAN_MIN(i));
1025 }
1026
1027 /* 4 temperature inputs */
1028 for (i = 1; i <= 4; i++) {
1029 data->temp[i-1] = asb100_read_value(client,
1030 ASB100_REG_TEMP(i));
1031 data->temp_max[i-1] = asb100_read_value(client,
1032 ASB100_REG_TEMP_MAX(i));
1033 data->temp_hyst[i-1] = asb100_read_value(client,
1034 ASB100_REG_TEMP_HYST(i));
1035 }
1036
1037 /* VID and fan divisors */
1038 i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
1039 data->vid = i & 0x0f;
1040 data->vid |= (asb100_read_value(client,
1041 ASB100_REG_CHIPID) & 0x01) << 4;
1042 data->fan_div[0] = (i >> 4) & 0x03;
1043 data->fan_div[1] = (i >> 6) & 0x03;
1044 data->fan_div[2] = (asb100_read_value(client,
1045 ASB100_REG_PIN) >> 6) & 0x03;
1046
1047 /* PWM */
1048 data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
1049
1050 /* alarms */
1051 data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
1052 (asb100_read_value(client, ASB100_REG_ALARM2) << 8);
1053
1054 data->last_updated = jiffies;
1055 data->valid = 1;
1056
1057 dev_dbg(&client->dev, "... device update complete\n");
1058 }
1059
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001060 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 return data;
1063}
1064
1065static int __init asb100_init(void)
1066{
1067 return i2c_add_driver(&asb100_driver);
1068}
1069
1070static void __exit asb100_exit(void)
1071{
1072 i2c_del_driver(&asb100_driver);
1073}
1074
1075MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1076MODULE_DESCRIPTION("ASB100 Bach driver");
1077MODULE_LICENSE("GPL");
1078
1079module_init(asb100_init);
1080module_exit(asb100_exit);
1081