]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | asb100.c - Part of lm_sensors, Linux kernel modules for hardware | |
3 | monitoring | |
4 | ||
5 | Copyright (C) 2004 Mark M. Hoffman <[email protected]> | |
6 | ||
7 | (derived from w83781d.c) | |
8 | ||
9 | Copyright (C) 1998 - 2003 Frodo Looijaard <[email protected]>, | |
10 | Philip Edelbrock <[email protected]>, and | |
11 | Mark Studebaker <[email protected]> | |
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> | |
943b0830 | 42 | #include <linux/hwmon.h> |
fad33c5f | 43 | #include <linux/hwmon-sysfs.h> |
303760b4 | 44 | #include <linux/hwmon-vid.h> |
943b0830 | 45 | #include <linux/err.h> |
1da177e4 | 46 | #include <linux/init.h> |
ff324094 | 47 | #include <linux/jiffies.h> |
9a61bf63 | 48 | #include <linux/mutex.h> |
1da177e4 LT |
49 | #include "lm75.h" |
50 | ||
1da177e4 | 51 | /* I2C addresses to scan */ |
25e9c86d | 52 | static const unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END }; |
1da177e4 | 53 | |
1da177e4 | 54 | /* Insmod parameters */ |
f4b50261 | 55 | I2C_CLIENT_INSMOD_1(asb100); |
3aed198c JD |
56 | |
57 | static unsigned short force_subclients[4]; | |
58 | module_param_array(force_subclients, short, NULL, 0); | |
59 | MODULE_PARM_DESC(force_subclients, "List of subclient addresses: " | |
1da177e4 LT |
60 | "{bus, clientaddr, subclientaddr1, subclientaddr2}"); |
61 | ||
62 | /* Voltage IN registers 0-6 */ | |
63 | #define ASB100_REG_IN(nr) (0x20 + (nr)) | |
64 | #define ASB100_REG_IN_MAX(nr) (0x2b + (nr * 2)) | |
65 | #define ASB100_REG_IN_MIN(nr) (0x2c + (nr * 2)) | |
66 | ||
67 | /* FAN IN registers 1-3 */ | |
68 | #define ASB100_REG_FAN(nr) (0x28 + (nr)) | |
69 | #define ASB100_REG_FAN_MIN(nr) (0x3b + (nr)) | |
70 | ||
71 | /* TEMPERATURE registers 1-4 */ | |
72 | static const u16 asb100_reg_temp[] = {0, 0x27, 0x150, 0x250, 0x17}; | |
73 | static const u16 asb100_reg_temp_max[] = {0, 0x39, 0x155, 0x255, 0x18}; | |
74 | static const u16 asb100_reg_temp_hyst[] = {0, 0x3a, 0x153, 0x253, 0x19}; | |
75 | ||
76 | #define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr]) | |
77 | #define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr]) | |
78 | #define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr]) | |
79 | ||
80 | #define ASB100_REG_TEMP2_CONFIG 0x0152 | |
81 | #define ASB100_REG_TEMP3_CONFIG 0x0252 | |
82 | ||
83 | ||
84 | #define ASB100_REG_CONFIG 0x40 | |
85 | #define ASB100_REG_ALARM1 0x41 | |
86 | #define ASB100_REG_ALARM2 0x42 | |
87 | #define ASB100_REG_SMIM1 0x43 | |
88 | #define ASB100_REG_SMIM2 0x44 | |
89 | #define ASB100_REG_VID_FANDIV 0x47 | |
90 | #define ASB100_REG_I2C_ADDR 0x48 | |
91 | #define ASB100_REG_CHIPID 0x49 | |
92 | #define ASB100_REG_I2C_SUBADDR 0x4a | |
93 | #define ASB100_REG_PIN 0x4b | |
94 | #define ASB100_REG_IRQ 0x4c | |
95 | #define ASB100_REG_BANK 0x4e | |
96 | #define ASB100_REG_CHIPMAN 0x4f | |
97 | ||
98 | #define ASB100_REG_WCHIPID 0x58 | |
99 | ||
100 | /* bit 7 -> enable, bits 0-3 -> duty cycle */ | |
101 | #define ASB100_REG_PWM1 0x59 | |
102 | ||
103 | /* CONVERSIONS | |
104 | Rounding and limit checking is only done on the TO_REG variants. */ | |
105 | ||
106 | /* These constants are a guess, consistent w/ w83781d */ | |
107 | #define ASB100_IN_MIN ( 0) | |
108 | #define ASB100_IN_MAX (4080) | |
109 | ||
110 | /* IN: 1/1000 V (0V to 4.08V) | |
111 | REG: 16mV/bit */ | |
112 | static u8 IN_TO_REG(unsigned val) | |
113 | { | |
114 | unsigned nval = SENSORS_LIMIT(val, ASB100_IN_MIN, ASB100_IN_MAX); | |
115 | return (nval + 8) / 16; | |
116 | } | |
117 | ||
118 | static unsigned IN_FROM_REG(u8 reg) | |
119 | { | |
120 | return reg * 16; | |
121 | } | |
122 | ||
123 | static u8 FAN_TO_REG(long rpm, int div) | |
124 | { | |
125 | if (rpm == -1) | |
126 | return 0; | |
127 | if (rpm == 0) | |
128 | return 255; | |
129 | rpm = SENSORS_LIMIT(rpm, 1, 1000000); | |
130 | return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254); | |
131 | } | |
132 | ||
133 | static int FAN_FROM_REG(u8 val, int div) | |
134 | { | |
135 | return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div); | |
136 | } | |
137 | ||
138 | /* These constants are a guess, consistent w/ w83781d */ | |
139 | #define ASB100_TEMP_MIN (-128000) | |
140 | #define ASB100_TEMP_MAX ( 127000) | |
141 | ||
142 | /* TEMP: 0.001C/bit (-128C to +127C) | |
143 | REG: 1C/bit, two's complement */ | |
5bfedac0 | 144 | static u8 TEMP_TO_REG(long temp) |
1da177e4 LT |
145 | { |
146 | int ntemp = SENSORS_LIMIT(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX); | |
147 | ntemp += (ntemp<0 ? -500 : 500); | |
148 | return (u8)(ntemp / 1000); | |
149 | } | |
150 | ||
151 | static int TEMP_FROM_REG(u8 reg) | |
152 | { | |
153 | return (s8)reg * 1000; | |
154 | } | |
155 | ||
156 | /* PWM: 0 - 255 per sensors documentation | |
157 | REG: (6.25% duty cycle per bit) */ | |
158 | static u8 ASB100_PWM_TO_REG(int pwm) | |
159 | { | |
160 | pwm = SENSORS_LIMIT(pwm, 0, 255); | |
161 | return (u8)(pwm / 16); | |
162 | } | |
163 | ||
164 | static int ASB100_PWM_FROM_REG(u8 reg) | |
165 | { | |
166 | return reg * 16; | |
167 | } | |
168 | ||
1da177e4 LT |
169 | #define DIV_FROM_REG(val) (1 << (val)) |
170 | ||
171 | /* FAN DIV: 1, 2, 4, or 8 (defaults to 2) | |
172 | REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */ | |
173 | static u8 DIV_TO_REG(long val) | |
174 | { | |
175 | return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1; | |
176 | } | |
177 | ||
178 | /* For each registered client, we need to keep some data in memory. That | |
179 | data is pointed to by client->data. The structure itself is | |
180 | dynamically allocated, at the same time the client itself is allocated. */ | |
181 | struct asb100_data { | |
1beeffe4 | 182 | struct device *hwmon_dev; |
9a61bf63 | 183 | struct mutex lock; |
1da177e4 | 184 | |
9a61bf63 | 185 | struct mutex update_lock; |
1da177e4 LT |
186 | unsigned long last_updated; /* In jiffies */ |
187 | ||
188 | /* array of 2 pointers to subclients */ | |
189 | struct i2c_client *lm75[2]; | |
190 | ||
191 | char valid; /* !=0 if following fields are valid */ | |
192 | u8 in[7]; /* Register value */ | |
193 | u8 in_max[7]; /* Register value */ | |
194 | u8 in_min[7]; /* Register value */ | |
195 | u8 fan[3]; /* Register value */ | |
196 | u8 fan_min[3]; /* Register value */ | |
197 | u16 temp[4]; /* Register value (0 and 3 are u8 only) */ | |
198 | u16 temp_max[4]; /* Register value (0 and 3 are u8 only) */ | |
199 | u16 temp_hyst[4]; /* Register value (0 and 3 are u8 only) */ | |
200 | u8 fan_div[3]; /* Register encoding, right justified */ | |
201 | u8 pwm; /* Register encoding */ | |
202 | u8 vid; /* Register encoding, combined */ | |
203 | u32 alarms; /* Register encoding, combined */ | |
204 | u8 vrm; | |
205 | }; | |
206 | ||
207 | static int asb100_read_value(struct i2c_client *client, u16 reg); | |
208 | static void asb100_write_value(struct i2c_client *client, u16 reg, u16 val); | |
209 | ||
063675b1 JD |
210 | static int asb100_probe(struct i2c_client *client, |
211 | const struct i2c_device_id *id); | |
310ec792 | 212 | static int asb100_detect(struct i2c_client *client, |
063675b1 JD |
213 | struct i2c_board_info *info); |
214 | static int asb100_remove(struct i2c_client *client); | |
1da177e4 LT |
215 | static struct asb100_data *asb100_update_device(struct device *dev); |
216 | static void asb100_init_client(struct i2c_client *client); | |
217 | ||
063675b1 JD |
218 | static const struct i2c_device_id asb100_id[] = { |
219 | { "asb100", asb100 }, | |
220 | { } | |
221 | }; | |
222 | MODULE_DEVICE_TABLE(i2c, asb100_id); | |
223 | ||
1da177e4 | 224 | static struct i2c_driver asb100_driver = { |
063675b1 | 225 | .class = I2C_CLASS_HWMON, |
cdaf7934 | 226 | .driver = { |
cdaf7934 LR |
227 | .name = "asb100", |
228 | }, | |
063675b1 JD |
229 | .probe = asb100_probe, |
230 | .remove = asb100_remove, | |
231 | .id_table = asb100_id, | |
232 | .detect = asb100_detect, | |
c3813d6a | 233 | .address_list = normal_i2c, |
1da177e4 LT |
234 | }; |
235 | ||
236 | /* 7 Voltages */ | |
237 | #define show_in_reg(reg) \ | |
fad33c5f JD |
238 | static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \ |
239 | char *buf) \ | |
1da177e4 | 240 | { \ |
fad33c5f | 241 | int nr = to_sensor_dev_attr(attr)->index; \ |
1da177e4 LT |
242 | struct asb100_data *data = asb100_update_device(dev); \ |
243 | return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \ | |
244 | } | |
245 | ||
246 | show_in_reg(in) | |
247 | show_in_reg(in_min) | |
248 | show_in_reg(in_max) | |
249 | ||
250 | #define set_in_reg(REG, reg) \ | |
fad33c5f JD |
251 | static ssize_t set_in_##reg(struct device *dev, struct device_attribute *attr, \ |
252 | const char *buf, size_t count) \ | |
1da177e4 | 253 | { \ |
fad33c5f | 254 | int nr = to_sensor_dev_attr(attr)->index; \ |
1da177e4 LT |
255 | struct i2c_client *client = to_i2c_client(dev); \ |
256 | struct asb100_data *data = i2c_get_clientdata(client); \ | |
257 | unsigned long val = simple_strtoul(buf, NULL, 10); \ | |
258 | \ | |
9a61bf63 | 259 | mutex_lock(&data->update_lock); \ |
1da177e4 LT |
260 | data->in_##reg[nr] = IN_TO_REG(val); \ |
261 | asb100_write_value(client, ASB100_REG_IN_##REG(nr), \ | |
262 | data->in_##reg[nr]); \ | |
9a61bf63 | 263 | mutex_unlock(&data->update_lock); \ |
1da177e4 LT |
264 | return count; \ |
265 | } | |
266 | ||
267 | set_in_reg(MIN, min) | |
268 | set_in_reg(MAX, max) | |
269 | ||
270 | #define sysfs_in(offset) \ | |
fad33c5f JD |
271 | static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \ |
272 | show_in, NULL, offset); \ | |
273 | static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ | |
274 | show_in_min, set_in_min, offset); \ | |
275 | static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ | |
276 | show_in_max, set_in_max, offset) | |
1da177e4 LT |
277 | |
278 | sysfs_in(0); | |
279 | sysfs_in(1); | |
280 | sysfs_in(2); | |
281 | sysfs_in(3); | |
282 | sysfs_in(4); | |
283 | sysfs_in(5); | |
284 | sysfs_in(6); | |
285 | ||
1da177e4 | 286 | /* 3 Fans */ |
fad33c5f JD |
287 | static ssize_t show_fan(struct device *dev, struct device_attribute *attr, |
288 | char *buf) | |
1da177e4 | 289 | { |
fad33c5f | 290 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 LT |
291 | struct asb100_data *data = asb100_update_device(dev); |
292 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr], | |
293 | DIV_FROM_REG(data->fan_div[nr]))); | |
294 | } | |
295 | ||
fad33c5f JD |
296 | static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr, |
297 | char *buf) | |
1da177e4 | 298 | { |
fad33c5f | 299 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 LT |
300 | struct asb100_data *data = asb100_update_device(dev); |
301 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr], | |
302 | DIV_FROM_REG(data->fan_div[nr]))); | |
303 | } | |
304 | ||
fad33c5f JD |
305 | static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr, |
306 | char *buf) | |
1da177e4 | 307 | { |
fad33c5f | 308 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 LT |
309 | struct asb100_data *data = asb100_update_device(dev); |
310 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr])); | |
311 | } | |
312 | ||
fad33c5f JD |
313 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, |
314 | const char *buf, size_t count) | |
1da177e4 | 315 | { |
fad33c5f | 316 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 LT |
317 | struct i2c_client *client = to_i2c_client(dev); |
318 | struct asb100_data *data = i2c_get_clientdata(client); | |
319 | u32 val = simple_strtoul(buf, NULL, 10); | |
320 | ||
9a61bf63 | 321 | mutex_lock(&data->update_lock); |
1da177e4 LT |
322 | data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); |
323 | asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]); | |
9a61bf63 | 324 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
325 | return count; |
326 | } | |
327 | ||
328 | /* Note: we save and restore the fan minimum here, because its value is | |
329 | determined in part by the fan divisor. This follows the principle of | |
d6e05edc | 330 | least surprise; the user doesn't expect the fan minimum to change just |
1da177e4 | 331 | because the divisor changed. */ |
fad33c5f JD |
332 | static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr, |
333 | const char *buf, size_t count) | |
1da177e4 | 334 | { |
fad33c5f | 335 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 LT |
336 | struct i2c_client *client = to_i2c_client(dev); |
337 | struct asb100_data *data = i2c_get_clientdata(client); | |
338 | unsigned long min; | |
339 | unsigned long val = simple_strtoul(buf, NULL, 10); | |
340 | int reg; | |
af221931 | 341 | |
9a61bf63 | 342 | mutex_lock(&data->update_lock); |
1da177e4 LT |
343 | |
344 | min = FAN_FROM_REG(data->fan_min[nr], | |
345 | DIV_FROM_REG(data->fan_div[nr])); | |
346 | data->fan_div[nr] = DIV_TO_REG(val); | |
347 | ||
af221931 | 348 | switch (nr) { |
1da177e4 LT |
349 | case 0: /* fan 1 */ |
350 | reg = asb100_read_value(client, ASB100_REG_VID_FANDIV); | |
351 | reg = (reg & 0xcf) | (data->fan_div[0] << 4); | |
352 | asb100_write_value(client, ASB100_REG_VID_FANDIV, reg); | |
353 | break; | |
354 | ||
355 | case 1: /* fan 2 */ | |
356 | reg = asb100_read_value(client, ASB100_REG_VID_FANDIV); | |
357 | reg = (reg & 0x3f) | (data->fan_div[1] << 6); | |
358 | asb100_write_value(client, ASB100_REG_VID_FANDIV, reg); | |
359 | break; | |
360 | ||
361 | case 2: /* fan 3 */ | |
362 | reg = asb100_read_value(client, ASB100_REG_PIN); | |
363 | reg = (reg & 0x3f) | (data->fan_div[2] << 6); | |
364 | asb100_write_value(client, ASB100_REG_PIN, reg); | |
365 | break; | |
366 | } | |
367 | ||
368 | data->fan_min[nr] = | |
369 | FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); | |
370 | asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]); | |
371 | ||
9a61bf63 | 372 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
373 | |
374 | return count; | |
375 | } | |
376 | ||
377 | #define sysfs_fan(offset) \ | |
fad33c5f JD |
378 | static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ |
379 | show_fan, NULL, offset - 1); \ | |
380 | static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ | |
381 | show_fan_min, set_fan_min, offset - 1); \ | |
382 | static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \ | |
383 | show_fan_div, set_fan_div, offset - 1) | |
1da177e4 LT |
384 | |
385 | sysfs_fan(1); | |
386 | sysfs_fan(2); | |
387 | sysfs_fan(3); | |
388 | ||
1da177e4 LT |
389 | /* 4 Temp. Sensors */ |
390 | static int sprintf_temp_from_reg(u16 reg, char *buf, int nr) | |
391 | { | |
392 | int ret = 0; | |
393 | ||
394 | switch (nr) { | |
395 | case 1: case 2: | |
396 | ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg)); | |
397 | break; | |
398 | case 0: case 3: default: | |
399 | ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg)); | |
400 | break; | |
401 | } | |
402 | return ret; | |
403 | } | |
af221931 | 404 | |
1da177e4 | 405 | #define show_temp_reg(reg) \ |
fad33c5f JD |
406 | static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \ |
407 | char *buf) \ | |
1da177e4 | 408 | { \ |
fad33c5f | 409 | int nr = to_sensor_dev_attr(attr)->index; \ |
1da177e4 LT |
410 | struct asb100_data *data = asb100_update_device(dev); \ |
411 | return sprintf_temp_from_reg(data->reg[nr], buf, nr); \ | |
412 | } | |
413 | ||
414 | show_temp_reg(temp); | |
415 | show_temp_reg(temp_max); | |
416 | show_temp_reg(temp_hyst); | |
417 | ||
418 | #define set_temp_reg(REG, reg) \ | |
fad33c5f JD |
419 | static ssize_t set_##reg(struct device *dev, struct device_attribute *attr, \ |
420 | const char *buf, size_t count) \ | |
1da177e4 | 421 | { \ |
fad33c5f | 422 | int nr = to_sensor_dev_attr(attr)->index; \ |
1da177e4 LT |
423 | struct i2c_client *client = to_i2c_client(dev); \ |
424 | struct asb100_data *data = i2c_get_clientdata(client); \ | |
5bfedac0 | 425 | long val = simple_strtol(buf, NULL, 10); \ |
1da177e4 | 426 | \ |
9a61bf63 | 427 | mutex_lock(&data->update_lock); \ |
1da177e4 LT |
428 | switch (nr) { \ |
429 | case 1: case 2: \ | |
430 | data->reg[nr] = LM75_TEMP_TO_REG(val); \ | |
431 | break; \ | |
432 | case 0: case 3: default: \ | |
433 | data->reg[nr] = TEMP_TO_REG(val); \ | |
434 | break; \ | |
435 | } \ | |
436 | asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \ | |
437 | data->reg[nr]); \ | |
9a61bf63 | 438 | mutex_unlock(&data->update_lock); \ |
1da177e4 LT |
439 | return count; \ |
440 | } | |
441 | ||
442 | set_temp_reg(MAX, temp_max); | |
443 | set_temp_reg(HYST, temp_hyst); | |
444 | ||
445 | #define sysfs_temp(num) \ | |
fad33c5f JD |
446 | static SENSOR_DEVICE_ATTR(temp##num##_input, S_IRUGO, \ |
447 | show_temp, NULL, num - 1); \ | |
448 | static SENSOR_DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \ | |
449 | show_temp_max, set_temp_max, num - 1); \ | |
450 | static SENSOR_DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \ | |
451 | show_temp_hyst, set_temp_hyst, num - 1) | |
1da177e4 LT |
452 | |
453 | sysfs_temp(1); | |
454 | sysfs_temp(2); | |
455 | sysfs_temp(3); | |
456 | sysfs_temp(4); | |
457 | ||
458 | /* VID */ | |
af221931 JD |
459 | static ssize_t show_vid(struct device *dev, struct device_attribute *attr, |
460 | char *buf) | |
1da177e4 LT |
461 | { |
462 | struct asb100_data *data = asb100_update_device(dev); | |
463 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); | |
464 | } | |
465 | ||
466 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); | |
1da177e4 LT |
467 | |
468 | /* VRM */ | |
af221931 JD |
469 | static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, |
470 | char *buf) | |
1da177e4 | 471 | { |
90d6619a | 472 | struct asb100_data *data = dev_get_drvdata(dev); |
1da177e4 LT |
473 | return sprintf(buf, "%d\n", data->vrm); |
474 | } | |
475 | ||
af221931 JD |
476 | static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, |
477 | const char *buf, size_t count) | |
1da177e4 | 478 | { |
8f74efe8 JD |
479 | struct asb100_data *data = dev_get_drvdata(dev); |
480 | data->vrm = simple_strtoul(buf, NULL, 10); | |
1da177e4 LT |
481 | return count; |
482 | } | |
483 | ||
484 | /* Alarms */ | |
485 | static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm); | |
1da177e4 | 486 | |
af221931 JD |
487 | static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, |
488 | char *buf) | |
1da177e4 LT |
489 | { |
490 | struct asb100_data *data = asb100_update_device(dev); | |
68188ba7 | 491 | return sprintf(buf, "%u\n", data->alarms); |
1da177e4 LT |
492 | } |
493 | ||
494 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); | |
1da177e4 | 495 | |
636866b9 JD |
496 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
497 | char *buf) | |
498 | { | |
499 | int bitnr = to_sensor_dev_attr(attr)->index; | |
500 | struct asb100_data *data = asb100_update_device(dev); | |
501 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); | |
502 | } | |
503 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); | |
504 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); | |
505 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); | |
506 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); | |
507 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8); | |
508 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6); | |
509 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7); | |
510 | static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11); | |
511 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); | |
512 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5); | |
513 | static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13); | |
514 | ||
1da177e4 | 515 | /* 1 PWM */ |
af221931 JD |
516 | static ssize_t show_pwm1(struct device *dev, struct device_attribute *attr, |
517 | char *buf) | |
1da177e4 LT |
518 | { |
519 | struct asb100_data *data = asb100_update_device(dev); | |
520 | return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f)); | |
521 | } | |
522 | ||
af221931 JD |
523 | static ssize_t set_pwm1(struct device *dev, struct device_attribute *attr, |
524 | const char *buf, size_t count) | |
1da177e4 LT |
525 | { |
526 | struct i2c_client *client = to_i2c_client(dev); | |
527 | struct asb100_data *data = i2c_get_clientdata(client); | |
528 | unsigned long val = simple_strtoul(buf, NULL, 10); | |
529 | ||
9a61bf63 | 530 | mutex_lock(&data->update_lock); |
1da177e4 LT |
531 | data->pwm &= 0x80; /* keep the enable bit */ |
532 | data->pwm |= (0x0f & ASB100_PWM_TO_REG(val)); | |
533 | asb100_write_value(client, ASB100_REG_PWM1, data->pwm); | |
9a61bf63 | 534 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
535 | return count; |
536 | } | |
537 | ||
af221931 JD |
538 | static ssize_t show_pwm_enable1(struct device *dev, |
539 | struct device_attribute *attr, char *buf) | |
1da177e4 LT |
540 | { |
541 | struct asb100_data *data = asb100_update_device(dev); | |
542 | return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0); | |
543 | } | |
544 | ||
af221931 JD |
545 | static ssize_t set_pwm_enable1(struct device *dev, |
546 | struct device_attribute *attr, const char *buf, size_t count) | |
1da177e4 LT |
547 | { |
548 | struct i2c_client *client = to_i2c_client(dev); | |
549 | struct asb100_data *data = i2c_get_clientdata(client); | |
550 | unsigned long val = simple_strtoul(buf, NULL, 10); | |
551 | ||
9a61bf63 | 552 | mutex_lock(&data->update_lock); |
1da177e4 LT |
553 | data->pwm &= 0x0f; /* keep the duty cycle bits */ |
554 | data->pwm |= (val ? 0x80 : 0x00); | |
555 | asb100_write_value(client, ASB100_REG_PWM1, data->pwm); | |
9a61bf63 | 556 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
557 | return count; |
558 | } | |
559 | ||
560 | static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm1, set_pwm1); | |
561 | static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, | |
562 | show_pwm_enable1, set_pwm_enable1); | |
c1685f61 MH |
563 | |
564 | static struct attribute *asb100_attributes[] = { | |
fad33c5f JD |
565 | &sensor_dev_attr_in0_input.dev_attr.attr, |
566 | &sensor_dev_attr_in0_min.dev_attr.attr, | |
567 | &sensor_dev_attr_in0_max.dev_attr.attr, | |
568 | &sensor_dev_attr_in1_input.dev_attr.attr, | |
569 | &sensor_dev_attr_in1_min.dev_attr.attr, | |
570 | &sensor_dev_attr_in1_max.dev_attr.attr, | |
571 | &sensor_dev_attr_in2_input.dev_attr.attr, | |
572 | &sensor_dev_attr_in2_min.dev_attr.attr, | |
573 | &sensor_dev_attr_in2_max.dev_attr.attr, | |
574 | &sensor_dev_attr_in3_input.dev_attr.attr, | |
575 | &sensor_dev_attr_in3_min.dev_attr.attr, | |
576 | &sensor_dev_attr_in3_max.dev_attr.attr, | |
577 | &sensor_dev_attr_in4_input.dev_attr.attr, | |
578 | &sensor_dev_attr_in4_min.dev_attr.attr, | |
579 | &sensor_dev_attr_in4_max.dev_attr.attr, | |
580 | &sensor_dev_attr_in5_input.dev_attr.attr, | |
581 | &sensor_dev_attr_in5_min.dev_attr.attr, | |
582 | &sensor_dev_attr_in5_max.dev_attr.attr, | |
583 | &sensor_dev_attr_in6_input.dev_attr.attr, | |
584 | &sensor_dev_attr_in6_min.dev_attr.attr, | |
585 | &sensor_dev_attr_in6_max.dev_attr.attr, | |
586 | ||
587 | &sensor_dev_attr_fan1_input.dev_attr.attr, | |
588 | &sensor_dev_attr_fan1_min.dev_attr.attr, | |
589 | &sensor_dev_attr_fan1_div.dev_attr.attr, | |
590 | &sensor_dev_attr_fan2_input.dev_attr.attr, | |
591 | &sensor_dev_attr_fan2_min.dev_attr.attr, | |
592 | &sensor_dev_attr_fan2_div.dev_attr.attr, | |
593 | &sensor_dev_attr_fan3_input.dev_attr.attr, | |
594 | &sensor_dev_attr_fan3_min.dev_attr.attr, | |
595 | &sensor_dev_attr_fan3_div.dev_attr.attr, | |
596 | ||
597 | &sensor_dev_attr_temp1_input.dev_attr.attr, | |
598 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
599 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, | |
600 | &sensor_dev_attr_temp2_input.dev_attr.attr, | |
601 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
602 | &sensor_dev_attr_temp2_max_hyst.dev_attr.attr, | |
603 | &sensor_dev_attr_temp3_input.dev_attr.attr, | |
604 | &sensor_dev_attr_temp3_max.dev_attr.attr, | |
605 | &sensor_dev_attr_temp3_max_hyst.dev_attr.attr, | |
606 | &sensor_dev_attr_temp4_input.dev_attr.attr, | |
607 | &sensor_dev_attr_temp4_max.dev_attr.attr, | |
608 | &sensor_dev_attr_temp4_max_hyst.dev_attr.attr, | |
c1685f61 | 609 | |
636866b9 JD |
610 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
611 | &sensor_dev_attr_in1_alarm.dev_attr.attr, | |
612 | &sensor_dev_attr_in2_alarm.dev_attr.attr, | |
613 | &sensor_dev_attr_in3_alarm.dev_attr.attr, | |
614 | &sensor_dev_attr_in4_alarm.dev_attr.attr, | |
615 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, | |
616 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, | |
617 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, | |
618 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, | |
619 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, | |
620 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, | |
621 | ||
c1685f61 MH |
622 | &dev_attr_cpu0_vid.attr, |
623 | &dev_attr_vrm.attr, | |
624 | &dev_attr_alarms.attr, | |
625 | &dev_attr_pwm1.attr, | |
626 | &dev_attr_pwm1_enable.attr, | |
627 | ||
628 | NULL | |
629 | }; | |
630 | ||
631 | static const struct attribute_group asb100_group = { | |
632 | .attrs = asb100_attributes, | |
633 | }; | |
1da177e4 | 634 | |
063675b1 | 635 | static int asb100_detect_subclients(struct i2c_client *client) |
1da177e4 LT |
636 | { |
637 | int i, id, err; | |
063675b1 JD |
638 | int address = client->addr; |
639 | unsigned short sc_addr[2]; | |
af221931 | 640 | struct asb100_data *data = i2c_get_clientdata(client); |
063675b1 | 641 | struct i2c_adapter *adapter = client->adapter; |
1da177e4 LT |
642 | |
643 | id = i2c_adapter_id(adapter); | |
644 | ||
645 | if (force_subclients[0] == id && force_subclients[1] == address) { | |
646 | for (i = 2; i <= 3; i++) { | |
647 | if (force_subclients[i] < 0x48 || | |
648 | force_subclients[i] > 0x4f) { | |
af221931 | 649 | dev_err(&client->dev, "invalid subclient " |
1da177e4 LT |
650 | "address %d; must be 0x48-0x4f\n", |
651 | force_subclients[i]); | |
652 | err = -ENODEV; | |
653 | goto ERROR_SC_2; | |
654 | } | |
655 | } | |
af221931 | 656 | asb100_write_value(client, ASB100_REG_I2C_SUBADDR, |
1da177e4 | 657 | (force_subclients[2] & 0x07) | |
af221931 | 658 | ((force_subclients[3] & 0x07) << 4)); |
063675b1 JD |
659 | sc_addr[0] = force_subclients[2]; |
660 | sc_addr[1] = force_subclients[3]; | |
1da177e4 | 661 | } else { |
af221931 | 662 | int val = asb100_read_value(client, ASB100_REG_I2C_SUBADDR); |
063675b1 JD |
663 | sc_addr[0] = 0x48 + (val & 0x07); |
664 | sc_addr[1] = 0x48 + ((val >> 4) & 0x07); | |
1da177e4 LT |
665 | } |
666 | ||
063675b1 | 667 | if (sc_addr[0] == sc_addr[1]) { |
af221931 | 668 | dev_err(&client->dev, "duplicate addresses 0x%x " |
063675b1 | 669 | "for subclients\n", sc_addr[0]); |
1da177e4 LT |
670 | err = -ENODEV; |
671 | goto ERROR_SC_2; | |
672 | } | |
673 | ||
063675b1 JD |
674 | data->lm75[0] = i2c_new_dummy(adapter, sc_addr[0]); |
675 | if (!data->lm75[0]) { | |
af221931 | 676 | dev_err(&client->dev, "subclient %d registration " |
063675b1 JD |
677 | "at address 0x%x failed.\n", 1, sc_addr[0]); |
678 | err = -ENOMEM; | |
1da177e4 LT |
679 | goto ERROR_SC_2; |
680 | } | |
681 | ||
063675b1 JD |
682 | data->lm75[1] = i2c_new_dummy(adapter, sc_addr[1]); |
683 | if (!data->lm75[1]) { | |
af221931 | 684 | dev_err(&client->dev, "subclient %d registration " |
063675b1 JD |
685 | "at address 0x%x failed.\n", 2, sc_addr[1]); |
686 | err = -ENOMEM; | |
1da177e4 LT |
687 | goto ERROR_SC_3; |
688 | } | |
689 | ||
690 | return 0; | |
691 | ||
692 | /* Undo inits in case of errors */ | |
693 | ERROR_SC_3: | |
063675b1 | 694 | i2c_unregister_device(data->lm75[0]); |
1da177e4 | 695 | ERROR_SC_2: |
1da177e4 LT |
696 | return err; |
697 | } | |
698 | ||
063675b1 | 699 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
310ec792 | 700 | static int asb100_detect(struct i2c_client *client, |
063675b1 | 701 | struct i2c_board_info *info) |
1da177e4 | 702 | { |
063675b1 | 703 | struct i2c_adapter *adapter = client->adapter; |
52df6440 | 704 | int val1, val2; |
1da177e4 | 705 | |
1da177e4 LT |
706 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
707 | pr_debug("asb100.o: detect failed, " | |
708 | "smbus byte data not supported!\n"); | |
063675b1 | 709 | return -ENODEV; |
1da177e4 LT |
710 | } |
711 | ||
52df6440 JD |
712 | val1 = i2c_smbus_read_byte_data(client, ASB100_REG_BANK); |
713 | val2 = i2c_smbus_read_byte_data(client, ASB100_REG_CHIPMAN); | |
1da177e4 | 714 | |
52df6440 JD |
715 | /* If we're in bank 0 */ |
716 | if ((!(val1 & 0x07)) && | |
717 | /* Check for ASB100 ID (low byte) */ | |
718 | (((!(val1 & 0x80)) && (val2 != 0x94)) || | |
719 | /* Check for ASB100 ID (high byte ) */ | |
720 | ((val1 & 0x80) && (val2 != 0x06)))) { | |
721 | pr_debug("asb100: detect failed, bad chip id 0x%02x!\n", val2); | |
722 | return -ENODEV; | |
723 | } | |
1da177e4 | 724 | |
52df6440 | 725 | /* Put it now into bank 0 and Vendor ID High Byte */ |
063675b1 JD |
726 | i2c_smbus_write_byte_data(client, ASB100_REG_BANK, |
727 | (i2c_smbus_read_byte_data(client, ASB100_REG_BANK) & 0x78) | |
728 | | 0x80); | |
1da177e4 LT |
729 | |
730 | /* Determine the chip type. */ | |
52df6440 JD |
731 | val1 = i2c_smbus_read_byte_data(client, ASB100_REG_WCHIPID); |
732 | val2 = i2c_smbus_read_byte_data(client, ASB100_REG_CHIPMAN); | |
733 | ||
734 | if (val1 != 0x31 || val2 != 0x06) | |
735 | return -ENODEV; | |
1da177e4 | 736 | |
063675b1 | 737 | strlcpy(info->type, "asb100", I2C_NAME_SIZE); |
1da177e4 | 738 | |
063675b1 JD |
739 | return 0; |
740 | } | |
741 | ||
742 | static int asb100_probe(struct i2c_client *client, | |
743 | const struct i2c_device_id *id) | |
744 | { | |
745 | int err; | |
746 | struct asb100_data *data; | |
747 | ||
748 | data = kzalloc(sizeof(struct asb100_data), GFP_KERNEL); | |
749 | if (!data) { | |
750 | pr_debug("asb100.o: probe failed, kzalloc failed!\n"); | |
751 | err = -ENOMEM; | |
752 | goto ERROR0; | |
753 | } | |
754 | ||
755 | i2c_set_clientdata(client, data); | |
756 | mutex_init(&data->lock); | |
757 | mutex_init(&data->update_lock); | |
1da177e4 LT |
758 | |
759 | /* Attach secondary lm75 clients */ | |
063675b1 JD |
760 | err = asb100_detect_subclients(client); |
761 | if (err) | |
762 | goto ERROR1; | |
1da177e4 LT |
763 | |
764 | /* Initialize the chip */ | |
af221931 | 765 | asb100_init_client(client); |
1da177e4 LT |
766 | |
767 | /* A few vars need to be filled upon startup */ | |
af221931 JD |
768 | data->fan_min[0] = asb100_read_value(client, ASB100_REG_FAN_MIN(0)); |
769 | data->fan_min[1] = asb100_read_value(client, ASB100_REG_FAN_MIN(1)); | |
770 | data->fan_min[2] = asb100_read_value(client, ASB100_REG_FAN_MIN(2)); | |
1da177e4 LT |
771 | |
772 | /* Register sysfs hooks */ | |
af221931 | 773 | if ((err = sysfs_create_group(&client->dev.kobj, &asb100_group))) |
c1685f61 MH |
774 | goto ERROR3; |
775 | ||
af221931 | 776 | data->hwmon_dev = hwmon_device_register(&client->dev); |
1beeffe4 TJ |
777 | if (IS_ERR(data->hwmon_dev)) { |
778 | err = PTR_ERR(data->hwmon_dev); | |
c1685f61 | 779 | goto ERROR4; |
943b0830 MH |
780 | } |
781 | ||
1da177e4 LT |
782 | return 0; |
783 | ||
c1685f61 | 784 | ERROR4: |
af221931 | 785 | sysfs_remove_group(&client->dev.kobj, &asb100_group); |
943b0830 | 786 | ERROR3: |
063675b1 JD |
787 | i2c_unregister_device(data->lm75[1]); |
788 | i2c_unregister_device(data->lm75[0]); | |
1da177e4 LT |
789 | ERROR1: |
790 | kfree(data); | |
791 | ERROR0: | |
792 | return err; | |
793 | } | |
794 | ||
063675b1 | 795 | static int asb100_remove(struct i2c_client *client) |
1da177e4 | 796 | { |
943b0830 | 797 | struct asb100_data *data = i2c_get_clientdata(client); |
943b0830 | 798 | |
063675b1 JD |
799 | hwmon_device_unregister(data->hwmon_dev); |
800 | sysfs_remove_group(&client->dev.kobj, &asb100_group); | |
1da177e4 | 801 | |
063675b1 JD |
802 | i2c_unregister_device(data->lm75[1]); |
803 | i2c_unregister_device(data->lm75[0]); | |
943b0830 | 804 | |
063675b1 | 805 | kfree(data); |
1da177e4 LT |
806 | |
807 | return 0; | |
808 | } | |
809 | ||
810 | /* The SMBus locks itself, usually, but nothing may access the chip between | |
811 | bank switches. */ | |
812 | static int asb100_read_value(struct i2c_client *client, u16 reg) | |
813 | { | |
814 | struct asb100_data *data = i2c_get_clientdata(client); | |
815 | struct i2c_client *cl; | |
816 | int res, bank; | |
817 | ||
9a61bf63 | 818 | mutex_lock(&data->lock); |
1da177e4 LT |
819 | |
820 | bank = (reg >> 8) & 0x0f; | |
821 | if (bank > 2) | |
822 | /* switch banks */ | |
823 | i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank); | |
824 | ||
825 | if (bank == 0 || bank > 2) { | |
826 | res = i2c_smbus_read_byte_data(client, reg & 0xff); | |
827 | } else { | |
828 | /* switch to subclient */ | |
829 | cl = data->lm75[bank - 1]; | |
830 | ||
831 | /* convert from ISA to LM75 I2C addresses */ | |
832 | switch (reg & 0xff) { | |
833 | case 0x50: /* TEMP */ | |
af221931 | 834 | res = swab16(i2c_smbus_read_word_data(cl, 0)); |
1da177e4 LT |
835 | break; |
836 | case 0x52: /* CONFIG */ | |
837 | res = i2c_smbus_read_byte_data(cl, 1); | |
838 | break; | |
839 | case 0x53: /* HYST */ | |
af221931 | 840 | res = swab16(i2c_smbus_read_word_data(cl, 2)); |
1da177e4 LT |
841 | break; |
842 | case 0x55: /* MAX */ | |
843 | default: | |
af221931 | 844 | res = swab16(i2c_smbus_read_word_data(cl, 3)); |
1da177e4 LT |
845 | break; |
846 | } | |
847 | } | |
848 | ||
849 | if (bank > 2) | |
850 | i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0); | |
851 | ||
9a61bf63 | 852 | mutex_unlock(&data->lock); |
1da177e4 LT |
853 | |
854 | return res; | |
855 | } | |
856 | ||
857 | static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value) | |
858 | { | |
859 | struct asb100_data *data = i2c_get_clientdata(client); | |
860 | struct i2c_client *cl; | |
861 | int bank; | |
862 | ||
9a61bf63 | 863 | mutex_lock(&data->lock); |
1da177e4 LT |
864 | |
865 | bank = (reg >> 8) & 0x0f; | |
866 | if (bank > 2) | |
867 | /* switch banks */ | |
868 | i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank); | |
869 | ||
870 | if (bank == 0 || bank > 2) { | |
871 | i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff); | |
872 | } else { | |
873 | /* switch to subclient */ | |
874 | cl = data->lm75[bank - 1]; | |
875 | ||
876 | /* convert from ISA to LM75 I2C addresses */ | |
877 | switch (reg & 0xff) { | |
878 | case 0x52: /* CONFIG */ | |
879 | i2c_smbus_write_byte_data(cl, 1, value & 0xff); | |
880 | break; | |
881 | case 0x53: /* HYST */ | |
882 | i2c_smbus_write_word_data(cl, 2, swab16(value)); | |
883 | break; | |
884 | case 0x55: /* MAX */ | |
885 | i2c_smbus_write_word_data(cl, 3, swab16(value)); | |
886 | break; | |
887 | } | |
888 | } | |
889 | ||
890 | if (bank > 2) | |
891 | i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0); | |
892 | ||
9a61bf63 | 893 | mutex_unlock(&data->lock); |
1da177e4 LT |
894 | } |
895 | ||
896 | static void asb100_init_client(struct i2c_client *client) | |
897 | { | |
898 | struct asb100_data *data = i2c_get_clientdata(client); | |
1da177e4 | 899 | |
303760b4 | 900 | data->vrm = vid_which_vrm(); |
1da177e4 LT |
901 | |
902 | /* Start monitoring */ | |
af221931 | 903 | asb100_write_value(client, ASB100_REG_CONFIG, |
1da177e4 LT |
904 | (asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01); |
905 | } | |
906 | ||
907 | static struct asb100_data *asb100_update_device(struct device *dev) | |
908 | { | |
909 | struct i2c_client *client = to_i2c_client(dev); | |
910 | struct asb100_data *data = i2c_get_clientdata(client); | |
911 | int i; | |
912 | ||
9a61bf63 | 913 | mutex_lock(&data->update_lock); |
1da177e4 LT |
914 | |
915 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) | |
916 | || !data->valid) { | |
917 | ||
918 | dev_dbg(&client->dev, "starting device update...\n"); | |
919 | ||
920 | /* 7 voltage inputs */ | |
921 | for (i = 0; i < 7; i++) { | |
922 | data->in[i] = asb100_read_value(client, | |
923 | ASB100_REG_IN(i)); | |
924 | data->in_min[i] = asb100_read_value(client, | |
925 | ASB100_REG_IN_MIN(i)); | |
926 | data->in_max[i] = asb100_read_value(client, | |
927 | ASB100_REG_IN_MAX(i)); | |
928 | } | |
929 | ||
930 | /* 3 fan inputs */ | |
931 | for (i = 0; i < 3; i++) { | |
932 | data->fan[i] = asb100_read_value(client, | |
933 | ASB100_REG_FAN(i)); | |
934 | data->fan_min[i] = asb100_read_value(client, | |
935 | ASB100_REG_FAN_MIN(i)); | |
936 | } | |
937 | ||
938 | /* 4 temperature inputs */ | |
939 | for (i = 1; i <= 4; i++) { | |
940 | data->temp[i-1] = asb100_read_value(client, | |
941 | ASB100_REG_TEMP(i)); | |
942 | data->temp_max[i-1] = asb100_read_value(client, | |
943 | ASB100_REG_TEMP_MAX(i)); | |
944 | data->temp_hyst[i-1] = asb100_read_value(client, | |
945 | ASB100_REG_TEMP_HYST(i)); | |
946 | } | |
947 | ||
948 | /* VID and fan divisors */ | |
949 | i = asb100_read_value(client, ASB100_REG_VID_FANDIV); | |
950 | data->vid = i & 0x0f; | |
951 | data->vid |= (asb100_read_value(client, | |
952 | ASB100_REG_CHIPID) & 0x01) << 4; | |
953 | data->fan_div[0] = (i >> 4) & 0x03; | |
954 | data->fan_div[1] = (i >> 6) & 0x03; | |
955 | data->fan_div[2] = (asb100_read_value(client, | |
956 | ASB100_REG_PIN) >> 6) & 0x03; | |
957 | ||
958 | /* PWM */ | |
959 | data->pwm = asb100_read_value(client, ASB100_REG_PWM1); | |
960 | ||
961 | /* alarms */ | |
962 | data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) + | |
963 | (asb100_read_value(client, ASB100_REG_ALARM2) << 8); | |
964 | ||
965 | data->last_updated = jiffies; | |
966 | data->valid = 1; | |
967 | ||
968 | dev_dbg(&client->dev, "... device update complete\n"); | |
969 | } | |
970 | ||
9a61bf63 | 971 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
972 | |
973 | return data; | |
974 | } | |
975 | ||
976 | static int __init asb100_init(void) | |
977 | { | |
978 | return i2c_add_driver(&asb100_driver); | |
979 | } | |
980 | ||
981 | static void __exit asb100_exit(void) | |
982 | { | |
983 | i2c_del_driver(&asb100_driver); | |
984 | } | |
985 | ||
986 | MODULE_AUTHOR("Mark M. Hoffman <[email protected]>"); | |
987 | MODULE_DESCRIPTION("ASB100 Bach driver"); | |
988 | MODULE_LICENSE("GPL"); | |
989 | ||
990 | module_init(asb100_init); | |
991 | module_exit(asb100_exit); |