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/module.h>
40 #include <linux/slab.h>
41 #include <linux/i2c.h>
42 #include <linux/i2c-sensor.h>
43 #include <linux/i2c-vid.h>
44 #include <linux/init.h>
45 #include <linux/jiffies.h>
50 2003-12-29 1.0.0 Ported from lm_sensors project for kernel 2.6
52 #define ASB100_VERSION "1.0.0"
54 /* I2C addresses to scan */
55 static unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
57 /* ISA addresses to scan (none) */
58 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
60 /* Insmod parameters */
61 SENSORS_INSMOD_1(asb100);
62 I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
63 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
65 /* Voltage IN registers 0-6 */
66 #define ASB100_REG_IN(nr) (0x20 + (nr))
67 #define ASB100_REG_IN_MAX(nr) (0x2b + (nr * 2))
68 #define ASB100_REG_IN_MIN(nr) (0x2c + (nr * 2))
70 /* FAN IN registers 1-3 */
71 #define ASB100_REG_FAN(nr) (0x28 + (nr))
72 #define ASB100_REG_FAN_MIN(nr) (0x3b + (nr))
74 /* TEMPERATURE registers 1-4 */
75 static const u16 asb100_reg_temp[] = {0, 0x27, 0x150, 0x250, 0x17};
76 static const u16 asb100_reg_temp_max[] = {0, 0x39, 0x155, 0x255, 0x18};
77 static const u16 asb100_reg_temp_hyst[] = {0, 0x3a, 0x153, 0x253, 0x19};
79 #define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])
80 #define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])
81 #define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])
83 #define ASB100_REG_TEMP2_CONFIG 0x0152
84 #define ASB100_REG_TEMP3_CONFIG 0x0252
87 #define ASB100_REG_CONFIG 0x40
88 #define ASB100_REG_ALARM1 0x41
89 #define ASB100_REG_ALARM2 0x42
90 #define ASB100_REG_SMIM1 0x43
91 #define ASB100_REG_SMIM2 0x44
92 #define ASB100_REG_VID_FANDIV 0x47
93 #define ASB100_REG_I2C_ADDR 0x48
94 #define ASB100_REG_CHIPID 0x49
95 #define ASB100_REG_I2C_SUBADDR 0x4a
96 #define ASB100_REG_PIN 0x4b
97 #define ASB100_REG_IRQ 0x4c
98 #define ASB100_REG_BANK 0x4e
99 #define ASB100_REG_CHIPMAN 0x4f
101 #define ASB100_REG_WCHIPID 0x58
103 /* bit 7 -> enable, bits 0-3 -> duty cycle */
104 #define ASB100_REG_PWM1 0x59
107 Rounding and limit checking is only done on the TO_REG variants. */
109 /* These constants are a guess, consistent w/ w83781d */
110 #define ASB100_IN_MIN ( 0)
111 #define ASB100_IN_MAX (4080)
113 /* IN: 1/1000 V (0V to 4.08V)
115 static u8 IN_TO_REG(unsigned val)
117 unsigned nval = SENSORS_LIMIT(val, ASB100_IN_MIN, ASB100_IN_MAX);
118 return (nval + 8) / 16;
121 static unsigned IN_FROM_REG(u8 reg)
126 static u8 FAN_TO_REG(long rpm, int div)
132 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
133 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
136 static int FAN_FROM_REG(u8 val, int div)
138 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
141 /* These constants are a guess, consistent w/ w83781d */
142 #define ASB100_TEMP_MIN (-128000)
143 #define ASB100_TEMP_MAX ( 127000)
145 /* TEMP: 0.001C/bit (-128C to +127C)
146 REG: 1C/bit, two's complement */
147 static u8 TEMP_TO_REG(int temp)
149 int ntemp = SENSORS_LIMIT(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX);
150 ntemp += (ntemp<0 ? -500 : 500);
151 return (u8)(ntemp / 1000);
154 static int TEMP_FROM_REG(u8 reg)
156 return (s8)reg * 1000;
159 /* PWM: 0 - 255 per sensors documentation
160 REG: (6.25% duty cycle per bit) */
161 static u8 ASB100_PWM_TO_REG(int pwm)
163 pwm = SENSORS_LIMIT(pwm, 0, 255);
164 return (u8)(pwm / 16);
167 static int ASB100_PWM_FROM_REG(u8 reg)
172 #define DIV_FROM_REG(val) (1 << (val))
174 /* FAN DIV: 1, 2, 4, or 8 (defaults to 2)
175 REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */
176 static u8 DIV_TO_REG(long val)
178 return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1;
181 /* For each registered client, we need to keep some data in memory. That
182 data is pointed to by client->data. The structure itself is
183 dynamically allocated, at the same time the client itself is allocated. */
185 struct i2c_client client;
186 struct semaphore lock;
189 struct semaphore update_lock;
190 unsigned long last_updated; /* In jiffies */
192 /* array of 2 pointers to subclients */
193 struct i2c_client *lm75[2];
195 char valid; /* !=0 if following fields are valid */
196 u8 in[7]; /* Register value */
197 u8 in_max[7]; /* Register value */
198 u8 in_min[7]; /* Register value */
199 u8 fan[3]; /* Register value */
200 u8 fan_min[3]; /* Register value */
201 u16 temp[4]; /* Register value (0 and 3 are u8 only) */
202 u16 temp_max[4]; /* Register value (0 and 3 are u8 only) */
203 u16 temp_hyst[4]; /* Register value (0 and 3 are u8 only) */
204 u8 fan_div[3]; /* Register encoding, right justified */
205 u8 pwm; /* Register encoding */
206 u8 vid; /* Register encoding, combined */
207 u32 alarms; /* Register encoding, combined */
211 static int asb100_read_value(struct i2c_client *client, u16 reg);
212 static void asb100_write_value(struct i2c_client *client, u16 reg, u16 val);
214 static int asb100_attach_adapter(struct i2c_adapter *adapter);
215 static int asb100_detect(struct i2c_adapter *adapter, int address, int kind);
216 static int asb100_detach_client(struct i2c_client *client);
217 static struct asb100_data *asb100_update_device(struct device *dev);
218 static void asb100_init_client(struct i2c_client *client);
220 static struct i2c_driver asb100_driver = {
221 .owner = THIS_MODULE,
223 .id = I2C_DRIVERID_ASB100,
224 .flags = I2C_DF_NOTIFY,
225 .attach_adapter = asb100_attach_adapter,
226 .detach_client = asb100_detach_client,
230 #define show_in_reg(reg) \
231 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
233 struct asb100_data *data = asb100_update_device(dev); \
234 return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
241 #define set_in_reg(REG, reg) \
242 static ssize_t set_in_##reg(struct device *dev, const char *buf, \
243 size_t count, int nr) \
245 struct i2c_client *client = to_i2c_client(dev); \
246 struct asb100_data *data = i2c_get_clientdata(client); \
247 unsigned long val = simple_strtoul(buf, NULL, 10); \
249 down(&data->update_lock); \
250 data->in_##reg[nr] = IN_TO_REG(val); \
251 asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
252 data->in_##reg[nr]); \
253 up(&data->update_lock); \
260 #define sysfs_in(offset) \
262 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
264 return show_in(dev, buf, offset); \
266 static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
267 show_in##offset, NULL); \
269 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
271 return show_in_min(dev, buf, offset); \
274 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
276 return show_in_max(dev, buf, offset); \
278 static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
279 const char *buf, size_t count) \
281 return set_in_min(dev, buf, count, offset); \
283 static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
284 const char *buf, size_t count) \
286 return set_in_max(dev, buf, count, offset); \
288 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
289 show_in##offset##_min, set_in##offset##_min); \
290 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
291 show_in##offset##_max, set_in##offset##_max);
301 #define device_create_file_in(client, offset) do { \
302 device_create_file(&client->dev, &dev_attr_in##offset##_input); \
303 device_create_file(&client->dev, &dev_attr_in##offset##_min); \
304 device_create_file(&client->dev, &dev_attr_in##offset##_max); \
308 static ssize_t show_fan(struct device *dev, char *buf, int nr)
310 struct asb100_data *data = asb100_update_device(dev);
311 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
312 DIV_FROM_REG(data->fan_div[nr])));
315 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
317 struct asb100_data *data = asb100_update_device(dev);
318 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
319 DIV_FROM_REG(data->fan_div[nr])));
322 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
324 struct asb100_data *data = asb100_update_device(dev);
325 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
328 static ssize_t set_fan_min(struct device *dev, const char *buf,
329 size_t count, int nr)
331 struct i2c_client *client = to_i2c_client(dev);
332 struct asb100_data *data = i2c_get_clientdata(client);
333 u32 val = simple_strtoul(buf, NULL, 10);
335 down(&data->update_lock);
336 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
337 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
338 up(&data->update_lock);
342 /* Note: we save and restore the fan minimum here, because its value is
343 determined in part by the fan divisor. This follows the principle of
344 least suprise; the user doesn't expect the fan minimum to change just
345 because the divisor changed. */
346 static ssize_t set_fan_div(struct device *dev, const char *buf,
347 size_t count, int nr)
349 struct i2c_client *client = to_i2c_client(dev);
350 struct asb100_data *data = i2c_get_clientdata(client);
352 unsigned long val = simple_strtoul(buf, NULL, 10);
355 down(&data->update_lock);
357 min = FAN_FROM_REG(data->fan_min[nr],
358 DIV_FROM_REG(data->fan_div[nr]));
359 data->fan_div[nr] = DIV_TO_REG(val);
363 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
364 reg = (reg & 0xcf) | (data->fan_div[0] << 4);
365 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
369 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
370 reg = (reg & 0x3f) | (data->fan_div[1] << 6);
371 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
375 reg = asb100_read_value(client, ASB100_REG_PIN);
376 reg = (reg & 0x3f) | (data->fan_div[2] << 6);
377 asb100_write_value(client, ASB100_REG_PIN, reg);
382 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
383 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
385 up(&data->update_lock);
390 #define sysfs_fan(offset) \
391 static ssize_t show_fan##offset(struct device *dev, struct device_attribute *attr, char *buf) \
393 return show_fan(dev, buf, offset - 1); \
395 static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
397 return show_fan_min(dev, buf, offset - 1); \
399 static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
401 return show_fan_div(dev, buf, offset - 1); \
403 static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
406 return set_fan_min(dev, buf, count, offset - 1); \
408 static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \
411 return set_fan_div(dev, buf, count, offset - 1); \
413 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
414 show_fan##offset, NULL); \
415 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
416 show_fan##offset##_min, set_fan##offset##_min); \
417 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
418 show_fan##offset##_div, set_fan##offset##_div);
424 #define device_create_file_fan(client, offset) do { \
425 device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
426 device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
427 device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
430 /* 4 Temp. Sensors */
431 static int sprintf_temp_from_reg(u16 reg, char *buf, int nr)
437 ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg));
439 case 0: case 3: default:
440 ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
446 #define show_temp_reg(reg) \
447 static ssize_t show_##reg(struct device *dev, char *buf, int nr) \
449 struct asb100_data *data = asb100_update_device(dev); \
450 return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
454 show_temp_reg(temp_max);
455 show_temp_reg(temp_hyst);
457 #define set_temp_reg(REG, reg) \
458 static ssize_t set_##reg(struct device *dev, const char *buf, \
459 size_t count, int nr) \
461 struct i2c_client *client = to_i2c_client(dev); \
462 struct asb100_data *data = i2c_get_clientdata(client); \
463 unsigned long val = simple_strtoul(buf, NULL, 10); \
465 down(&data->update_lock); \
468 data->reg[nr] = LM75_TEMP_TO_REG(val); \
470 case 0: case 3: default: \
471 data->reg[nr] = TEMP_TO_REG(val); \
474 asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
476 up(&data->update_lock); \
480 set_temp_reg(MAX, temp_max);
481 set_temp_reg(HYST, temp_hyst);
483 #define sysfs_temp(num) \
484 static ssize_t show_temp##num(struct device *dev, struct device_attribute *attr, char *buf) \
486 return show_temp(dev, buf, num-1); \
488 static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL); \
489 static ssize_t show_temp_max##num(struct device *dev, struct device_attribute *attr, char *buf) \
491 return show_temp_max(dev, buf, num-1); \
493 static ssize_t set_temp_max##num(struct device *dev, struct device_attribute *attr, const char *buf, \
496 return set_temp_max(dev, buf, count, num-1); \
498 static DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
499 show_temp_max##num, set_temp_max##num); \
500 static ssize_t show_temp_hyst##num(struct device *dev, struct device_attribute *attr, char *buf) \
502 return show_temp_hyst(dev, buf, num-1); \
504 static ssize_t set_temp_hyst##num(struct device *dev, struct device_attribute *attr, const char *buf, \
507 return set_temp_hyst(dev, buf, count, num-1); \
509 static DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
510 show_temp_hyst##num, set_temp_hyst##num);
518 #define device_create_file_temp(client, num) do { \
519 device_create_file(&client->dev, &dev_attr_temp##num##_input); \
520 device_create_file(&client->dev, &dev_attr_temp##num##_max); \
521 device_create_file(&client->dev, &dev_attr_temp##num##_max_hyst); \
524 static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
526 struct asb100_data *data = asb100_update_device(dev);
527 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
530 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
531 #define device_create_file_vid(client) \
532 device_create_file(&client->dev, &dev_attr_cpu0_vid)
535 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
537 struct asb100_data *data = asb100_update_device(dev);
538 return sprintf(buf, "%d\n", data->vrm);
541 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
543 struct i2c_client *client = to_i2c_client(dev);
544 struct asb100_data *data = i2c_get_clientdata(client);
545 unsigned long val = simple_strtoul(buf, NULL, 10);
551 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
552 #define device_create_file_vrm(client) \
553 device_create_file(&client->dev, &dev_attr_vrm);
555 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
557 struct asb100_data *data = asb100_update_device(dev);
558 return sprintf(buf, "%u\n", data->alarms);
561 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
562 #define device_create_file_alarms(client) \
563 device_create_file(&client->dev, &dev_attr_alarms)
566 static ssize_t show_pwm1(struct device *dev, struct device_attribute *attr, char *buf)
568 struct asb100_data *data = asb100_update_device(dev);
569 return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
572 static ssize_t set_pwm1(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
574 struct i2c_client *client = to_i2c_client(dev);
575 struct asb100_data *data = i2c_get_clientdata(client);
576 unsigned long val = simple_strtoul(buf, NULL, 10);
578 down(&data->update_lock);
579 data->pwm &= 0x80; /* keep the enable bit */
580 data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
581 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
582 up(&data->update_lock);
586 static ssize_t show_pwm_enable1(struct device *dev, struct device_attribute *attr, char *buf)
588 struct asb100_data *data = asb100_update_device(dev);
589 return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
592 static ssize_t set_pwm_enable1(struct device *dev, struct device_attribute *attr, const char *buf,
595 struct i2c_client *client = to_i2c_client(dev);
596 struct asb100_data *data = i2c_get_clientdata(client);
597 unsigned long val = simple_strtoul(buf, NULL, 10);
599 down(&data->update_lock);
600 data->pwm &= 0x0f; /* keep the duty cycle bits */
601 data->pwm |= (val ? 0x80 : 0x00);
602 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
603 up(&data->update_lock);
607 static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm1, set_pwm1);
608 static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
609 show_pwm_enable1, set_pwm_enable1);
610 #define device_create_file_pwm1(client) do { \
611 device_create_file(&new_client->dev, &dev_attr_pwm1); \
612 device_create_file(&new_client->dev, &dev_attr_pwm1_enable); \
615 /* This function is called when:
616 asb100_driver is inserted (when this module is loaded), for each
618 when a new adapter is inserted (and asb100_driver is still present)
620 static int asb100_attach_adapter(struct i2c_adapter *adapter)
622 if (!(adapter->class & I2C_CLASS_HWMON))
624 return i2c_detect(adapter, &addr_data, asb100_detect);
627 static int asb100_detect_subclients(struct i2c_adapter *adapter, int address,
628 int kind, struct i2c_client *new_client)
631 struct asb100_data *data = i2c_get_clientdata(new_client);
633 data->lm75[0] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
634 if (!(data->lm75[0])) {
638 memset(data->lm75[0], 0x00, sizeof(struct i2c_client));
640 data->lm75[1] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
641 if (!(data->lm75[1])) {
645 memset(data->lm75[1], 0x00, sizeof(struct i2c_client));
647 id = i2c_adapter_id(adapter);
649 if (force_subclients[0] == id && force_subclients[1] == address) {
650 for (i = 2; i <= 3; i++) {
651 if (force_subclients[i] < 0x48 ||
652 force_subclients[i] > 0x4f) {
653 dev_err(&new_client->dev, "invalid subclient "
654 "address %d; must be 0x48-0x4f\n",
655 force_subclients[i]);
660 asb100_write_value(new_client, ASB100_REG_I2C_SUBADDR,
661 (force_subclients[2] & 0x07) |
662 ((force_subclients[3] & 0x07) <<4));
663 data->lm75[0]->addr = force_subclients[2];
664 data->lm75[1]->addr = force_subclients[3];
666 int val = asb100_read_value(new_client, ASB100_REG_I2C_SUBADDR);
667 data->lm75[0]->addr = 0x48 + (val & 0x07);
668 data->lm75[1]->addr = 0x48 + ((val >> 4) & 0x07);
671 if(data->lm75[0]->addr == data->lm75[1]->addr) {
672 dev_err(&new_client->dev, "duplicate addresses 0x%x "
673 "for subclients\n", data->lm75[0]->addr);
678 for (i = 0; i <= 1; i++) {
679 i2c_set_clientdata(data->lm75[i], NULL);
680 data->lm75[i]->adapter = adapter;
681 data->lm75[i]->driver = &asb100_driver;
682 data->lm75[i]->flags = 0;
683 strlcpy(data->lm75[i]->name, "asb100 subclient", I2C_NAME_SIZE);
686 if ((err = i2c_attach_client(data->lm75[0]))) {
687 dev_err(&new_client->dev, "subclient %d registration "
688 "at address 0x%x failed.\n", i, data->lm75[0]->addr);
692 if ((err = i2c_attach_client(data->lm75[1]))) {
693 dev_err(&new_client->dev, "subclient %d registration "
694 "at address 0x%x failed.\n", i, data->lm75[1]->addr);
700 /* Undo inits in case of errors */
702 i2c_detach_client(data->lm75[0]);
704 kfree(data->lm75[1]);
706 kfree(data->lm75[0]);
711 static int asb100_detect(struct i2c_adapter *adapter, int address, int kind)
714 struct i2c_client *new_client;
715 struct asb100_data *data;
717 /* asb100 is SMBus only */
718 if (i2c_is_isa_adapter(adapter)) {
719 pr_debug("asb100.o: detect failed, "
720 "cannot attach to legacy adapter!\n");
725 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
726 pr_debug("asb100.o: detect failed, "
727 "smbus byte data not supported!\n");
732 /* OK. For now, we presume we have a valid client. We now create the
733 client structure, even though we cannot fill it completely yet.
734 But it allows us to access asb100_{read,write}_value. */
736 if (!(data = kmalloc(sizeof(struct asb100_data), GFP_KERNEL))) {
737 pr_debug("asb100.o: detect failed, kmalloc failed!\n");
741 memset(data, 0, sizeof(struct asb100_data));
743 new_client = &data->client;
744 init_MUTEX(&data->lock);
745 i2c_set_clientdata(new_client, data);
746 new_client->addr = address;
747 new_client->adapter = adapter;
748 new_client->driver = &asb100_driver;
749 new_client->flags = 0;
751 /* Now, we do the remaining detection. */
753 /* The chip may be stuck in some other bank than bank 0. This may
754 make reading other information impossible. Specify a force=... or
755 force_*=... parameter, and the chip will be reset to the right
759 int val1 = asb100_read_value(new_client, ASB100_REG_BANK);
760 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
762 /* If we're in bank 0 */
763 if ( (!(val1 & 0x07)) &&
764 /* Check for ASB100 ID (low byte) */
765 ( ((!(val1 & 0x80)) && (val2 != 0x94)) ||
766 /* Check for ASB100 ID (high byte ) */
767 ((val1 & 0x80) && (val2 != 0x06)) ) ) {
768 pr_debug("asb100.o: detect failed, "
769 "bad chip id 0x%02x!\n", val2);
776 /* We have either had a force parameter, or we have already detected
777 Winbond. Put it now into bank 0 and Vendor ID High Byte */
778 asb100_write_value(new_client, ASB100_REG_BANK,
779 (asb100_read_value(new_client, ASB100_REG_BANK) & 0x78) | 0x80);
781 /* Determine the chip type. */
783 int val1 = asb100_read_value(new_client, ASB100_REG_WCHIPID);
784 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
786 if ((val1 == 0x31) && (val2 == 0x06))
790 dev_warn(&new_client->dev, "ignoring "
791 "'force' parameter for unknown chip "
792 "at adapter %d, address 0x%02x.\n",
793 i2c_adapter_id(adapter), address);
799 /* Fill in remaining client fields and put it into the global list */
800 strlcpy(new_client->name, "asb100", I2C_NAME_SIZE);
804 init_MUTEX(&data->update_lock);
806 /* Tell the I2C layer a new client has arrived */
807 if ((err = i2c_attach_client(new_client)))
810 /* Attach secondary lm75 clients */
811 if ((err = asb100_detect_subclients(adapter, address, kind,
815 /* Initialize the chip */
816 asb100_init_client(new_client);
818 /* A few vars need to be filled upon startup */
819 data->fan_min[0] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(0));
820 data->fan_min[1] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(1));
821 data->fan_min[2] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(2));
823 /* Register sysfs hooks */
824 device_create_file_in(new_client, 0);
825 device_create_file_in(new_client, 1);
826 device_create_file_in(new_client, 2);
827 device_create_file_in(new_client, 3);
828 device_create_file_in(new_client, 4);
829 device_create_file_in(new_client, 5);
830 device_create_file_in(new_client, 6);
832 device_create_file_fan(new_client, 1);
833 device_create_file_fan(new_client, 2);
834 device_create_file_fan(new_client, 3);
836 device_create_file_temp(new_client, 1);
837 device_create_file_temp(new_client, 2);
838 device_create_file_temp(new_client, 3);
839 device_create_file_temp(new_client, 4);
841 device_create_file_vid(new_client);
842 device_create_file_vrm(new_client);
844 device_create_file_alarms(new_client);
846 device_create_file_pwm1(new_client);
851 i2c_detach_client(new_client);
858 static int asb100_detach_client(struct i2c_client *client)
862 if ((err = i2c_detach_client(client))) {
863 dev_err(&client->dev, "client deregistration failed; "
864 "client not detached.\n");
868 if (i2c_get_clientdata(client)==NULL) {
873 kfree(i2c_get_clientdata(client));
879 /* The SMBus locks itself, usually, but nothing may access the chip between
881 static int asb100_read_value(struct i2c_client *client, u16 reg)
883 struct asb100_data *data = i2c_get_clientdata(client);
884 struct i2c_client *cl;
889 bank = (reg >> 8) & 0x0f;
892 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
894 if (bank == 0 || bank > 2) {
895 res = i2c_smbus_read_byte_data(client, reg & 0xff);
897 /* switch to subclient */
898 cl = data->lm75[bank - 1];
900 /* convert from ISA to LM75 I2C addresses */
901 switch (reg & 0xff) {
902 case 0x50: /* TEMP */
903 res = swab16(i2c_smbus_read_word_data (cl, 0));
905 case 0x52: /* CONFIG */
906 res = i2c_smbus_read_byte_data(cl, 1);
908 case 0x53: /* HYST */
909 res = swab16(i2c_smbus_read_word_data (cl, 2));
913 res = swab16(i2c_smbus_read_word_data (cl, 3));
919 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
926 static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
928 struct asb100_data *data = i2c_get_clientdata(client);
929 struct i2c_client *cl;
934 bank = (reg >> 8) & 0x0f;
937 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
939 if (bank == 0 || bank > 2) {
940 i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
942 /* switch to subclient */
943 cl = data->lm75[bank - 1];
945 /* convert from ISA to LM75 I2C addresses */
946 switch (reg & 0xff) {
947 case 0x52: /* CONFIG */
948 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
950 case 0x53: /* HYST */
951 i2c_smbus_write_word_data(cl, 2, swab16(value));
954 i2c_smbus_write_word_data(cl, 3, swab16(value));
960 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
965 static void asb100_init_client(struct i2c_client *client)
967 struct asb100_data *data = i2c_get_clientdata(client);
970 vid = asb100_read_value(client, ASB100_REG_VID_FANDIV) & 0x0f;
971 vid |= (asb100_read_value(client, ASB100_REG_CHIPID) & 0x01) << 4;
972 data->vrm = i2c_which_vrm();
973 vid = vid_from_reg(vid, data->vrm);
975 /* Start monitoring */
976 asb100_write_value(client, ASB100_REG_CONFIG,
977 (asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
980 static struct asb100_data *asb100_update_device(struct device *dev)
982 struct i2c_client *client = to_i2c_client(dev);
983 struct asb100_data *data = i2c_get_clientdata(client);
986 down(&data->update_lock);
988 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
991 dev_dbg(&client->dev, "starting device update...\n");
993 /* 7 voltage inputs */
994 for (i = 0; i < 7; i++) {
995 data->in[i] = asb100_read_value(client,
997 data->in_min[i] = asb100_read_value(client,
998 ASB100_REG_IN_MIN(i));
999 data->in_max[i] = asb100_read_value(client,
1000 ASB100_REG_IN_MAX(i));
1004 for (i = 0; i < 3; i++) {
1005 data->fan[i] = asb100_read_value(client,
1007 data->fan_min[i] = asb100_read_value(client,
1008 ASB100_REG_FAN_MIN(i));
1011 /* 4 temperature inputs */
1012 for (i = 1; i <= 4; i++) {
1013 data->temp[i-1] = asb100_read_value(client,
1014 ASB100_REG_TEMP(i));
1015 data->temp_max[i-1] = asb100_read_value(client,
1016 ASB100_REG_TEMP_MAX(i));
1017 data->temp_hyst[i-1] = asb100_read_value(client,
1018 ASB100_REG_TEMP_HYST(i));
1021 /* VID and fan divisors */
1022 i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
1023 data->vid = i & 0x0f;
1024 data->vid |= (asb100_read_value(client,
1025 ASB100_REG_CHIPID) & 0x01) << 4;
1026 data->fan_div[0] = (i >> 4) & 0x03;
1027 data->fan_div[1] = (i >> 6) & 0x03;
1028 data->fan_div[2] = (asb100_read_value(client,
1029 ASB100_REG_PIN) >> 6) & 0x03;
1032 data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
1035 data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
1036 (asb100_read_value(client, ASB100_REG_ALARM2) << 8);
1038 data->last_updated = jiffies;
1041 dev_dbg(&client->dev, "... device update complete\n");
1044 up(&data->update_lock);
1049 static int __init asb100_init(void)
1051 return i2c_add_driver(&asb100_driver);
1054 static void __exit asb100_exit(void)
1056 i2c_del_driver(&asb100_driver);
1059 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1060 MODULE_DESCRIPTION("ASB100 Bach driver");
1061 MODULE_LICENSE("GPL");
1063 module_init(asb100_init);
1064 module_exit(asb100_exit);