2 asb100.c - Part of lm_sensors, Linux kernel modules for hardware
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
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/config.h>
40 #include <linux/module.h>
41 #include <linux/slab.h>
42 #include <linux/ioport.h>
43 #include <linux/types.h>
44 #include <linux/i2c.h>
45 #include <linux/i2c-sensor.h>
46 #include <linux/i2c-vid.h>
47 #include <linux/init.h>
48 #include <asm/errno.h>
54 2003-12-29 1.0.0 Ported from lm_sensors project for kernel 2.6
56 #define ASB100_VERSION "1.0.0"
58 /* I2C addresses to scan */
59 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
60 static unsigned short normal_i2c_range[] = { 0x28, 0x2f, I2C_CLIENT_END };
62 /* ISA addresses to scan (none) */
63 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
64 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
66 /* default VRM to 9.0 instead of 8.2 */
67 #define ASB100_DEFAULT_VRM 90
69 /* Insmod parameters */
70 SENSORS_INSMOD_1(asb100);
71 I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
72 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
74 /* Voltage IN registers 0-6 */
75 #define ASB100_REG_IN(nr) (0x20 + (nr))
76 #define ASB100_REG_IN_MAX(nr) (0x2b + (nr * 2))
77 #define ASB100_REG_IN_MIN(nr) (0x2c + (nr * 2))
79 /* FAN IN registers 1-3 */
80 #define ASB100_REG_FAN(nr) (0x28 + (nr))
81 #define ASB100_REG_FAN_MIN(nr) (0x3b + (nr))
83 /* TEMPERATURE registers 1-4 */
84 static const u16 asb100_reg_temp[] = {0, 0x27, 0x150, 0x250, 0x17};
85 static const u16 asb100_reg_temp_max[] = {0, 0x39, 0x155, 0x255, 0x18};
86 static const u16 asb100_reg_temp_hyst[] = {0, 0x3a, 0x153, 0x253, 0x19};
88 #define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])
89 #define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])
90 #define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])
92 #define ASB100_REG_TEMP2_CONFIG 0x0152
93 #define ASB100_REG_TEMP3_CONFIG 0x0252
96 #define ASB100_REG_CONFIG 0x40
97 #define ASB100_REG_ALARM1 0x41
98 #define ASB100_REG_ALARM2 0x42
99 #define ASB100_REG_SMIM1 0x43
100 #define ASB100_REG_SMIM2 0x44
101 #define ASB100_REG_VID_FANDIV 0x47
102 #define ASB100_REG_I2C_ADDR 0x48
103 #define ASB100_REG_CHIPID 0x49
104 #define ASB100_REG_I2C_SUBADDR 0x4a
105 #define ASB100_REG_PIN 0x4b
106 #define ASB100_REG_IRQ 0x4c
107 #define ASB100_REG_BANK 0x4e
108 #define ASB100_REG_CHIPMAN 0x4f
110 #define ASB100_REG_WCHIPID 0x58
112 /* bit 7 -> enable, bits 0-3 -> duty cycle */
113 #define ASB100_REG_PWM1 0x59
116 Rounding and limit checking is only done on the TO_REG variants. */
118 /* These constants are a guess, consistent w/ w83781d */
119 #define ASB100_IN_MIN ( 0)
120 #define ASB100_IN_MAX (4080)
122 /* IN: 1/1000 V (0V to 4.08V)
124 static u8 IN_TO_REG(unsigned val)
126 unsigned nval = SENSORS_LIMIT(val, ASB100_IN_MIN, ASB100_IN_MAX);
127 return (nval + 8) / 16;
130 static unsigned IN_FROM_REG(u8 reg)
135 static u8 FAN_TO_REG(long rpm, int div)
141 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
142 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
145 static int FAN_FROM_REG(u8 val, int div)
147 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
150 /* These constants are a guess, consistent w/ w83781d */
151 #define ASB100_TEMP_MIN (-128000)
152 #define ASB100_TEMP_MAX ( 127000)
154 /* TEMP: 0.001C/bit (-128C to +127C)
155 REG: 1C/bit, two's complement */
156 static u8 TEMP_TO_REG(int temp)
158 int ntemp = SENSORS_LIMIT(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX);
159 ntemp += (ntemp<0 ? -500 : 500);
160 return (u8)(ntemp / 1000);
163 static int TEMP_FROM_REG(u8 reg)
165 return (s8)reg * 1000;
168 /* PWM: 0 - 255 per sensors documentation
169 REG: (6.25% duty cycle per bit) */
170 static u8 ASB100_PWM_TO_REG(int pwm)
172 pwm = SENSORS_LIMIT(pwm, 0, 255);
173 return (u8)(pwm / 16);
176 static int ASB100_PWM_FROM_REG(u8 reg)
181 #define ALARMS_FROM_REG(val) (val)
183 #define DIV_FROM_REG(val) (1 << (val))
185 /* FAN DIV: 1, 2, 4, or 8 (defaults to 2)
186 REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */
187 static u8 DIV_TO_REG(long val)
189 return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1;
192 /* For each registered client, we need to keep some data in memory. That
193 data is pointed to by client->data. The structure itself is
194 dynamically allocated, at the same time the client itself is allocated. */
196 struct i2c_client client;
197 struct semaphore lock;
200 struct semaphore update_lock;
201 unsigned long last_updated; /* In jiffies */
203 /* array of 2 pointers to subclients */
204 struct i2c_client *lm75[2];
206 char valid; /* !=0 if following fields are valid */
207 u8 in[7]; /* Register value */
208 u8 in_max[7]; /* Register value */
209 u8 in_min[7]; /* Register value */
210 u8 fan[3]; /* Register value */
211 u8 fan_min[3]; /* Register value */
212 u16 temp[4]; /* Register value (0 and 3 are u8 only) */
213 u16 temp_max[4]; /* Register value (0 and 3 are u8 only) */
214 u16 temp_hyst[4]; /* Register value (0 and 3 are u8 only) */
215 u8 fan_div[3]; /* Register encoding, right justified */
216 u8 pwm; /* Register encoding */
217 u8 vid; /* Register encoding, combined */
218 u32 alarms; /* Register encoding, combined */
222 static int asb100_read_value(struct i2c_client *client, u16 reg);
223 static void asb100_write_value(struct i2c_client *client, u16 reg, u16 val);
225 static int asb100_attach_adapter(struct i2c_adapter *adapter);
226 static int asb100_detect(struct i2c_adapter *adapter, int address, int kind);
227 static int asb100_detach_client(struct i2c_client *client);
228 static struct asb100_data *asb100_update_device(struct device *dev);
229 static void asb100_init_client(struct i2c_client *client);
231 static struct i2c_driver asb100_driver = {
232 .owner = THIS_MODULE,
234 .id = I2C_DRIVERID_ASB100,
235 .flags = I2C_DF_NOTIFY,
236 .attach_adapter = asb100_attach_adapter,
237 .detach_client = asb100_detach_client,
241 #define show_in_reg(reg) \
242 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
244 struct asb100_data *data = asb100_update_device(dev); \
245 return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
252 #define set_in_reg(REG, reg) \
253 static ssize_t set_in_##reg(struct device *dev, const char *buf, \
254 size_t count, int nr) \
256 struct i2c_client *client = to_i2c_client(dev); \
257 struct asb100_data *data = i2c_get_clientdata(client); \
258 unsigned long val = simple_strtoul(buf, NULL, 10); \
259 data->in_##reg[nr] = IN_TO_REG(val); \
260 asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
261 data->in_##reg[nr]); \
268 #define sysfs_in(offset) \
270 show_in##offset (struct device *dev, char *buf) \
272 return show_in(dev, buf, 0x##offset); \
274 static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
275 show_in##offset, NULL) \
277 show_in##offset##_min (struct device *dev, char *buf) \
279 return show_in_min(dev, buf, 0x##offset); \
282 show_in##offset##_max (struct device *dev, char *buf) \
284 return show_in_max(dev, buf, 0x##offset); \
286 static ssize_t set_in##offset##_min (struct device *dev, \
287 const char *buf, size_t count) \
289 return set_in_min(dev, buf, count, 0x##offset); \
291 static ssize_t set_in##offset##_max (struct device *dev, \
292 const char *buf, size_t count) \
294 return set_in_max(dev, buf, count, 0x##offset); \
296 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
297 show_in##offset##_min, set_in##offset##_min) \
298 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
299 show_in##offset##_max, set_in##offset##_max)
309 #define device_create_file_in(client, offset) do { \
310 device_create_file(&client->dev, &dev_attr_in##offset##_input); \
311 device_create_file(&client->dev, &dev_attr_in##offset##_min); \
312 device_create_file(&client->dev, &dev_attr_in##offset##_max); \
316 static ssize_t show_fan(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[nr],
320 DIV_FROM_REG(data->fan_div[nr])));
323 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
325 struct asb100_data *data = asb100_update_device(dev);
326 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
327 DIV_FROM_REG(data->fan_div[nr])));
330 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
332 struct asb100_data *data = asb100_update_device(dev);
333 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
336 static ssize_t set_fan_min(struct device *dev, const char *buf,
337 size_t count, int nr)
339 struct i2c_client *client = to_i2c_client(dev);
340 struct asb100_data *data = i2c_get_clientdata(client);
341 u32 val = simple_strtoul(buf, NULL, 10);
342 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
343 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
347 /* Note: we save and restore the fan minimum here, because its value is
348 determined in part by the fan divisor. This follows the principle of
349 least suprise; the user doesn't expect the fan minimum to change just
350 because the divisor changed. */
351 static ssize_t set_fan_div(struct device *dev, const char *buf,
352 size_t count, int nr)
354 struct i2c_client *client = to_i2c_client(dev);
355 struct asb100_data *data = i2c_get_clientdata(client);
356 unsigned long min = FAN_FROM_REG(data->fan_min[nr],
357 DIV_FROM_REG(data->fan_div[nr]));
358 unsigned long val = simple_strtoul(buf, NULL, 10);
361 data->fan_div[nr] = DIV_TO_REG(val);
365 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
366 reg = (reg & 0xcf) | (data->fan_div[0] << 4);
367 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
371 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
372 reg = (reg & 0x3f) | (data->fan_div[1] << 6);
373 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
377 reg = asb100_read_value(client, ASB100_REG_PIN);
378 reg = (reg & 0x3f) | (data->fan_div[2] << 6);
379 asb100_write_value(client, ASB100_REG_PIN, reg);
384 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
385 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
389 #define sysfs_fan(offset) \
390 static ssize_t show_fan##offset(struct device *dev, char *buf) \
392 return show_fan(dev, buf, offset - 1); \
394 static ssize_t show_fan##offset##_min(struct device *dev, char *buf) \
396 return show_fan_min(dev, buf, offset - 1); \
398 static ssize_t show_fan##offset##_div(struct device *dev, char *buf) \
400 return show_fan_div(dev, buf, offset - 1); \
402 static ssize_t set_fan##offset##_min(struct device *dev, const char *buf, \
405 return set_fan_min(dev, buf, count, offset - 1); \
407 static ssize_t set_fan##offset##_div(struct device *dev, const char *buf, \
410 return set_fan_div(dev, buf, count, offset - 1); \
412 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
413 show_fan##offset, NULL) \
414 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
415 show_fan##offset##_min, set_fan##offset##_min) \
416 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
417 show_fan##offset##_div, set_fan##offset##_div)
423 #define device_create_file_fan(client, offset) do { \
424 device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
425 device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
426 device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
429 /* 4 Temp. Sensors */
430 static int sprintf_temp_from_reg(u16 reg, char *buf, int nr)
436 ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg));
438 case 0: case 3: default:
439 ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
445 #define show_temp_reg(reg) \
446 static ssize_t show_##reg(struct device *dev, char *buf, int nr) \
448 struct asb100_data *data = asb100_update_device(dev); \
449 return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
453 show_temp_reg(temp_max)
454 show_temp_reg(temp_hyst)
456 #define set_temp_reg(REG, reg) \
457 static ssize_t set_##reg(struct device *dev, const char *buf, \
458 size_t count, int nr) \
460 struct i2c_client *client = to_i2c_client(dev); \
461 struct asb100_data *data = i2c_get_clientdata(client); \
462 unsigned long val = simple_strtoul(buf, NULL, 10); \
465 data->reg[nr] = LM75_TEMP_TO_REG(val); \
467 case 0: case 3: default: \
468 data->reg[nr] = TEMP_TO_REG(val); \
471 asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
476 set_temp_reg(MAX, temp_max)
477 set_temp_reg(HYST, temp_hyst)
479 #define sysfs_temp(num) \
480 static ssize_t show_temp##num(struct device *dev, char *buf) \
482 return show_temp(dev, buf, num-1); \
484 static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL) \
485 static ssize_t show_temp_max##num(struct device *dev, char *buf) \
487 return show_temp_max(dev, buf, num-1); \
489 static ssize_t set_temp_max##num(struct device *dev, const char *buf, \
492 return set_temp_max(dev, buf, count, num-1); \
494 static DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
495 show_temp_max##num, set_temp_max##num) \
496 static ssize_t show_temp_hyst##num(struct device *dev, char *buf) \
498 return show_temp_hyst(dev, buf, num-1); \
500 static ssize_t set_temp_hyst##num(struct device *dev, const char *buf, \
503 return set_temp_hyst(dev, buf, count, num-1); \
505 static DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
506 show_temp_hyst##num, set_temp_hyst##num)
514 #define device_create_file_temp(client, num) do { \
515 device_create_file(&client->dev, &dev_attr_temp##num##_input); \
516 device_create_file(&client->dev, &dev_attr_temp##num##_max); \
517 device_create_file(&client->dev, &dev_attr_temp##num##_max_hyst); \
520 static ssize_t show_vid(struct device *dev, char *buf)
522 struct asb100_data *data = asb100_update_device(dev);
523 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
526 static DEVICE_ATTR(in0_ref, S_IRUGO, show_vid, NULL)
527 #define device_create_file_vid(client) \
528 device_create_file(&client->dev, &dev_attr_in0_ref)
531 static ssize_t show_vrm(struct device *dev, char *buf)
533 struct asb100_data *data = asb100_update_device(dev);
534 return sprintf(buf, "%d\n", data->vrm);
537 static ssize_t set_vrm(struct device *dev, const char *buf, size_t count)
539 struct i2c_client *client = to_i2c_client(dev);
540 struct asb100_data *data = i2c_get_clientdata(client);
541 unsigned long val = simple_strtoul(buf, NULL, 10);
547 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm)
548 #define device_create_file_vrm(client) \
549 device_create_file(&client->dev, &dev_attr_vrm);
551 static ssize_t show_alarms(struct device *dev, char *buf)
553 struct asb100_data *data = asb100_update_device(dev);
554 return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->alarms));
557 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL)
558 #define device_create_file_alarms(client) \
559 device_create_file(&client->dev, &dev_attr_alarms)
562 static ssize_t show_pwm1(struct device *dev, char *buf)
564 struct asb100_data *data = asb100_update_device(dev);
565 return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
568 static ssize_t set_pwm1(struct device *dev, const char *buf, size_t count)
570 struct i2c_client *client = to_i2c_client(dev);
571 struct asb100_data *data = i2c_get_clientdata(client);
572 unsigned long val = simple_strtoul(buf, NULL, 10);
573 data->pwm &= 0x80; /* keep the enable bit */
574 data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
575 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
579 static ssize_t show_pwm_enable1(struct device *dev, char *buf)
581 struct asb100_data *data = asb100_update_device(dev);
582 return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
585 static ssize_t set_pwm_enable1(struct device *dev, const char *buf,
588 struct i2c_client *client = to_i2c_client(dev);
589 struct asb100_data *data = i2c_get_clientdata(client);
590 unsigned long val = simple_strtoul(buf, NULL, 10);
591 data->pwm &= 0x0f; /* keep the duty cycle bits */
592 data->pwm |= (val ? 0x80 : 0x00);
593 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
597 static DEVICE_ATTR(fan1_pwm, S_IRUGO | S_IWUSR, show_pwm1, set_pwm1)
598 static DEVICE_ATTR(fan1_pwm_enable, S_IRUGO | S_IWUSR,
599 show_pwm_enable1, set_pwm_enable1)
600 #define device_create_file_pwm1(client) do { \
601 device_create_file(&new_client->dev, &dev_attr_fan1_pwm); \
602 device_create_file(&new_client->dev, &dev_attr_fan1_pwm_enable); \
605 /* This function is called when:
606 asb100_driver is inserted (when this module is loaded), for each
608 when a new adapter is inserted (and asb100_driver is still present)
610 static int asb100_attach_adapter(struct i2c_adapter *adapter)
612 if (!(adapter->class & I2C_CLASS_HWMON))
614 return i2c_detect(adapter, &addr_data, asb100_detect);
617 static int asb100_detect_subclients(struct i2c_adapter *adapter, int address,
618 int kind, struct i2c_client *new_client)
621 struct asb100_data *data = i2c_get_clientdata(new_client);
623 data->lm75[0] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
624 if (!(data->lm75[0])) {
628 memset(data->lm75[0], 0x00, sizeof(struct i2c_client));
630 data->lm75[1] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
631 if (!(data->lm75[1])) {
635 memset(data->lm75[1], 0x00, sizeof(struct i2c_client));
637 id = i2c_adapter_id(adapter);
639 if (force_subclients[0] == id && force_subclients[1] == address) {
640 for (i = 2; i <= 3; i++) {
641 if (force_subclients[i] < 0x48 ||
642 force_subclients[i] > 0x4f) {
643 dev_err(&new_client->dev, "invalid subclient "
644 "address %d; must be 0x48-0x4f\n",
645 force_subclients[i]);
650 asb100_write_value(new_client, ASB100_REG_I2C_SUBADDR,
651 (force_subclients[2] & 0x07) |
652 ((force_subclients[3] & 0x07) <<4));
653 data->lm75[0]->addr = force_subclients[2];
654 data->lm75[1]->addr = force_subclients[3];
656 int val = asb100_read_value(new_client, ASB100_REG_I2C_SUBADDR);
657 data->lm75[0]->addr = 0x48 + (val & 0x07);
658 data->lm75[1]->addr = 0x48 + ((val >> 4) & 0x07);
661 if(data->lm75[0]->addr == data->lm75[1]->addr) {
662 dev_err(&new_client->dev, "duplicate addresses 0x%x "
663 "for subclients\n", data->lm75[0]->addr);
668 for (i = 0; i <= 1; i++) {
669 i2c_set_clientdata(data->lm75[i], NULL);
670 data->lm75[i]->adapter = adapter;
671 data->lm75[i]->driver = &asb100_driver;
672 data->lm75[i]->flags = 0;
673 strlcpy(data->lm75[i]->name, "asb100 subclient", I2C_NAME_SIZE);
676 if ((err = i2c_attach_client(data->lm75[0]))) {
677 dev_err(&new_client->dev, "subclient %d registration "
678 "at address 0x%x failed.\n", i, data->lm75[0]->addr);
682 if ((err = i2c_attach_client(data->lm75[1]))) {
683 dev_err(&new_client->dev, "subclient %d registration "
684 "at address 0x%x failed.\n", i, data->lm75[1]->addr);
690 /* Undo inits in case of errors */
692 i2c_detach_client(data->lm75[0]);
694 kfree(data->lm75[1]);
696 kfree(data->lm75[0]);
701 static int asb100_detect(struct i2c_adapter *adapter, int address, int kind)
704 struct i2c_client *new_client;
705 struct asb100_data *data;
707 /* asb100 is SMBus only */
708 if (i2c_is_isa_adapter(adapter)) {
709 pr_debug("asb100.o: detect failed, "
710 "cannot attach to legacy adapter!\n");
715 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
716 pr_debug("asb100.o: detect failed, "
717 "smbus byte data not supported!\n");
722 /* OK. For now, we presume we have a valid client. We now create the
723 client structure, even though we cannot fill it completely yet.
724 But it allows us to access asb100_{read,write}_value. */
726 if (!(data = kmalloc(sizeof(struct asb100_data), GFP_KERNEL))) {
727 pr_debug("asb100.o: detect failed, kmalloc failed!\n");
731 memset(data, 0, sizeof(struct asb100_data));
733 new_client = &data->client;
734 init_MUTEX(&data->lock);
735 i2c_set_clientdata(new_client, data);
736 new_client->addr = address;
737 new_client->adapter = adapter;
738 new_client->driver = &asb100_driver;
739 new_client->flags = 0;
741 /* Now, we do the remaining detection. */
743 /* The chip may be stuck in some other bank than bank 0. This may
744 make reading other information impossible. Specify a force=... or
745 force_*=... parameter, and the chip will be reset to the right
749 int val1 = asb100_read_value(new_client, ASB100_REG_BANK);
750 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
752 /* If we're in bank 0 */
753 if ( (!(val1 & 0x07)) &&
754 /* Check for ASB100 ID (low byte) */
755 ( ((!(val1 & 0x80)) && (val2 != 0x94)) ||
756 /* Check for ASB100 ID (high byte ) */
757 ((val1 & 0x80) && (val2 != 0x06)) ) ) {
758 pr_debug("asb100.o: detect failed, "
759 "bad chip id 0x%02x!\n", val2);
766 /* We have either had a force parameter, or we have already detected
767 Winbond. Put it now into bank 0 and Vendor ID High Byte */
768 asb100_write_value(new_client, ASB100_REG_BANK,
769 (asb100_read_value(new_client, ASB100_REG_BANK) & 0x78) | 0x80);
771 /* Determine the chip type. */
773 int val1 = asb100_read_value(new_client, ASB100_REG_WCHIPID);
774 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
776 if ((val1 == 0x31) && (val2 == 0x06))
780 dev_warn(&new_client->dev, "ignoring "
781 "'force' parameter for unknown chip "
782 "at adapter %d, address 0x%02x.\n",
783 i2c_adapter_id(adapter), address);
789 /* Fill in remaining client fields and put it into the global list */
790 strlcpy(new_client->name, "asb100", I2C_NAME_SIZE);
794 init_MUTEX(&data->update_lock);
796 /* Tell the I2C layer a new client has arrived */
797 if ((err = i2c_attach_client(new_client)))
800 /* Attach secondary lm75 clients */
801 if ((err = asb100_detect_subclients(adapter, address, kind,
805 /* Initialize the chip */
806 asb100_init_client(new_client);
808 /* A few vars need to be filled upon startup */
809 data->fan_min[0] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(0));
810 data->fan_min[1] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(1));
811 data->fan_min[2] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(2));
813 /* Register sysfs hooks */
814 device_create_file_in(new_client, 0);
815 device_create_file_in(new_client, 1);
816 device_create_file_in(new_client, 2);
817 device_create_file_in(new_client, 3);
818 device_create_file_in(new_client, 4);
819 device_create_file_in(new_client, 5);
820 device_create_file_in(new_client, 6);
822 device_create_file_fan(new_client, 1);
823 device_create_file_fan(new_client, 2);
824 device_create_file_fan(new_client, 3);
826 device_create_file_temp(new_client, 1);
827 device_create_file_temp(new_client, 2);
828 device_create_file_temp(new_client, 3);
829 device_create_file_temp(new_client, 4);
831 device_create_file_vid(new_client);
832 device_create_file_vrm(new_client);
834 device_create_file_alarms(new_client);
836 device_create_file_pwm1(new_client);
841 i2c_detach_client(new_client);
848 static int asb100_detach_client(struct i2c_client *client)
852 if ((err = i2c_detach_client(client))) {
853 dev_err(&client->dev, "client deregistration failed; "
854 "client not detached.\n");
858 if (i2c_get_clientdata(client)==NULL) {
863 kfree(i2c_get_clientdata(client));
869 /* The SMBus locks itself, usually, but nothing may access the chip between
871 static int asb100_read_value(struct i2c_client *client, u16 reg)
873 struct asb100_data *data = i2c_get_clientdata(client);
874 struct i2c_client *cl;
879 bank = (reg >> 8) & 0x0f;
882 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
884 if (bank == 0 || bank > 2) {
885 res = i2c_smbus_read_byte_data(client, reg & 0xff);
887 /* switch to subclient */
888 cl = data->lm75[bank - 1];
890 /* convert from ISA to LM75 I2C addresses */
891 switch (reg & 0xff) {
892 case 0x50: /* TEMP */
893 res = swab16(i2c_smbus_read_word_data (cl, 0));
895 case 0x52: /* CONFIG */
896 res = i2c_smbus_read_byte_data(cl, 1);
898 case 0x53: /* HYST */
899 res = swab16(i2c_smbus_read_word_data (cl, 2));
903 res = swab16(i2c_smbus_read_word_data (cl, 3));
909 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
916 static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
918 struct asb100_data *data = i2c_get_clientdata(client);
919 struct i2c_client *cl;
924 bank = (reg >> 8) & 0x0f;
927 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
929 if (bank == 0 || bank > 2) {
930 i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
932 /* switch to subclient */
933 cl = data->lm75[bank - 1];
935 /* convert from ISA to LM75 I2C addresses */
936 switch (reg & 0xff) {
937 case 0x52: /* CONFIG */
938 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
940 case 0x53: /* HYST */
941 i2c_smbus_write_word_data(cl, 2, swab16(value));
944 i2c_smbus_write_word_data(cl, 3, swab16(value));
950 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
955 static void asb100_init_client(struct i2c_client *client)
957 struct asb100_data *data = i2c_get_clientdata(client);
960 vid = asb100_read_value(client, ASB100_REG_VID_FANDIV) & 0x0f;
961 vid |= (asb100_read_value(client, ASB100_REG_CHIPID) & 0x01) << 4;
962 data->vrm = ASB100_DEFAULT_VRM;
963 vid = vid_from_reg(vid, data->vrm);
965 /* Start monitoring */
966 asb100_write_value(client, ASB100_REG_CONFIG,
967 (asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
970 static struct asb100_data *asb100_update_device(struct device *dev)
972 struct i2c_client *client = to_i2c_client(dev);
973 struct asb100_data *data = i2c_get_clientdata(client);
976 down(&data->update_lock);
978 if (time_after(jiffies - data->last_updated, (unsigned long)(HZ+HZ/2))
979 || time_before(jiffies, data->last_updated) || !data->valid) {
981 dev_dbg(&client->dev, "starting device update...\n");
983 /* 7 voltage inputs */
984 for (i = 0; i < 7; i++) {
985 data->in[i] = asb100_read_value(client,
987 data->in_min[i] = asb100_read_value(client,
988 ASB100_REG_IN_MIN(i));
989 data->in_max[i] = asb100_read_value(client,
990 ASB100_REG_IN_MAX(i));
994 for (i = 0; i < 3; i++) {
995 data->fan[i] = asb100_read_value(client,
997 data->fan_min[i] = asb100_read_value(client,
998 ASB100_REG_FAN_MIN(i));
1001 /* 4 temperature inputs */
1002 for (i = 1; i <= 4; i++) {
1003 data->temp[i-1] = asb100_read_value(client,
1004 ASB100_REG_TEMP(i));
1005 data->temp_max[i-1] = asb100_read_value(client,
1006 ASB100_REG_TEMP_MAX(i));
1007 data->temp_hyst[i-1] = asb100_read_value(client,
1008 ASB100_REG_TEMP_HYST(i));
1011 /* VID and fan divisors */
1012 i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
1013 data->vid = i & 0x0f;
1014 data->vid |= (asb100_read_value(client,
1015 ASB100_REG_CHIPID) & 0x01) << 4;
1016 data->fan_div[0] = (i >> 4) & 0x03;
1017 data->fan_div[1] = (i >> 6) & 0x03;
1018 data->fan_div[2] = (asb100_read_value(client,
1019 ASB100_REG_PIN) >> 6) & 0x03;
1022 data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
1025 data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
1026 (asb100_read_value(client, ASB100_REG_ALARM2) << 8);
1028 data->last_updated = jiffies;
1031 dev_dbg(&client->dev, "... device update complete\n");
1034 up(&data->update_lock);
1039 static int __init asb100_init(void)
1041 return i2c_add_driver(&asb100_driver);
1044 static void __exit asb100_exit(void)
1046 i2c_del_driver(&asb100_driver);
1049 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1050 MODULE_DESCRIPTION("ASB100 Bach driver");
1051 MODULE_LICENSE("GPL");
1053 module_init(asb100_init);
1054 module_exit(asb100_exit);