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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41 #include <linux/module.h>
42 #include <linux/slab.h>
43 #include <linux/i2c.h>
44 #include <linux/hwmon.h>
45 #include <linux/hwmon-sysfs.h>
46 #include <linux/hwmon-vid.h>
47 #include <linux/err.h>
48 #include <linux/init.h>
49 #include <linux/jiffies.h>
50 #include <linux/mutex.h>
53 /* I2C addresses to scan */
54 static const unsigned short normal_i2c
[] = { 0x2d, I2C_CLIENT_END
};
56 static unsigned short force_subclients
[4];
57 module_param_array(force_subclients
, short, NULL
, 0);
58 MODULE_PARM_DESC(force_subclients
,
59 "List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}");
61 /* Voltage IN registers 0-6 */
62 #define ASB100_REG_IN(nr) (0x20 + (nr))
63 #define ASB100_REG_IN_MAX(nr) (0x2b + (nr * 2))
64 #define ASB100_REG_IN_MIN(nr) (0x2c + (nr * 2))
66 /* FAN IN registers 1-3 */
67 #define ASB100_REG_FAN(nr) (0x28 + (nr))
68 #define ASB100_REG_FAN_MIN(nr) (0x3b + (nr))
70 /* TEMPERATURE registers 1-4 */
71 static const u16 asb100_reg_temp
[] = {0, 0x27, 0x150, 0x250, 0x17};
72 static const u16 asb100_reg_temp_max
[] = {0, 0x39, 0x155, 0x255, 0x18};
73 static const u16 asb100_reg_temp_hyst
[] = {0, 0x3a, 0x153, 0x253, 0x19};
75 #define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])
76 #define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])
77 #define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])
79 #define ASB100_REG_TEMP2_CONFIG 0x0152
80 #define ASB100_REG_TEMP3_CONFIG 0x0252
83 #define ASB100_REG_CONFIG 0x40
84 #define ASB100_REG_ALARM1 0x41
85 #define ASB100_REG_ALARM2 0x42
86 #define ASB100_REG_SMIM1 0x43
87 #define ASB100_REG_SMIM2 0x44
88 #define ASB100_REG_VID_FANDIV 0x47
89 #define ASB100_REG_I2C_ADDR 0x48
90 #define ASB100_REG_CHIPID 0x49
91 #define ASB100_REG_I2C_SUBADDR 0x4a
92 #define ASB100_REG_PIN 0x4b
93 #define ASB100_REG_IRQ 0x4c
94 #define ASB100_REG_BANK 0x4e
95 #define ASB100_REG_CHIPMAN 0x4f
97 #define ASB100_REG_WCHIPID 0x58
99 /* bit 7 -> enable, bits 0-3 -> duty cycle */
100 #define ASB100_REG_PWM1 0x59
104 * Rounding and limit checking is only done on the TO_REG variants.
107 /* These constants are a guess, consistent w/ w83781d */
108 #define ASB100_IN_MIN 0
109 #define ASB100_IN_MAX 4080
112 * IN: 1/1000 V (0V to 4.08V)
115 static u8
IN_TO_REG(unsigned val
)
117 unsigned nval
= clamp_val(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
= clamp_val(rpm
, 1, 1000000);
133 return clamp_val((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
146 * TEMP: 0.001C/bit (-128C to +127C)
147 * REG: 1C/bit, two's complement
149 static u8
TEMP_TO_REG(long temp
)
151 int ntemp
= clamp_val(temp
, ASB100_TEMP_MIN
, ASB100_TEMP_MAX
);
152 ntemp
+= (ntemp
< 0 ? -500 : 500);
153 return (u8
)(ntemp
/ 1000);
156 static int TEMP_FROM_REG(u8 reg
)
158 return (s8
)reg
* 1000;
162 * PWM: 0 - 255 per sensors documentation
163 * REG: (6.25% duty cycle per bit)
165 static u8
ASB100_PWM_TO_REG(int pwm
)
167 pwm
= clamp_val(pwm
, 0, 255);
168 return (u8
)(pwm
/ 16);
171 static int ASB100_PWM_FROM_REG(u8 reg
)
176 #define DIV_FROM_REG(val) (1 << (val))
179 * FAN DIV: 1, 2, 4, or 8 (defaults to 2)
180 * REG: 0, 1, 2, or 3 (respectively) (defaults to 1)
182 static u8
DIV_TO_REG(long val
)
184 return val
== 8 ? 3 : val
== 4 ? 2 : val
== 1 ? 0 : 1;
188 * For each registered client, we need to keep some data in memory. That
189 * data is pointed to by client->data. The structure itself is
190 * dynamically allocated, at the same time the client itself is allocated.
193 struct device
*hwmon_dev
;
196 struct mutex update_lock
;
197 unsigned long last_updated
; /* In jiffies */
199 /* array of 2 pointers to subclients */
200 struct i2c_client
*lm75
[2];
202 char valid
; /* !=0 if following fields are valid */
203 u8 in
[7]; /* Register value */
204 u8 in_max
[7]; /* Register value */
205 u8 in_min
[7]; /* Register value */
206 u8 fan
[3]; /* Register value */
207 u8 fan_min
[3]; /* Register value */
208 u16 temp
[4]; /* Register value (0 and 3 are u8 only) */
209 u16 temp_max
[4]; /* Register value (0 and 3 are u8 only) */
210 u16 temp_hyst
[4]; /* Register value (0 and 3 are u8 only) */
211 u8 fan_div
[3]; /* Register encoding, right justified */
212 u8 pwm
; /* Register encoding */
213 u8 vid
; /* Register encoding, combined */
214 u32 alarms
; /* Register encoding, combined */
218 static int asb100_read_value(struct i2c_client
*client
, u16 reg
);
219 static void asb100_write_value(struct i2c_client
*client
, u16 reg
, u16 val
);
221 static int asb100_probe(struct i2c_client
*client
,
222 const struct i2c_device_id
*id
);
223 static int asb100_detect(struct i2c_client
*client
,
224 struct i2c_board_info
*info
);
225 static int asb100_remove(struct i2c_client
*client
);
226 static struct asb100_data
*asb100_update_device(struct device
*dev
);
227 static void asb100_init_client(struct i2c_client
*client
);
229 static const struct i2c_device_id asb100_id
[] = {
233 MODULE_DEVICE_TABLE(i2c
, asb100_id
);
235 static struct i2c_driver asb100_driver
= {
236 .class = I2C_CLASS_HWMON
,
240 .probe
= asb100_probe
,
241 .remove
= asb100_remove
,
242 .id_table
= asb100_id
,
243 .detect
= asb100_detect
,
244 .address_list
= normal_i2c
,
248 #define show_in_reg(reg) \
249 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
252 int nr = to_sensor_dev_attr(attr)->index; \
253 struct asb100_data *data = asb100_update_device(dev); \
254 return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
261 #define set_in_reg(REG, reg) \
262 static ssize_t set_in_##reg(struct device *dev, struct device_attribute *attr, \
263 const char *buf, size_t count) \
265 int nr = to_sensor_dev_attr(attr)->index; \
266 struct i2c_client *client = to_i2c_client(dev); \
267 struct asb100_data *data = i2c_get_clientdata(client); \
269 int err = kstrtoul(buf, 10, &val); \
272 mutex_lock(&data->update_lock); \
273 data->in_##reg[nr] = IN_TO_REG(val); \
274 asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
275 data->in_##reg[nr]); \
276 mutex_unlock(&data->update_lock); \
283 #define sysfs_in(offset) \
284 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
285 show_in, NULL, offset); \
286 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
287 show_in_min, set_in_min, offset); \
288 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
289 show_in_max, set_in_max, offset)
300 static ssize_t
show_fan(struct device
*dev
, struct device_attribute
*attr
,
303 int nr
= to_sensor_dev_attr(attr
)->index
;
304 struct asb100_data
*data
= asb100_update_device(dev
);
305 return sprintf(buf
, "%d\n", FAN_FROM_REG(data
->fan
[nr
],
306 DIV_FROM_REG(data
->fan_div
[nr
])));
309 static ssize_t
show_fan_min(struct device
*dev
, struct device_attribute
*attr
,
312 int nr
= to_sensor_dev_attr(attr
)->index
;
313 struct asb100_data
*data
= asb100_update_device(dev
);
314 return sprintf(buf
, "%d\n", FAN_FROM_REG(data
->fan_min
[nr
],
315 DIV_FROM_REG(data
->fan_div
[nr
])));
318 static ssize_t
show_fan_div(struct device
*dev
, struct device_attribute
*attr
,
321 int nr
= to_sensor_dev_attr(attr
)->index
;
322 struct asb100_data
*data
= asb100_update_device(dev
);
323 return sprintf(buf
, "%d\n", DIV_FROM_REG(data
->fan_div
[nr
]));
326 static ssize_t
set_fan_min(struct device
*dev
, struct device_attribute
*attr
,
327 const char *buf
, size_t count
)
329 int nr
= to_sensor_dev_attr(attr
)->index
;
330 struct i2c_client
*client
= to_i2c_client(dev
);
331 struct asb100_data
*data
= i2c_get_clientdata(client
);
335 err
= kstrtoul(buf
, 10, &val
);
339 mutex_lock(&data
->update_lock
);
340 data
->fan_min
[nr
] = FAN_TO_REG(val
, DIV_FROM_REG(data
->fan_div
[nr
]));
341 asb100_write_value(client
, ASB100_REG_FAN_MIN(nr
), data
->fan_min
[nr
]);
342 mutex_unlock(&data
->update_lock
);
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 surprise; the user doesn't expect the fan minimum to change just
350 * because the divisor changed.
352 static ssize_t
set_fan_div(struct device
*dev
, struct device_attribute
*attr
,
353 const char *buf
, size_t count
)
355 int nr
= to_sensor_dev_attr(attr
)->index
;
356 struct i2c_client
*client
= to_i2c_client(dev
);
357 struct asb100_data
*data
= i2c_get_clientdata(client
);
363 err
= kstrtoul(buf
, 10, &val
);
367 mutex_lock(&data
->update_lock
);
369 min
= FAN_FROM_REG(data
->fan_min
[nr
],
370 DIV_FROM_REG(data
->fan_div
[nr
]));
371 data
->fan_div
[nr
] = DIV_TO_REG(val
);
375 reg
= asb100_read_value(client
, ASB100_REG_VID_FANDIV
);
376 reg
= (reg
& 0xcf) | (data
->fan_div
[0] << 4);
377 asb100_write_value(client
, ASB100_REG_VID_FANDIV
, reg
);
381 reg
= asb100_read_value(client
, ASB100_REG_VID_FANDIV
);
382 reg
= (reg
& 0x3f) | (data
->fan_div
[1] << 6);
383 asb100_write_value(client
, ASB100_REG_VID_FANDIV
, reg
);
387 reg
= asb100_read_value(client
, ASB100_REG_PIN
);
388 reg
= (reg
& 0x3f) | (data
->fan_div
[2] << 6);
389 asb100_write_value(client
, ASB100_REG_PIN
, reg
);
394 FAN_TO_REG(min
, DIV_FROM_REG(data
->fan_div
[nr
]));
395 asb100_write_value(client
, ASB100_REG_FAN_MIN(nr
), data
->fan_min
[nr
]);
397 mutex_unlock(&data
->update_lock
);
402 #define sysfs_fan(offset) \
403 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
404 show_fan, NULL, offset - 1); \
405 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
406 show_fan_min, set_fan_min, offset - 1); \
407 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
408 show_fan_div, set_fan_div, offset - 1)
414 /* 4 Temp. Sensors */
415 static int sprintf_temp_from_reg(u16 reg
, char *buf
, int nr
)
421 ret
= sprintf(buf
, "%d\n", LM75_TEMP_FROM_REG(reg
));
423 case 0: case 3: default:
424 ret
= sprintf(buf
, "%d\n", TEMP_FROM_REG(reg
));
430 #define show_temp_reg(reg) \
431 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
434 int nr = to_sensor_dev_attr(attr)->index; \
435 struct asb100_data *data = asb100_update_device(dev); \
436 return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
440 show_temp_reg(temp_max
);
441 show_temp_reg(temp_hyst
);
443 #define set_temp_reg(REG, reg) \
444 static ssize_t set_##reg(struct device *dev, struct device_attribute *attr, \
445 const char *buf, size_t count) \
447 int nr = to_sensor_dev_attr(attr)->index; \
448 struct i2c_client *client = to_i2c_client(dev); \
449 struct asb100_data *data = i2c_get_clientdata(client); \
451 int err = kstrtol(buf, 10, &val); \
454 mutex_lock(&data->update_lock); \
457 data->reg[nr] = LM75_TEMP_TO_REG(val); \
459 case 0: case 3: default: \
460 data->reg[nr] = TEMP_TO_REG(val); \
463 asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
465 mutex_unlock(&data->update_lock); \
469 set_temp_reg(MAX
, temp_max
);
470 set_temp_reg(HYST
, temp_hyst
);
472 #define sysfs_temp(num) \
473 static SENSOR_DEVICE_ATTR(temp##num##_input, S_IRUGO, \
474 show_temp, NULL, num - 1); \
475 static SENSOR_DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
476 show_temp_max, set_temp_max, num - 1); \
477 static SENSOR_DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
478 show_temp_hyst, set_temp_hyst, num - 1)
486 static ssize_t
show_vid(struct device
*dev
, struct device_attribute
*attr
,
489 struct asb100_data
*data
= asb100_update_device(dev
);
490 return sprintf(buf
, "%d\n", vid_from_reg(data
->vid
, data
->vrm
));
493 static DEVICE_ATTR(cpu0_vid
, S_IRUGO
, show_vid
, NULL
);
496 static ssize_t
show_vrm(struct device
*dev
, struct device_attribute
*attr
,
499 struct asb100_data
*data
= dev_get_drvdata(dev
);
500 return sprintf(buf
, "%d\n", data
->vrm
);
503 static ssize_t
set_vrm(struct device
*dev
, struct device_attribute
*attr
,
504 const char *buf
, size_t count
)
506 struct asb100_data
*data
= dev_get_drvdata(dev
);
510 err
= kstrtoul(buf
, 10, &val
);
522 static DEVICE_ATTR(vrm
, S_IRUGO
| S_IWUSR
, show_vrm
, set_vrm
);
524 static ssize_t
show_alarms(struct device
*dev
, struct device_attribute
*attr
,
527 struct asb100_data
*data
= asb100_update_device(dev
);
528 return sprintf(buf
, "%u\n", data
->alarms
);
531 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms
, NULL
);
533 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
*attr
,
536 int bitnr
= to_sensor_dev_attr(attr
)->index
;
537 struct asb100_data
*data
= asb100_update_device(dev
);
538 return sprintf(buf
, "%u\n", (data
->alarms
>> bitnr
) & 1);
540 static SENSOR_DEVICE_ATTR(in0_alarm
, S_IRUGO
, show_alarm
, NULL
, 0);
541 static SENSOR_DEVICE_ATTR(in1_alarm
, S_IRUGO
, show_alarm
, NULL
, 1);
542 static SENSOR_DEVICE_ATTR(in2_alarm
, S_IRUGO
, show_alarm
, NULL
, 2);
543 static SENSOR_DEVICE_ATTR(in3_alarm
, S_IRUGO
, show_alarm
, NULL
, 3);
544 static SENSOR_DEVICE_ATTR(in4_alarm
, S_IRUGO
, show_alarm
, NULL
, 8);
545 static SENSOR_DEVICE_ATTR(fan1_alarm
, S_IRUGO
, show_alarm
, NULL
, 6);
546 static SENSOR_DEVICE_ATTR(fan2_alarm
, S_IRUGO
, show_alarm
, NULL
, 7);
547 static SENSOR_DEVICE_ATTR(fan3_alarm
, S_IRUGO
, show_alarm
, NULL
, 11);
548 static SENSOR_DEVICE_ATTR(temp1_alarm
, S_IRUGO
, show_alarm
, NULL
, 4);
549 static SENSOR_DEVICE_ATTR(temp2_alarm
, S_IRUGO
, show_alarm
, NULL
, 5);
550 static SENSOR_DEVICE_ATTR(temp3_alarm
, S_IRUGO
, show_alarm
, NULL
, 13);
553 static ssize_t
show_pwm1(struct device
*dev
, struct device_attribute
*attr
,
556 struct asb100_data
*data
= asb100_update_device(dev
);
557 return sprintf(buf
, "%d\n", ASB100_PWM_FROM_REG(data
->pwm
& 0x0f));
560 static ssize_t
set_pwm1(struct device
*dev
, struct device_attribute
*attr
,
561 const char *buf
, size_t count
)
563 struct i2c_client
*client
= to_i2c_client(dev
);
564 struct asb100_data
*data
= i2c_get_clientdata(client
);
568 err
= kstrtoul(buf
, 10, &val
);
572 mutex_lock(&data
->update_lock
);
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
);
576 mutex_unlock(&data
->update_lock
);
580 static ssize_t
show_pwm_enable1(struct device
*dev
,
581 struct device_attribute
*attr
, char *buf
)
583 struct asb100_data
*data
= asb100_update_device(dev
);
584 return sprintf(buf
, "%d\n", (data
->pwm
& 0x80) ? 1 : 0);
587 static ssize_t
set_pwm_enable1(struct device
*dev
,
588 struct device_attribute
*attr
, const char *buf
, size_t count
)
590 struct i2c_client
*client
= to_i2c_client(dev
);
591 struct asb100_data
*data
= i2c_get_clientdata(client
);
595 err
= kstrtoul(buf
, 10, &val
);
599 mutex_lock(&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 mutex_unlock(&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
);
611 static struct attribute
*asb100_attributes
[] = {
612 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
613 &sensor_dev_attr_in0_min
.dev_attr
.attr
,
614 &sensor_dev_attr_in0_max
.dev_attr
.attr
,
615 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
616 &sensor_dev_attr_in1_min
.dev_attr
.attr
,
617 &sensor_dev_attr_in1_max
.dev_attr
.attr
,
618 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
619 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
620 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
621 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
622 &sensor_dev_attr_in3_min
.dev_attr
.attr
,
623 &sensor_dev_attr_in3_max
.dev_attr
.attr
,
624 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
625 &sensor_dev_attr_in4_min
.dev_attr
.attr
,
626 &sensor_dev_attr_in4_max
.dev_attr
.attr
,
627 &sensor_dev_attr_in5_input
.dev_attr
.attr
,
628 &sensor_dev_attr_in5_min
.dev_attr
.attr
,
629 &sensor_dev_attr_in5_max
.dev_attr
.attr
,
630 &sensor_dev_attr_in6_input
.dev_attr
.attr
,
631 &sensor_dev_attr_in6_min
.dev_attr
.attr
,
632 &sensor_dev_attr_in6_max
.dev_attr
.attr
,
634 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
635 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
636 &sensor_dev_attr_fan1_div
.dev_attr
.attr
,
637 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
638 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
639 &sensor_dev_attr_fan2_div
.dev_attr
.attr
,
640 &sensor_dev_attr_fan3_input
.dev_attr
.attr
,
641 &sensor_dev_attr_fan3_min
.dev_attr
.attr
,
642 &sensor_dev_attr_fan3_div
.dev_attr
.attr
,
644 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
645 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
646 &sensor_dev_attr_temp1_max_hyst
.dev_attr
.attr
,
647 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
648 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
649 &sensor_dev_attr_temp2_max_hyst
.dev_attr
.attr
,
650 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
651 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
652 &sensor_dev_attr_temp3_max_hyst
.dev_attr
.attr
,
653 &sensor_dev_attr_temp4_input
.dev_attr
.attr
,
654 &sensor_dev_attr_temp4_max
.dev_attr
.attr
,
655 &sensor_dev_attr_temp4_max_hyst
.dev_attr
.attr
,
657 &sensor_dev_attr_in0_alarm
.dev_attr
.attr
,
658 &sensor_dev_attr_in1_alarm
.dev_attr
.attr
,
659 &sensor_dev_attr_in2_alarm
.dev_attr
.attr
,
660 &sensor_dev_attr_in3_alarm
.dev_attr
.attr
,
661 &sensor_dev_attr_in4_alarm
.dev_attr
.attr
,
662 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
663 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
664 &sensor_dev_attr_fan3_alarm
.dev_attr
.attr
,
665 &sensor_dev_attr_temp1_alarm
.dev_attr
.attr
,
666 &sensor_dev_attr_temp2_alarm
.dev_attr
.attr
,
667 &sensor_dev_attr_temp3_alarm
.dev_attr
.attr
,
669 &dev_attr_cpu0_vid
.attr
,
671 &dev_attr_alarms
.attr
,
673 &dev_attr_pwm1_enable
.attr
,
678 static const struct attribute_group asb100_group
= {
679 .attrs
= asb100_attributes
,
682 static int asb100_detect_subclients(struct i2c_client
*client
)
685 int address
= client
->addr
;
686 unsigned short sc_addr
[2];
687 struct asb100_data
*data
= i2c_get_clientdata(client
);
688 struct i2c_adapter
*adapter
= client
->adapter
;
690 id
= i2c_adapter_id(adapter
);
692 if (force_subclients
[0] == id
&& force_subclients
[1] == address
) {
693 for (i
= 2; i
<= 3; i
++) {
694 if (force_subclients
[i
] < 0x48 ||
695 force_subclients
[i
] > 0x4f) {
696 dev_err(&client
->dev
,
697 "invalid subclient address %d; must be 0x48-0x4f\n",
698 force_subclients
[i
]);
703 asb100_write_value(client
, ASB100_REG_I2C_SUBADDR
,
704 (force_subclients
[2] & 0x07) |
705 ((force_subclients
[3] & 0x07) << 4));
706 sc_addr
[0] = force_subclients
[2];
707 sc_addr
[1] = force_subclients
[3];
709 int val
= asb100_read_value(client
, ASB100_REG_I2C_SUBADDR
);
710 sc_addr
[0] = 0x48 + (val
& 0x07);
711 sc_addr
[1] = 0x48 + ((val
>> 4) & 0x07);
714 if (sc_addr
[0] == sc_addr
[1]) {
715 dev_err(&client
->dev
,
716 "duplicate addresses 0x%x for subclients\n",
722 data
->lm75
[0] = i2c_new_dummy(adapter
, sc_addr
[0]);
723 if (!data
->lm75
[0]) {
724 dev_err(&client
->dev
,
725 "subclient %d registration at address 0x%x failed.\n",
731 data
->lm75
[1] = i2c_new_dummy(adapter
, sc_addr
[1]);
732 if (!data
->lm75
[1]) {
733 dev_err(&client
->dev
,
734 "subclient %d registration at address 0x%x failed.\n",
742 /* Undo inits in case of errors */
744 i2c_unregister_device(data
->lm75
[0]);
749 /* Return 0 if detection is successful, -ENODEV otherwise */
750 static int asb100_detect(struct i2c_client
*client
,
751 struct i2c_board_info
*info
)
753 struct i2c_adapter
*adapter
= client
->adapter
;
756 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
757 pr_debug("detect failed, smbus byte data not supported!\n");
761 val1
= i2c_smbus_read_byte_data(client
, ASB100_REG_BANK
);
762 val2
= i2c_smbus_read_byte_data(client
, ASB100_REG_CHIPMAN
);
764 /* If we're in bank 0 */
765 if ((!(val1
& 0x07)) &&
766 /* Check for ASB100 ID (low byte) */
767 (((!(val1
& 0x80)) && (val2
!= 0x94)) ||
768 /* Check for ASB100 ID (high byte ) */
769 ((val1
& 0x80) && (val2
!= 0x06)))) {
770 pr_debug("detect failed, bad chip id 0x%02x!\n", val2
);
774 /* Put it now into bank 0 and Vendor ID High Byte */
775 i2c_smbus_write_byte_data(client
, ASB100_REG_BANK
,
776 (i2c_smbus_read_byte_data(client
, ASB100_REG_BANK
) & 0x78)
779 /* Determine the chip type. */
780 val1
= i2c_smbus_read_byte_data(client
, ASB100_REG_WCHIPID
);
781 val2
= i2c_smbus_read_byte_data(client
, ASB100_REG_CHIPMAN
);
783 if (val1
!= 0x31 || val2
!= 0x06)
786 strlcpy(info
->type
, "asb100", I2C_NAME_SIZE
);
791 static int asb100_probe(struct i2c_client
*client
,
792 const struct i2c_device_id
*id
)
795 struct asb100_data
*data
;
797 data
= devm_kzalloc(&client
->dev
, sizeof(struct asb100_data
),
802 i2c_set_clientdata(client
, data
);
803 mutex_init(&data
->lock
);
804 mutex_init(&data
->update_lock
);
806 /* Attach secondary lm75 clients */
807 err
= asb100_detect_subclients(client
);
811 /* Initialize the chip */
812 asb100_init_client(client
);
814 /* A few vars need to be filled upon startup */
815 data
->fan_min
[0] = asb100_read_value(client
, ASB100_REG_FAN_MIN(0));
816 data
->fan_min
[1] = asb100_read_value(client
, ASB100_REG_FAN_MIN(1));
817 data
->fan_min
[2] = asb100_read_value(client
, ASB100_REG_FAN_MIN(2));
819 /* Register sysfs hooks */
820 err
= sysfs_create_group(&client
->dev
.kobj
, &asb100_group
);
824 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
825 if (IS_ERR(data
->hwmon_dev
)) {
826 err
= PTR_ERR(data
->hwmon_dev
);
833 sysfs_remove_group(&client
->dev
.kobj
, &asb100_group
);
835 i2c_unregister_device(data
->lm75
[1]);
836 i2c_unregister_device(data
->lm75
[0]);
840 static int asb100_remove(struct i2c_client
*client
)
842 struct asb100_data
*data
= i2c_get_clientdata(client
);
844 hwmon_device_unregister(data
->hwmon_dev
);
845 sysfs_remove_group(&client
->dev
.kobj
, &asb100_group
);
847 i2c_unregister_device(data
->lm75
[1]);
848 i2c_unregister_device(data
->lm75
[0]);
854 * The SMBus locks itself, usually, but nothing may access the chip between
857 static int asb100_read_value(struct i2c_client
*client
, u16 reg
)
859 struct asb100_data
*data
= i2c_get_clientdata(client
);
860 struct i2c_client
*cl
;
863 mutex_lock(&data
->lock
);
865 bank
= (reg
>> 8) & 0x0f;
868 i2c_smbus_write_byte_data(client
, ASB100_REG_BANK
, bank
);
870 if (bank
== 0 || bank
> 2) {
871 res
= i2c_smbus_read_byte_data(client
, reg
& 0xff);
873 /* switch to subclient */
874 cl
= data
->lm75
[bank
- 1];
876 /* convert from ISA to LM75 I2C addresses */
877 switch (reg
& 0xff) {
878 case 0x50: /* TEMP */
879 res
= i2c_smbus_read_word_swapped(cl
, 0);
881 case 0x52: /* CONFIG */
882 res
= i2c_smbus_read_byte_data(cl
, 1);
884 case 0x53: /* HYST */
885 res
= i2c_smbus_read_word_swapped(cl
, 2);
889 res
= i2c_smbus_read_word_swapped(cl
, 3);
895 i2c_smbus_write_byte_data(client
, ASB100_REG_BANK
, 0);
897 mutex_unlock(&data
->lock
);
902 static void asb100_write_value(struct i2c_client
*client
, u16 reg
, u16 value
)
904 struct asb100_data
*data
= i2c_get_clientdata(client
);
905 struct i2c_client
*cl
;
908 mutex_lock(&data
->lock
);
910 bank
= (reg
>> 8) & 0x0f;
913 i2c_smbus_write_byte_data(client
, ASB100_REG_BANK
, bank
);
915 if (bank
== 0 || bank
> 2) {
916 i2c_smbus_write_byte_data(client
, reg
& 0xff, value
& 0xff);
918 /* switch to subclient */
919 cl
= data
->lm75
[bank
- 1];
921 /* convert from ISA to LM75 I2C addresses */
922 switch (reg
& 0xff) {
923 case 0x52: /* CONFIG */
924 i2c_smbus_write_byte_data(cl
, 1, value
& 0xff);
926 case 0x53: /* HYST */
927 i2c_smbus_write_word_swapped(cl
, 2, value
);
930 i2c_smbus_write_word_swapped(cl
, 3, value
);
936 i2c_smbus_write_byte_data(client
, ASB100_REG_BANK
, 0);
938 mutex_unlock(&data
->lock
);
941 static void asb100_init_client(struct i2c_client
*client
)
943 struct asb100_data
*data
= i2c_get_clientdata(client
);
945 data
->vrm
= vid_which_vrm();
947 /* Start monitoring */
948 asb100_write_value(client
, ASB100_REG_CONFIG
,
949 (asb100_read_value(client
, ASB100_REG_CONFIG
) & 0xf7) | 0x01);
952 static struct asb100_data
*asb100_update_device(struct device
*dev
)
954 struct i2c_client
*client
= to_i2c_client(dev
);
955 struct asb100_data
*data
= i2c_get_clientdata(client
);
958 mutex_lock(&data
->update_lock
);
960 if (time_after(jiffies
, data
->last_updated
+ HZ
+ HZ
/ 2)
963 dev_dbg(&client
->dev
, "starting device update...\n");
965 /* 7 voltage inputs */
966 for (i
= 0; i
< 7; i
++) {
967 data
->in
[i
] = asb100_read_value(client
,
969 data
->in_min
[i
] = asb100_read_value(client
,
970 ASB100_REG_IN_MIN(i
));
971 data
->in_max
[i
] = asb100_read_value(client
,
972 ASB100_REG_IN_MAX(i
));
976 for (i
= 0; i
< 3; i
++) {
977 data
->fan
[i
] = asb100_read_value(client
,
979 data
->fan_min
[i
] = asb100_read_value(client
,
980 ASB100_REG_FAN_MIN(i
));
983 /* 4 temperature inputs */
984 for (i
= 1; i
<= 4; i
++) {
985 data
->temp
[i
-1] = asb100_read_value(client
,
987 data
->temp_max
[i
-1] = asb100_read_value(client
,
988 ASB100_REG_TEMP_MAX(i
));
989 data
->temp_hyst
[i
-1] = asb100_read_value(client
,
990 ASB100_REG_TEMP_HYST(i
));
993 /* VID and fan divisors */
994 i
= asb100_read_value(client
, ASB100_REG_VID_FANDIV
);
995 data
->vid
= i
& 0x0f;
996 data
->vid
|= (asb100_read_value(client
,
997 ASB100_REG_CHIPID
) & 0x01) << 4;
998 data
->fan_div
[0] = (i
>> 4) & 0x03;
999 data
->fan_div
[1] = (i
>> 6) & 0x03;
1000 data
->fan_div
[2] = (asb100_read_value(client
,
1001 ASB100_REG_PIN
) >> 6) & 0x03;
1004 data
->pwm
= asb100_read_value(client
, ASB100_REG_PWM1
);
1007 data
->alarms
= asb100_read_value(client
, ASB100_REG_ALARM1
) +
1008 (asb100_read_value(client
, ASB100_REG_ALARM2
) << 8);
1010 data
->last_updated
= jiffies
;
1013 dev_dbg(&client
->dev
, "... device update complete\n");
1016 mutex_unlock(&data
->update_lock
);
1021 module_i2c_driver(asb100_driver
);
1023 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1024 MODULE_DESCRIPTION("ASB100 Bach driver");
1025 MODULE_LICENSE("GPL");