[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / i2c / chips / asb100.c
blob7f899002bc5493ca2cbbf1225dca003c6c63e6b3
1 /*
2 asb100.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
5 Copyright (C) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
7 (derived from w83781d.c)
9 Copyright (C) 1998 - 2003 Frodo Looijaard <frodol@dds.nl>,
10 Philip Edelbrock <phil@netroedge.com>, and
11 Mark Studebaker <mdsxyz123@yahoo.com>
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.
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.
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.
29 This driver supports the hardware sensor chips: Asus ASB100 and
30 ASB100-A "BACH".
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.
35 Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
36 asb100 7 3 1 4 0x31 0x0694 yes no
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 "lm75.h"
48 HISTORY:
49 2003-12-29 1.0.0 Ported from lm_sensors project for kernel 2.6
51 #define ASB100_VERSION "1.0.0"
53 /* I2C addresses to scan */
54 static unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
56 /* ISA addresses to scan (none) */
57 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
59 /* Insmod parameters */
60 SENSORS_INSMOD_1(asb100);
61 I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
62 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
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))
69 /* FAN IN registers 1-3 */
70 #define ASB100_REG_FAN(nr) (0x28 + (nr))
71 #define ASB100_REG_FAN_MIN(nr) (0x3b + (nr))
73 /* TEMPERATURE registers 1-4 */
74 static const u16 asb100_reg_temp[] = {0, 0x27, 0x150, 0x250, 0x17};
75 static const u16 asb100_reg_temp_max[] = {0, 0x39, 0x155, 0x255, 0x18};
76 static const u16 asb100_reg_temp_hyst[] = {0, 0x3a, 0x153, 0x253, 0x19};
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])
82 #define ASB100_REG_TEMP2_CONFIG 0x0152
83 #define ASB100_REG_TEMP3_CONFIG 0x0252
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
100 #define ASB100_REG_WCHIPID 0x58
102 /* bit 7 -> enable, bits 0-3 -> duty cycle */
103 #define ASB100_REG_PWM1 0x59
105 /* CONVERSIONS
106 Rounding and limit checking is only done on the TO_REG variants. */
108 /* These constants are a guess, consistent w/ w83781d */
109 #define ASB100_IN_MIN ( 0)
110 #define ASB100_IN_MAX (4080)
112 /* IN: 1/1000 V (0V to 4.08V)
113 REG: 16mV/bit */
114 static u8 IN_TO_REG(unsigned val)
116 unsigned nval = SENSORS_LIMIT(val, ASB100_IN_MIN, ASB100_IN_MAX);
117 return (nval + 8) / 16;
120 static unsigned IN_FROM_REG(u8 reg)
122 return reg * 16;
125 static u8 FAN_TO_REG(long rpm, int div)
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);
135 static int FAN_FROM_REG(u8 val, int div)
137 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
140 /* These constants are a guess, consistent w/ w83781d */
141 #define ASB100_TEMP_MIN (-128000)
142 #define ASB100_TEMP_MAX ( 127000)
144 /* TEMP: 0.001C/bit (-128C to +127C)
145 REG: 1C/bit, two's complement */
146 static u8 TEMP_TO_REG(int temp)
148 int ntemp = SENSORS_LIMIT(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX);
149 ntemp += (ntemp<0 ? -500 : 500);
150 return (u8)(ntemp / 1000);
153 static int TEMP_FROM_REG(u8 reg)
155 return (s8)reg * 1000;
158 /* PWM: 0 - 255 per sensors documentation
159 REG: (6.25% duty cycle per bit) */
160 static u8 ASB100_PWM_TO_REG(int pwm)
162 pwm = SENSORS_LIMIT(pwm, 0, 255);
163 return (u8)(pwm / 16);
166 static int ASB100_PWM_FROM_REG(u8 reg)
168 return reg * 16;
171 #define ALARMS_FROM_REG(val) (val)
173 #define DIV_FROM_REG(val) (1 << (val))
175 /* FAN DIV: 1, 2, 4, or 8 (defaults to 2)
176 REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */
177 static u8 DIV_TO_REG(long val)
179 return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1;
182 /* For each registered client, we need to keep some data in memory. That
183 data is pointed to by client->data. The structure itself is
184 dynamically allocated, at the same time the client itself is allocated. */
185 struct asb100_data {
186 struct i2c_client client;
187 struct semaphore lock;
188 enum chips type;
190 struct semaphore update_lock;
191 unsigned long last_updated; /* In jiffies */
193 /* array of 2 pointers to subclients */
194 struct i2c_client *lm75[2];
196 char valid; /* !=0 if following fields are valid */
197 u8 in[7]; /* Register value */
198 u8 in_max[7]; /* Register value */
199 u8 in_min[7]; /* Register value */
200 u8 fan[3]; /* Register value */
201 u8 fan_min[3]; /* Register value */
202 u16 temp[4]; /* Register value (0 and 3 are u8 only) */
203 u16 temp_max[4]; /* Register value (0 and 3 are u8 only) */
204 u16 temp_hyst[4]; /* Register value (0 and 3 are u8 only) */
205 u8 fan_div[3]; /* Register encoding, right justified */
206 u8 pwm; /* Register encoding */
207 u8 vid; /* Register encoding, combined */
208 u32 alarms; /* Register encoding, combined */
209 u8 vrm;
212 static int asb100_read_value(struct i2c_client *client, u16 reg);
213 static void asb100_write_value(struct i2c_client *client, u16 reg, u16 val);
215 static int asb100_attach_adapter(struct i2c_adapter *adapter);
216 static int asb100_detect(struct i2c_adapter *adapter, int address, int kind);
217 static int asb100_detach_client(struct i2c_client *client);
218 static struct asb100_data *asb100_update_device(struct device *dev);
219 static void asb100_init_client(struct i2c_client *client);
221 static struct i2c_driver asb100_driver = {
222 .owner = THIS_MODULE,
223 .name = "asb100",
224 .id = I2C_DRIVERID_ASB100,
225 .flags = I2C_DF_NOTIFY,
226 .attach_adapter = asb100_attach_adapter,
227 .detach_client = asb100_detach_client,
230 /* 7 Voltages */
231 #define show_in_reg(reg) \
232 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
234 struct asb100_data *data = asb100_update_device(dev); \
235 return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
238 show_in_reg(in)
239 show_in_reg(in_min)
240 show_in_reg(in_max)
242 #define set_in_reg(REG, reg) \
243 static ssize_t set_in_##reg(struct device *dev, const char *buf, \
244 size_t count, int nr) \
246 struct i2c_client *client = to_i2c_client(dev); \
247 struct asb100_data *data = i2c_get_clientdata(client); \
248 unsigned long val = simple_strtoul(buf, NULL, 10); \
250 down(&data->update_lock); \
251 data->in_##reg[nr] = IN_TO_REG(val); \
252 asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
253 data->in_##reg[nr]); \
254 up(&data->update_lock); \
255 return count; \
258 set_in_reg(MIN, min)
259 set_in_reg(MAX, max)
261 #define sysfs_in(offset) \
262 static ssize_t \
263 show_in##offset (struct device *dev, char *buf) \
265 return show_in(dev, buf, offset); \
267 static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
268 show_in##offset, NULL); \
269 static ssize_t \
270 show_in##offset##_min (struct device *dev, char *buf) \
272 return show_in_min(dev, buf, offset); \
274 static ssize_t \
275 show_in##offset##_max (struct device *dev, char *buf) \
277 return show_in_max(dev, buf, offset); \
279 static ssize_t set_in##offset##_min (struct device *dev, \
280 const char *buf, size_t count) \
282 return set_in_min(dev, buf, count, offset); \
284 static ssize_t set_in##offset##_max (struct device *dev, \
285 const char *buf, size_t count) \
287 return set_in_max(dev, buf, count, offset); \
289 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
290 show_in##offset##_min, set_in##offset##_min); \
291 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
292 show_in##offset##_max, set_in##offset##_max);
294 sysfs_in(0);
295 sysfs_in(1);
296 sysfs_in(2);
297 sysfs_in(3);
298 sysfs_in(4);
299 sysfs_in(5);
300 sysfs_in(6);
302 #define device_create_file_in(client, offset) do { \
303 device_create_file(&client->dev, &dev_attr_in##offset##_input); \
304 device_create_file(&client->dev, &dev_attr_in##offset##_min); \
305 device_create_file(&client->dev, &dev_attr_in##offset##_max); \
306 } while (0)
308 /* 3 Fans */
309 static ssize_t show_fan(struct device *dev, char *buf, int nr)
311 struct asb100_data *data = asb100_update_device(dev);
312 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
313 DIV_FROM_REG(data->fan_div[nr])));
316 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
318 struct asb100_data *data = asb100_update_device(dev);
319 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
320 DIV_FROM_REG(data->fan_div[nr])));
323 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
325 struct asb100_data *data = asb100_update_device(dev);
326 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
329 static ssize_t set_fan_min(struct device *dev, const char *buf,
330 size_t count, int nr)
332 struct i2c_client *client = to_i2c_client(dev);
333 struct asb100_data *data = i2c_get_clientdata(client);
334 u32 val = simple_strtoul(buf, NULL, 10);
336 down(&data->update_lock);
337 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
338 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
339 up(&data->update_lock);
340 return count;
343 /* Note: we save and restore the fan minimum here, because its value is
344 determined in part by the fan divisor. This follows the principle of
345 least suprise; the user doesn't expect the fan minimum to change just
346 because the divisor changed. */
347 static ssize_t set_fan_div(struct device *dev, const char *buf,
348 size_t count, int nr)
350 struct i2c_client *client = to_i2c_client(dev);
351 struct asb100_data *data = i2c_get_clientdata(client);
352 unsigned long min;
353 unsigned long val = simple_strtoul(buf, NULL, 10);
354 int reg;
356 down(&data->update_lock);
358 min = FAN_FROM_REG(data->fan_min[nr],
359 DIV_FROM_REG(data->fan_div[nr]));
360 data->fan_div[nr] = DIV_TO_REG(val);
362 switch(nr) {
363 case 0: /* fan 1 */
364 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
365 reg = (reg & 0xcf) | (data->fan_div[0] << 4);
366 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
367 break;
369 case 1: /* fan 2 */
370 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
371 reg = (reg & 0x3f) | (data->fan_div[1] << 6);
372 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
373 break;
375 case 2: /* fan 3 */
376 reg = asb100_read_value(client, ASB100_REG_PIN);
377 reg = (reg & 0x3f) | (data->fan_div[2] << 6);
378 asb100_write_value(client, ASB100_REG_PIN, reg);
379 break;
382 data->fan_min[nr] =
383 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
384 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
386 up(&data->update_lock);
388 return count;
391 #define sysfs_fan(offset) \
392 static ssize_t show_fan##offset(struct device *dev, char *buf) \
394 return show_fan(dev, buf, offset - 1); \
396 static ssize_t show_fan##offset##_min(struct device *dev, char *buf) \
398 return show_fan_min(dev, buf, offset - 1); \
400 static ssize_t show_fan##offset##_div(struct device *dev, char *buf) \
402 return show_fan_div(dev, buf, offset - 1); \
404 static ssize_t set_fan##offset##_min(struct device *dev, const char *buf, \
405 size_t count) \
407 return set_fan_min(dev, buf, count, offset - 1); \
409 static ssize_t set_fan##offset##_div(struct device *dev, const char *buf, \
410 size_t count) \
412 return set_fan_div(dev, buf, count, offset - 1); \
414 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
415 show_fan##offset, NULL); \
416 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
417 show_fan##offset##_min, set_fan##offset##_min); \
418 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
419 show_fan##offset##_div, set_fan##offset##_div);
421 sysfs_fan(1);
422 sysfs_fan(2);
423 sysfs_fan(3);
425 #define device_create_file_fan(client, offset) do { \
426 device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
427 device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
428 device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
429 } while (0)
431 /* 4 Temp. Sensors */
432 static int sprintf_temp_from_reg(u16 reg, char *buf, int nr)
434 int ret = 0;
436 switch (nr) {
437 case 1: case 2:
438 ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg));
439 break;
440 case 0: case 3: default:
441 ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
442 break;
444 return ret;
447 #define show_temp_reg(reg) \
448 static ssize_t show_##reg(struct device *dev, char *buf, int nr) \
450 struct asb100_data *data = asb100_update_device(dev); \
451 return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
454 show_temp_reg(temp);
455 show_temp_reg(temp_max);
456 show_temp_reg(temp_hyst);
458 #define set_temp_reg(REG, reg) \
459 static ssize_t set_##reg(struct device *dev, const char *buf, \
460 size_t count, int nr) \
462 struct i2c_client *client = to_i2c_client(dev); \
463 struct asb100_data *data = i2c_get_clientdata(client); \
464 unsigned long val = simple_strtoul(buf, NULL, 10); \
466 down(&data->update_lock); \
467 switch (nr) { \
468 case 1: case 2: \
469 data->reg[nr] = LM75_TEMP_TO_REG(val); \
470 break; \
471 case 0: case 3: default: \
472 data->reg[nr] = TEMP_TO_REG(val); \
473 break; \
475 asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
476 data->reg[nr]); \
477 up(&data->update_lock); \
478 return count; \
481 set_temp_reg(MAX, temp_max);
482 set_temp_reg(HYST, temp_hyst);
484 #define sysfs_temp(num) \
485 static ssize_t show_temp##num(struct device *dev, char *buf) \
487 return show_temp(dev, buf, num-1); \
489 static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL); \
490 static ssize_t show_temp_max##num(struct device *dev, char *buf) \
492 return show_temp_max(dev, buf, num-1); \
494 static ssize_t set_temp_max##num(struct device *dev, const char *buf, \
495 size_t count) \
497 return set_temp_max(dev, buf, count, num-1); \
499 static DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
500 show_temp_max##num, set_temp_max##num); \
501 static ssize_t show_temp_hyst##num(struct device *dev, char *buf) \
503 return show_temp_hyst(dev, buf, num-1); \
505 static ssize_t set_temp_hyst##num(struct device *dev, const char *buf, \
506 size_t count) \
508 return set_temp_hyst(dev, buf, count, num-1); \
510 static DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
511 show_temp_hyst##num, set_temp_hyst##num);
513 sysfs_temp(1);
514 sysfs_temp(2);
515 sysfs_temp(3);
516 sysfs_temp(4);
518 /* VID */
519 #define device_create_file_temp(client, num) do { \
520 device_create_file(&client->dev, &dev_attr_temp##num##_input); \
521 device_create_file(&client->dev, &dev_attr_temp##num##_max); \
522 device_create_file(&client->dev, &dev_attr_temp##num##_max_hyst); \
523 } while (0)
525 static ssize_t show_vid(struct device *dev, char *buf)
527 struct asb100_data *data = asb100_update_device(dev);
528 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
531 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
532 #define device_create_file_vid(client) \
533 device_create_file(&client->dev, &dev_attr_cpu0_vid)
535 /* VRM */
536 static ssize_t show_vrm(struct device *dev, char *buf)
538 struct asb100_data *data = asb100_update_device(dev);
539 return sprintf(buf, "%d\n", data->vrm);
542 static ssize_t set_vrm(struct device *dev, const char *buf, size_t count)
544 struct i2c_client *client = to_i2c_client(dev);
545 struct asb100_data *data = i2c_get_clientdata(client);
546 unsigned long val = simple_strtoul(buf, NULL, 10);
547 data->vrm = val;
548 return count;
551 /* Alarms */
552 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
553 #define device_create_file_vrm(client) \
554 device_create_file(&client->dev, &dev_attr_vrm);
556 static ssize_t show_alarms(struct device *dev, char *buf)
558 struct asb100_data *data = asb100_update_device(dev);
559 return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->alarms));
562 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
563 #define device_create_file_alarms(client) \
564 device_create_file(&client->dev, &dev_attr_alarms)
566 /* 1 PWM */
567 static ssize_t show_pwm1(struct device *dev, char *buf)
569 struct asb100_data *data = asb100_update_device(dev);
570 return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
573 static ssize_t set_pwm1(struct device *dev, const char *buf, size_t count)
575 struct i2c_client *client = to_i2c_client(dev);
576 struct asb100_data *data = i2c_get_clientdata(client);
577 unsigned long val = simple_strtoul(buf, NULL, 10);
579 down(&data->update_lock);
580 data->pwm &= 0x80; /* keep the enable bit */
581 data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
582 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
583 up(&data->update_lock);
584 return count;
587 static ssize_t show_pwm_enable1(struct device *dev, char *buf)
589 struct asb100_data *data = asb100_update_device(dev);
590 return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
593 static ssize_t set_pwm_enable1(struct device *dev, const char *buf,
594 size_t count)
596 struct i2c_client *client = to_i2c_client(dev);
597 struct asb100_data *data = i2c_get_clientdata(client);
598 unsigned long val = simple_strtoul(buf, NULL, 10);
600 down(&data->update_lock);
601 data->pwm &= 0x0f; /* keep the duty cycle bits */
602 data->pwm |= (val ? 0x80 : 0x00);
603 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
604 up(&data->update_lock);
605 return count;
608 static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm1, set_pwm1);
609 static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
610 show_pwm_enable1, set_pwm_enable1);
611 #define device_create_file_pwm1(client) do { \
612 device_create_file(&new_client->dev, &dev_attr_pwm1); \
613 device_create_file(&new_client->dev, &dev_attr_pwm1_enable); \
614 } while (0)
616 /* This function is called when:
617 asb100_driver is inserted (when this module is loaded), for each
618 available adapter
619 when a new adapter is inserted (and asb100_driver is still present)
621 static int asb100_attach_adapter(struct i2c_adapter *adapter)
623 if (!(adapter->class & I2C_CLASS_HWMON))
624 return 0;
625 return i2c_detect(adapter, &addr_data, asb100_detect);
628 static int asb100_detect_subclients(struct i2c_adapter *adapter, int address,
629 int kind, struct i2c_client *new_client)
631 int i, id, err;
632 struct asb100_data *data = i2c_get_clientdata(new_client);
634 data->lm75[0] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
635 if (!(data->lm75[0])) {
636 err = -ENOMEM;
637 goto ERROR_SC_0;
639 memset(data->lm75[0], 0x00, sizeof(struct i2c_client));
641 data->lm75[1] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
642 if (!(data->lm75[1])) {
643 err = -ENOMEM;
644 goto ERROR_SC_1;
646 memset(data->lm75[1], 0x00, sizeof(struct i2c_client));
648 id = i2c_adapter_id(adapter);
650 if (force_subclients[0] == id && force_subclients[1] == address) {
651 for (i = 2; i <= 3; i++) {
652 if (force_subclients[i] < 0x48 ||
653 force_subclients[i] > 0x4f) {
654 dev_err(&new_client->dev, "invalid subclient "
655 "address %d; must be 0x48-0x4f\n",
656 force_subclients[i]);
657 err = -ENODEV;
658 goto ERROR_SC_2;
661 asb100_write_value(new_client, ASB100_REG_I2C_SUBADDR,
662 (force_subclients[2] & 0x07) |
663 ((force_subclients[3] & 0x07) <<4));
664 data->lm75[0]->addr = force_subclients[2];
665 data->lm75[1]->addr = force_subclients[3];
666 } else {
667 int val = asb100_read_value(new_client, ASB100_REG_I2C_SUBADDR);
668 data->lm75[0]->addr = 0x48 + (val & 0x07);
669 data->lm75[1]->addr = 0x48 + ((val >> 4) & 0x07);
672 if(data->lm75[0]->addr == data->lm75[1]->addr) {
673 dev_err(&new_client->dev, "duplicate addresses 0x%x "
674 "for subclients\n", data->lm75[0]->addr);
675 err = -ENODEV;
676 goto ERROR_SC_2;
679 for (i = 0; i <= 1; i++) {
680 i2c_set_clientdata(data->lm75[i], NULL);
681 data->lm75[i]->adapter = adapter;
682 data->lm75[i]->driver = &asb100_driver;
683 data->lm75[i]->flags = 0;
684 strlcpy(data->lm75[i]->name, "asb100 subclient", I2C_NAME_SIZE);
687 if ((err = i2c_attach_client(data->lm75[0]))) {
688 dev_err(&new_client->dev, "subclient %d registration "
689 "at address 0x%x failed.\n", i, data->lm75[0]->addr);
690 goto ERROR_SC_2;
693 if ((err = i2c_attach_client(data->lm75[1]))) {
694 dev_err(&new_client->dev, "subclient %d registration "
695 "at address 0x%x failed.\n", i, data->lm75[1]->addr);
696 goto ERROR_SC_3;
699 return 0;
701 /* Undo inits in case of errors */
702 ERROR_SC_3:
703 i2c_detach_client(data->lm75[0]);
704 ERROR_SC_2:
705 kfree(data->lm75[1]);
706 ERROR_SC_1:
707 kfree(data->lm75[0]);
708 ERROR_SC_0:
709 return err;
712 static int asb100_detect(struct i2c_adapter *adapter, int address, int kind)
714 int err;
715 struct i2c_client *new_client;
716 struct asb100_data *data;
718 /* asb100 is SMBus only */
719 if (i2c_is_isa_adapter(adapter)) {
720 pr_debug("asb100.o: detect failed, "
721 "cannot attach to legacy adapter!\n");
722 err = -ENODEV;
723 goto ERROR0;
726 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
727 pr_debug("asb100.o: detect failed, "
728 "smbus byte data not supported!\n");
729 err = -ENODEV;
730 goto ERROR0;
733 /* OK. For now, we presume we have a valid client. We now create the
734 client structure, even though we cannot fill it completely yet.
735 But it allows us to access asb100_{read,write}_value. */
737 if (!(data = kmalloc(sizeof(struct asb100_data), GFP_KERNEL))) {
738 pr_debug("asb100.o: detect failed, kmalloc failed!\n");
739 err = -ENOMEM;
740 goto ERROR0;
742 memset(data, 0, sizeof(struct asb100_data));
744 new_client = &data->client;
745 init_MUTEX(&data->lock);
746 i2c_set_clientdata(new_client, data);
747 new_client->addr = address;
748 new_client->adapter = adapter;
749 new_client->driver = &asb100_driver;
750 new_client->flags = 0;
752 /* Now, we do the remaining detection. */
754 /* The chip may be stuck in some other bank than bank 0. This may
755 make reading other information impossible. Specify a force=... or
756 force_*=... parameter, and the chip will be reset to the right
757 bank. */
758 if (kind < 0) {
760 int val1 = asb100_read_value(new_client, ASB100_REG_BANK);
761 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
763 /* If we're in bank 0 */
764 if ( (!(val1 & 0x07)) &&
765 /* Check for ASB100 ID (low byte) */
766 ( ((!(val1 & 0x80)) && (val2 != 0x94)) ||
767 /* Check for ASB100 ID (high byte ) */
768 ((val1 & 0x80) && (val2 != 0x06)) ) ) {
769 pr_debug("asb100.o: detect failed, "
770 "bad chip id 0x%02x!\n", val2);
771 err = -ENODEV;
772 goto ERROR1;
775 } /* kind < 0 */
777 /* We have either had a force parameter, or we have already detected
778 Winbond. Put it now into bank 0 and Vendor ID High Byte */
779 asb100_write_value(new_client, ASB100_REG_BANK,
780 (asb100_read_value(new_client, ASB100_REG_BANK) & 0x78) | 0x80);
782 /* Determine the chip type. */
783 if (kind <= 0) {
784 int val1 = asb100_read_value(new_client, ASB100_REG_WCHIPID);
785 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
787 if ((val1 == 0x31) && (val2 == 0x06))
788 kind = asb100;
789 else {
790 if (kind == 0)
791 dev_warn(&new_client->dev, "ignoring "
792 "'force' parameter for unknown chip "
793 "at adapter %d, address 0x%02x.\n",
794 i2c_adapter_id(adapter), address);
795 err = -ENODEV;
796 goto ERROR1;
800 /* Fill in remaining client fields and put it into the global list */
801 strlcpy(new_client->name, "asb100", I2C_NAME_SIZE);
802 data->type = kind;
804 data->valid = 0;
805 init_MUTEX(&data->update_lock);
807 /* Tell the I2C layer a new client has arrived */
808 if ((err = i2c_attach_client(new_client)))
809 goto ERROR1;
811 /* Attach secondary lm75 clients */
812 if ((err = asb100_detect_subclients(adapter, address, kind,
813 new_client)))
814 goto ERROR2;
816 /* Initialize the chip */
817 asb100_init_client(new_client);
819 /* A few vars need to be filled upon startup */
820 data->fan_min[0] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(0));
821 data->fan_min[1] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(1));
822 data->fan_min[2] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(2));
824 /* Register sysfs hooks */
825 device_create_file_in(new_client, 0);
826 device_create_file_in(new_client, 1);
827 device_create_file_in(new_client, 2);
828 device_create_file_in(new_client, 3);
829 device_create_file_in(new_client, 4);
830 device_create_file_in(new_client, 5);
831 device_create_file_in(new_client, 6);
833 device_create_file_fan(new_client, 1);
834 device_create_file_fan(new_client, 2);
835 device_create_file_fan(new_client, 3);
837 device_create_file_temp(new_client, 1);
838 device_create_file_temp(new_client, 2);
839 device_create_file_temp(new_client, 3);
840 device_create_file_temp(new_client, 4);
842 device_create_file_vid(new_client);
843 device_create_file_vrm(new_client);
845 device_create_file_alarms(new_client);
847 device_create_file_pwm1(new_client);
849 return 0;
851 ERROR2:
852 i2c_detach_client(new_client);
853 ERROR1:
854 kfree(data);
855 ERROR0:
856 return err;
859 static int asb100_detach_client(struct i2c_client *client)
861 int err;
863 if ((err = i2c_detach_client(client))) {
864 dev_err(&client->dev, "client deregistration failed; "
865 "client not detached.\n");
866 return err;
869 if (i2c_get_clientdata(client)==NULL) {
870 /* subclients */
871 kfree(client);
872 } else {
873 /* main client */
874 kfree(i2c_get_clientdata(client));
877 return 0;
880 /* The SMBus locks itself, usually, but nothing may access the chip between
881 bank switches. */
882 static int asb100_read_value(struct i2c_client *client, u16 reg)
884 struct asb100_data *data = i2c_get_clientdata(client);
885 struct i2c_client *cl;
886 int res, bank;
888 down(&data->lock);
890 bank = (reg >> 8) & 0x0f;
891 if (bank > 2)
892 /* switch banks */
893 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
895 if (bank == 0 || bank > 2) {
896 res = i2c_smbus_read_byte_data(client, reg & 0xff);
897 } else {
898 /* switch to subclient */
899 cl = data->lm75[bank - 1];
901 /* convert from ISA to LM75 I2C addresses */
902 switch (reg & 0xff) {
903 case 0x50: /* TEMP */
904 res = swab16(i2c_smbus_read_word_data (cl, 0));
905 break;
906 case 0x52: /* CONFIG */
907 res = i2c_smbus_read_byte_data(cl, 1);
908 break;
909 case 0x53: /* HYST */
910 res = swab16(i2c_smbus_read_word_data (cl, 2));
911 break;
912 case 0x55: /* MAX */
913 default:
914 res = swab16(i2c_smbus_read_word_data (cl, 3));
915 break;
919 if (bank > 2)
920 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
922 up(&data->lock);
924 return res;
927 static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
929 struct asb100_data *data = i2c_get_clientdata(client);
930 struct i2c_client *cl;
931 int bank;
933 down(&data->lock);
935 bank = (reg >> 8) & 0x0f;
936 if (bank > 2)
937 /* switch banks */
938 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
940 if (bank == 0 || bank > 2) {
941 i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
942 } else {
943 /* switch to subclient */
944 cl = data->lm75[bank - 1];
946 /* convert from ISA to LM75 I2C addresses */
947 switch (reg & 0xff) {
948 case 0x52: /* CONFIG */
949 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
950 break;
951 case 0x53: /* HYST */
952 i2c_smbus_write_word_data(cl, 2, swab16(value));
953 break;
954 case 0x55: /* MAX */
955 i2c_smbus_write_word_data(cl, 3, swab16(value));
956 break;
960 if (bank > 2)
961 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
963 up(&data->lock);
966 static void asb100_init_client(struct i2c_client *client)
968 struct asb100_data *data = i2c_get_clientdata(client);
969 int vid = 0;
971 vid = asb100_read_value(client, ASB100_REG_VID_FANDIV) & 0x0f;
972 vid |= (asb100_read_value(client, ASB100_REG_CHIPID) & 0x01) << 4;
973 data->vrm = i2c_which_vrm();
974 vid = vid_from_reg(vid, data->vrm);
976 /* Start monitoring */
977 asb100_write_value(client, ASB100_REG_CONFIG,
978 (asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
981 static struct asb100_data *asb100_update_device(struct device *dev)
983 struct i2c_client *client = to_i2c_client(dev);
984 struct asb100_data *data = i2c_get_clientdata(client);
985 int i;
987 down(&data->update_lock);
989 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
990 || !data->valid) {
992 dev_dbg(&client->dev, "starting device update...\n");
994 /* 7 voltage inputs */
995 for (i = 0; i < 7; i++) {
996 data->in[i] = asb100_read_value(client,
997 ASB100_REG_IN(i));
998 data->in_min[i] = asb100_read_value(client,
999 ASB100_REG_IN_MIN(i));
1000 data->in_max[i] = asb100_read_value(client,
1001 ASB100_REG_IN_MAX(i));
1004 /* 3 fan inputs */
1005 for (i = 0; i < 3; i++) {
1006 data->fan[i] = asb100_read_value(client,
1007 ASB100_REG_FAN(i));
1008 data->fan_min[i] = asb100_read_value(client,
1009 ASB100_REG_FAN_MIN(i));
1012 /* 4 temperature inputs */
1013 for (i = 1; i <= 4; i++) {
1014 data->temp[i-1] = asb100_read_value(client,
1015 ASB100_REG_TEMP(i));
1016 data->temp_max[i-1] = asb100_read_value(client,
1017 ASB100_REG_TEMP_MAX(i));
1018 data->temp_hyst[i-1] = asb100_read_value(client,
1019 ASB100_REG_TEMP_HYST(i));
1022 /* VID and fan divisors */
1023 i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
1024 data->vid = i & 0x0f;
1025 data->vid |= (asb100_read_value(client,
1026 ASB100_REG_CHIPID) & 0x01) << 4;
1027 data->fan_div[0] = (i >> 4) & 0x03;
1028 data->fan_div[1] = (i >> 6) & 0x03;
1029 data->fan_div[2] = (asb100_read_value(client,
1030 ASB100_REG_PIN) >> 6) & 0x03;
1032 /* PWM */
1033 data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
1035 /* alarms */
1036 data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
1037 (asb100_read_value(client, ASB100_REG_ALARM2) << 8);
1039 data->last_updated = jiffies;
1040 data->valid = 1;
1042 dev_dbg(&client->dev, "... device update complete\n");
1045 up(&data->update_lock);
1047 return data;
1050 static int __init asb100_init(void)
1052 return i2c_add_driver(&asb100_driver);
1055 static void __exit asb100_exit(void)
1057 i2c_del_driver(&asb100_driver);
1060 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1061 MODULE_DESCRIPTION("ASB100 Bach driver");
1062 MODULE_LICENSE("GPL");
1064 module_init(asb100_init);
1065 module_exit(asb100_exit);