39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41 #include <linux/module.h>
42 #include <linux/slab.h>
43 #include <linux/i2c.h>
54 static const unsigned short normal_i2c[] = { 0x2d,
I2C_CLIENT_END };
56 static unsigned short force_subclients[4];
59 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
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))
67 #define ASB100_REG_FAN(nr) (0x28 + (nr))
68 #define ASB100_REG_FAN_MIN(nr) (0x3b + (nr))
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
100 #define ASB100_REG_PWM1 0x59
108 #define ASB100_IN_MIN 0
109 #define ASB100_IN_MAX 4080
118 return (nval + 8) / 16;
132 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
133 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
138 return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val *
div);
142 #define ASB100_TEMP_MIN -128000
143 #define ASB100_TEMP_MAX 127000
152 ntemp += (ntemp < 0 ? -500 : 500);
153 return (
u8)(ntemp / 1000);
158 return (
s8)reg * 1000;
165 static u8 ASB100_PWM_TO_REG(
int pwm)
167 pwm = SENSORS_LIMIT(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))
184 return val == 8 ? 3 : val == 4 ? 2 : val == 1 ? 0 : 1;
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)
313 struct asb100_data *data = asb100_update_device(dev);
322 struct asb100_data *data = asb100_update_device(dev);
327 const char *buf,
size_t count)
331 struct asb100_data *data = i2c_get_clientdata(client);
335 err = kstrtoul(buf, 10, &val);
353 const char *buf,
size_t count)
357 struct asb100_data *data = i2c_get_clientdata(client);
363 err = kstrtoul(buf, 10, &val);
376 reg = (reg & 0xcf) | (data->
fan_div[0] << 4);
382 reg = (reg & 0x3f) | (data->
fan_div[1] << 6);
388 reg = (reg & 0x3f) | (data->
fan_div[2] << 6);
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)
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:
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); \
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); \
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)
489 struct asb100_data *data = asb100_update_device(dev);
504 const char *buf,
size_t count)
510 err = kstrtoul(buf, 10, &val);
523 struct asb100_data *data = asb100_update_device(dev);
533 struct asb100_data *data = asb100_update_device(dev);
552 struct asb100_data *data = asb100_update_device(dev);
553 return sprintf(buf,
"%d\n", ASB100_PWM_FROM_REG(data->
pwm & 0x0f));
557 const char *buf,
size_t count)
560 struct asb100_data *data = i2c_get_clientdata(client);
564 err = kstrtoul(buf, 10, &val);
570 data->
pwm |= (0x0f & ASB100_PWM_TO_REG(val));
579 struct asb100_data *data = asb100_update_device(dev);
580 return sprintf(buf,
"%d\n", (data->
pwm & 0x80) ? 1 : 0);
587 struct asb100_data *data = i2c_get_clientdata(client);
591 err = kstrtoul(buf, 10, &val);
597 data->
pwm |= (val ? 0x80 : 0x00);
605 show_pwm_enable1, set_pwm_enable1);
607 static struct attribute *asb100_attributes[] = {
608 &sensor_dev_attr_in0_input.dev_attr.attr,
609 &sensor_dev_attr_in0_min.dev_attr.attr,
610 &sensor_dev_attr_in0_max.dev_attr.attr,
611 &sensor_dev_attr_in1_input.dev_attr.attr,
612 &sensor_dev_attr_in1_min.dev_attr.attr,
613 &sensor_dev_attr_in1_max.dev_attr.attr,
614 &sensor_dev_attr_in2_input.dev_attr.attr,
615 &sensor_dev_attr_in2_min.dev_attr.attr,
616 &sensor_dev_attr_in2_max.dev_attr.attr,
617 &sensor_dev_attr_in3_input.dev_attr.attr,
618 &sensor_dev_attr_in3_min.dev_attr.attr,
619 &sensor_dev_attr_in3_max.dev_attr.attr,
620 &sensor_dev_attr_in4_input.dev_attr.attr,
621 &sensor_dev_attr_in4_min.dev_attr.attr,
622 &sensor_dev_attr_in4_max.dev_attr.attr,
623 &sensor_dev_attr_in5_input.dev_attr.attr,
624 &sensor_dev_attr_in5_min.dev_attr.attr,
625 &sensor_dev_attr_in5_max.dev_attr.attr,
626 &sensor_dev_attr_in6_input.dev_attr.attr,
627 &sensor_dev_attr_in6_min.dev_attr.attr,
628 &sensor_dev_attr_in6_max.dev_attr.attr,
630 &sensor_dev_attr_fan1_input.dev_attr.attr,
631 &sensor_dev_attr_fan1_min.dev_attr.attr,
632 &sensor_dev_attr_fan1_div.dev_attr.attr,
633 &sensor_dev_attr_fan2_input.dev_attr.attr,
634 &sensor_dev_attr_fan2_min.dev_attr.attr,
635 &sensor_dev_attr_fan2_div.dev_attr.attr,
636 &sensor_dev_attr_fan3_input.dev_attr.attr,
637 &sensor_dev_attr_fan3_min.dev_attr.attr,
638 &sensor_dev_attr_fan3_div.dev_attr.attr,
640 &sensor_dev_attr_temp1_input.dev_attr.attr,
641 &sensor_dev_attr_temp1_max.dev_attr.attr,
642 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
643 &sensor_dev_attr_temp2_input.dev_attr.attr,
644 &sensor_dev_attr_temp2_max.dev_attr.attr,
645 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
646 &sensor_dev_attr_temp3_input.dev_attr.attr,
647 &sensor_dev_attr_temp3_max.dev_attr.attr,
648 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
649 &sensor_dev_attr_temp4_input.dev_attr.attr,
650 &sensor_dev_attr_temp4_max.dev_attr.attr,
651 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
653 &sensor_dev_attr_in0_alarm.dev_attr.attr,
654 &sensor_dev_attr_in1_alarm.dev_attr.attr,
655 &sensor_dev_attr_in2_alarm.dev_attr.attr,
656 &sensor_dev_attr_in3_alarm.dev_attr.attr,
657 &sensor_dev_attr_in4_alarm.dev_attr.attr,
658 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
659 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
660 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
661 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
662 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
663 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
665 &dev_attr_cpu0_vid.attr,
667 &dev_attr_alarms.attr,
669 &dev_attr_pwm1_enable.attr,
675 .attrs = asb100_attributes,
678 static int asb100_detect_subclients(
struct i2c_client *client)
682 unsigned short sc_addr[2];
683 struct asb100_data *data = i2c_get_clientdata(client);
686 id = i2c_adapter_id(adapter);
688 if (force_subclients[0] ==
id && force_subclients[1] == address) {
689 for (i = 2; i <= 3; i++) {
690 if (force_subclients[i] < 0x48 ||
691 force_subclients[i] > 0x4f) {
693 "address %d; must be 0x48-0x4f\n",
694 force_subclients[i]);
700 (force_subclients[2] & 0x07) |
701 ((force_subclients[3] & 0x07) << 4));
702 sc_addr[0] = force_subclients[2];
703 sc_addr[1] = force_subclients[3];
706 sc_addr[0] = 0x48 + (val & 0x07);
707 sc_addr[1] = 0x48 + ((val >> 4) & 0x07);
710 if (sc_addr[0] == sc_addr[1]) {
711 dev_err(&client->
dev,
"duplicate addresses 0x%x "
712 "for subclients\n", sc_addr[0]);
718 if (!data->
lm75[0]) {
719 dev_err(&client->
dev,
"subclient %d registration "
720 "at address 0x%x failed.\n", 1, sc_addr[0]);
726 if (!data->
lm75[1]) {
727 dev_err(&client->
dev,
"subclient %d registration "
728 "at address 0x%x failed.\n", 2, sc_addr[1]);
743 static int asb100_detect(
struct i2c_client *client,
750 pr_debug(
"detect failed, smbus byte data not supported!\n");
758 if ((!(val1 & 0x07)) &&
760 (((!(val1 & 0x80)) && (val2 != 0x94)) ||
762 ((val1 & 0x80) && (val2 != 0x06)))) {
763 pr_debug(
"detect failed, bad chip id 0x%02x!\n", val2);
776 if (val1 != 0x31 || val2 != 0x06)
784 static int asb100_probe(
struct i2c_client *client,
795 i2c_set_clientdata(client, data);
800 err = asb100_detect_subclients(client);
805 asb100_init_client(client);
833 static int asb100_remove(
struct i2c_client *client)
835 struct asb100_data *data = i2c_get_clientdata(client);
850 static int asb100_read_value(
struct i2c_client *client,
u16 reg)
852 struct asb100_data *data = i2c_get_clientdata(client);
858 bank = (reg >> 8) & 0x0f;
863 if (bank == 0 || bank > 2) {
867 cl = data->
lm75[bank - 1];
870 switch (reg & 0xff) {
872 res = i2c_smbus_read_word_swapped(cl, 0);
878 res = i2c_smbus_read_word_swapped(cl, 2);
882 res = i2c_smbus_read_word_swapped(cl, 3);
897 struct asb100_data *data = i2c_get_clientdata(client);
903 bank = (reg >> 8) & 0x0f;
908 if (bank == 0 || bank > 2) {
912 cl = data->
lm75[bank - 1];
915 switch (reg & 0xff) {
920 i2c_smbus_write_word_swapped(cl, 2, value);
923 i2c_smbus_write_word_swapped(cl, 3, value);
934 static void asb100_init_client(
struct i2c_client *client)
936 struct asb100_data *data = i2c_get_clientdata(client);
948 struct asb100_data *data = i2c_get_clientdata(client);
956 dev_dbg(&client->
dev,
"starting device update...\n");
959 for (i = 0; i < 7; i++) {
960 data->
in[
i] = asb100_read_value(client,
962 data->
in_min[
i] = asb100_read_value(client,
964 data->
in_max[
i] = asb100_read_value(client,
969 for (i = 0; i < 3; i++) {
970 data->
fan[
i] = asb100_read_value(client,
972 data->
fan_min[
i] = asb100_read_value(client,
977 for (i = 1; i <= 4; i++) {
978 data->
temp[i-1] = asb100_read_value(client,
980 data->
temp_max[i-1] = asb100_read_value(client,
982 data->
temp_hyst[i-1] = asb100_read_value(client,
988 data->
vid = i & 0x0f;
989 data->
vid |= (asb100_read_value(client,
991 data->
fan_div[0] = (i >> 4) & 0x03;
992 data->
fan_div[1] = (i >> 6) & 0x03;
993 data->
fan_div[2] = (asb100_read_value(client,
1006 dev_dbg(&client->
dev,
"... device update complete\n");