1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * asb100.c - Part of lm_sensors, Linux kernel modules for hardware
8 * (derived from w83781d.c)
16 * This driver supports the hardware sensor chips: Asus ASB100 and
19 * ASB100-A supports pwm1, while plain ASB100 does not. There is no known
20 * way for the driver to tell which one is there.
22 * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
23 * asb100 7 3 1 4 0x31 0x0694 yes no
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/i2c.h>
31 #include <linux/hwmon.h>
32 #include <linux/hwmon-sysfs.h>
33 #include <linux/hwmon-vid.h>
34 #include <linux/err.h>
35 #include <linux/init.h>
36 #include <linux/jiffies.h>
37 #include <linux/mutex.h>
40 /* I2C addresses to scan */
41 static const unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
43 static unsigned short force_subclients[4];
44 module_param_array(force_subclients, short, NULL, 0);
45 MODULE_PARM_DESC(force_subclients,
46 "List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}");
48 /* Voltage IN registers 0-6 */
49 #define ASB100_REG_IN(nr) (0x20 + (nr))
50 #define ASB100_REG_IN_MAX(nr) (0x2b + (nr * 2))
51 #define ASB100_REG_IN_MIN(nr) (0x2c + (nr * 2))
53 /* FAN IN registers 1-3 */
54 #define ASB100_REG_FAN(nr) (0x28 + (nr))
55 #define ASB100_REG_FAN_MIN(nr) (0x3b + (nr))
57 /* TEMPERATURE registers 1-4 */
58 static const u16 asb100_reg_temp[] = {0, 0x27, 0x150, 0x250, 0x17};
59 static const u16 asb100_reg_temp_max[] = {0, 0x39, 0x155, 0x255, 0x18};
60 static const u16 asb100_reg_temp_hyst[] = {0, 0x3a, 0x153, 0x253, 0x19};
62 #define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])
63 #define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])
64 #define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])
66 #define ASB100_REG_TEMP2_CONFIG 0x0152
67 #define ASB100_REG_TEMP3_CONFIG 0x0252
70 #define ASB100_REG_CONFIG 0x40
71 #define ASB100_REG_ALARM1 0x41
72 #define ASB100_REG_ALARM2 0x42
73 #define ASB100_REG_SMIM1 0x43
74 #define ASB100_REG_SMIM2 0x44
75 #define ASB100_REG_VID_FANDIV 0x47
76 #define ASB100_REG_I2C_ADDR 0x48
77 #define ASB100_REG_CHIPID 0x49
78 #define ASB100_REG_I2C_SUBADDR 0x4a
79 #define ASB100_REG_PIN 0x4b
80 #define ASB100_REG_IRQ 0x4c
81 #define ASB100_REG_BANK 0x4e
82 #define ASB100_REG_CHIPMAN 0x4f
84 #define ASB100_REG_WCHIPID 0x58
86 /* bit 7 -> enable, bits 0-3 -> duty cycle */
87 #define ASB100_REG_PWM1 0x59
91 * Rounding and limit checking is only done on the TO_REG variants.
94 /* These constants are a guess, consistent w/ w83781d */
95 #define ASB100_IN_MIN 0
96 #define ASB100_IN_MAX 4080
99 * IN: 1/1000 V (0V to 4.08V)
102 static u8 IN_TO_REG(unsigned val)
104 unsigned nval = clamp_val(val, ASB100_IN_MIN, ASB100_IN_MAX);
105 return (nval + 8) / 16;
108 static unsigned IN_FROM_REG(u8 reg)
113 static u8 FAN_TO_REG(long rpm, int div)
119 rpm = clamp_val(rpm, 1, 1000000);
120 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
123 static int FAN_FROM_REG(u8 val, int div)
125 return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
128 /* These constants are a guess, consistent w/ w83781d */
129 #define ASB100_TEMP_MIN -128000
130 #define ASB100_TEMP_MAX 127000
133 * TEMP: 0.001C/bit (-128C to +127C)
134 * REG: 1C/bit, two's complement
136 static u8 TEMP_TO_REG(long temp)
138 int ntemp = clamp_val(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX);
139 ntemp += (ntemp < 0 ? -500 : 500);
140 return (u8)(ntemp / 1000);
143 static int TEMP_FROM_REG(u8 reg)
145 return (s8)reg * 1000;
149 * PWM: 0 - 255 per sensors documentation
150 * REG: (6.25% duty cycle per bit)
152 static u8 ASB100_PWM_TO_REG(int pwm)
154 pwm = clamp_val(pwm, 0, 255);
155 return (u8)(pwm / 16);
158 static int ASB100_PWM_FROM_REG(u8 reg)
163 #define DIV_FROM_REG(val) (1 << (val))
166 * FAN DIV: 1, 2, 4, or 8 (defaults to 2)
167 * REG: 0, 1, 2, or 3 (respectively) (defaults to 1)
169 static u8 DIV_TO_REG(long val)
171 return val == 8 ? 3 : val == 4 ? 2 : val == 1 ? 0 : 1;
175 * For each registered client, we need to keep some data in memory. That
176 * data is pointed to by client->data. The structure itself is
177 * dynamically allocated, at the same time the client itself is allocated.
180 struct device *hwmon_dev;
183 struct mutex update_lock;
184 unsigned long last_updated; /* In jiffies */
186 /* array of 2 pointers to subclients */
187 struct i2c_client *lm75[2];
189 char valid; /* !=0 if following fields are valid */
190 u8 in[7]; /* Register value */
191 u8 in_max[7]; /* Register value */
192 u8 in_min[7]; /* Register value */
193 u8 fan[3]; /* Register value */
194 u8 fan_min[3]; /* Register value */
195 u16 temp[4]; /* Register value (0 and 3 are u8 only) */
196 u16 temp_max[4]; /* Register value (0 and 3 are u8 only) */
197 u16 temp_hyst[4]; /* Register value (0 and 3 are u8 only) */
198 u8 fan_div[3]; /* Register encoding, right justified */
199 u8 pwm; /* Register encoding */
200 u8 vid; /* Register encoding, combined */
201 u32 alarms; /* Register encoding, combined */
205 static int asb100_read_value(struct i2c_client *client, u16 reg);
206 static void asb100_write_value(struct i2c_client *client, u16 reg, u16 val);
208 static int asb100_probe(struct i2c_client *client,
209 const struct i2c_device_id *id);
210 static int asb100_detect(struct i2c_client *client,
211 struct i2c_board_info *info);
212 static int asb100_remove(struct i2c_client *client);
213 static struct asb100_data *asb100_update_device(struct device *dev);
214 static void asb100_init_client(struct i2c_client *client);
216 static const struct i2c_device_id asb100_id[] = {
220 MODULE_DEVICE_TABLE(i2c, asb100_id);
222 static struct i2c_driver asb100_driver = {
223 .class = I2C_CLASS_HWMON,
227 .probe = asb100_probe,
228 .remove = asb100_remove,
229 .id_table = asb100_id,
230 .detect = asb100_detect,
231 .address_list = normal_i2c,
235 #define show_in_reg(reg) \
236 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
239 int nr = to_sensor_dev_attr(attr)->index; \
240 struct asb100_data *data = asb100_update_device(dev); \
241 return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
248 #define set_in_reg(REG, reg) \
249 static ssize_t set_in_##reg(struct device *dev, struct device_attribute *attr, \
250 const char *buf, size_t count) \
252 int nr = to_sensor_dev_attr(attr)->index; \
253 struct i2c_client *client = to_i2c_client(dev); \
254 struct asb100_data *data = i2c_get_clientdata(client); \
256 int err = kstrtoul(buf, 10, &val); \
259 mutex_lock(&data->update_lock); \
260 data->in_##reg[nr] = IN_TO_REG(val); \
261 asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
262 data->in_##reg[nr]); \
263 mutex_unlock(&data->update_lock); \
270 #define sysfs_in(offset) \
271 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
272 show_in, NULL, offset); \
273 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
274 show_in_min, set_in_min, offset); \
275 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
276 show_in_max, set_in_max, offset)
287 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
290 int nr = to_sensor_dev_attr(attr)->index;
291 struct asb100_data *data = asb100_update_device(dev);
292 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
293 DIV_FROM_REG(data->fan_div[nr])));
296 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
299 int nr = to_sensor_dev_attr(attr)->index;
300 struct asb100_data *data = asb100_update_device(dev);
301 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
302 DIV_FROM_REG(data->fan_div[nr])));
305 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
308 int nr = to_sensor_dev_attr(attr)->index;
309 struct asb100_data *data = asb100_update_device(dev);
310 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
313 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
314 const char *buf, size_t count)
316 int nr = to_sensor_dev_attr(attr)->index;
317 struct i2c_client *client = to_i2c_client(dev);
318 struct asb100_data *data = i2c_get_clientdata(client);
322 err = kstrtoul(buf, 10, &val);
326 mutex_lock(&data->update_lock);
327 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
328 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
329 mutex_unlock(&data->update_lock);
334 * Note: we save and restore the fan minimum here, because its value is
335 * determined in part by the fan divisor. This follows the principle of
336 * least surprise; the user doesn't expect the fan minimum to change just
337 * because the divisor changed.
339 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
340 const char *buf, size_t count)
342 int nr = to_sensor_dev_attr(attr)->index;
343 struct i2c_client *client = to_i2c_client(dev);
344 struct asb100_data *data = i2c_get_clientdata(client);
350 err = kstrtoul(buf, 10, &val);
354 mutex_lock(&data->update_lock);
356 min = FAN_FROM_REG(data->fan_min[nr],
357 DIV_FROM_REG(data->fan_div[nr]));
358 data->fan_div[nr] = DIV_TO_REG(val);
362 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
363 reg = (reg & 0xcf) | (data->fan_div[0] << 4);
364 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
368 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
369 reg = (reg & 0x3f) | (data->fan_div[1] << 6);
370 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
374 reg = asb100_read_value(client, ASB100_REG_PIN);
375 reg = (reg & 0x3f) | (data->fan_div[2] << 6);
376 asb100_write_value(client, ASB100_REG_PIN, reg);
381 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
382 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
384 mutex_unlock(&data->update_lock);
389 #define sysfs_fan(offset) \
390 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
391 show_fan, NULL, offset - 1); \
392 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
393 show_fan_min, set_fan_min, offset - 1); \
394 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
395 show_fan_div, set_fan_div, offset - 1)
401 /* 4 Temp. Sensors */
402 static int sprintf_temp_from_reg(u16 reg, char *buf, int nr)
408 ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg));
410 case 0: case 3: default:
411 ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
417 #define show_temp_reg(reg) \
418 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
421 int nr = to_sensor_dev_attr(attr)->index; \
422 struct asb100_data *data = asb100_update_device(dev); \
423 return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
427 show_temp_reg(temp_max);
428 show_temp_reg(temp_hyst);
430 #define set_temp_reg(REG, reg) \
431 static ssize_t set_##reg(struct device *dev, struct device_attribute *attr, \
432 const char *buf, size_t count) \
434 int nr = to_sensor_dev_attr(attr)->index; \
435 struct i2c_client *client = to_i2c_client(dev); \
436 struct asb100_data *data = i2c_get_clientdata(client); \
438 int err = kstrtol(buf, 10, &val); \
441 mutex_lock(&data->update_lock); \
444 data->reg[nr] = LM75_TEMP_TO_REG(val); \
446 case 0: case 3: default: \
447 data->reg[nr] = TEMP_TO_REG(val); \
450 asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
452 mutex_unlock(&data->update_lock); \
456 set_temp_reg(MAX, temp_max);
457 set_temp_reg(HYST, temp_hyst);
459 #define sysfs_temp(num) \
460 static SENSOR_DEVICE_ATTR(temp##num##_input, S_IRUGO, \
461 show_temp, NULL, num - 1); \
462 static SENSOR_DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
463 show_temp_max, set_temp_max, num - 1); \
464 static SENSOR_DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
465 show_temp_hyst, set_temp_hyst, num - 1)
473 static ssize_t cpu0_vid_show(struct device *dev,
474 struct device_attribute *attr, char *buf)
476 struct asb100_data *data = asb100_update_device(dev);
477 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
480 static DEVICE_ATTR_RO(cpu0_vid);
483 static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
486 struct asb100_data *data = dev_get_drvdata(dev);
487 return sprintf(buf, "%d\n", data->vrm);
490 static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
491 const char *buf, size_t count)
493 struct asb100_data *data = dev_get_drvdata(dev);
497 err = kstrtoul(buf, 10, &val);
509 static DEVICE_ATTR_RW(vrm);
511 static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
514 struct asb100_data *data = asb100_update_device(dev);
515 return sprintf(buf, "%u\n", data->alarms);
518 static DEVICE_ATTR_RO(alarms);
520 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
523 int bitnr = to_sensor_dev_attr(attr)->index;
524 struct asb100_data *data = asb100_update_device(dev);
525 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
527 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
528 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
529 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
530 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
531 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
532 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
533 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
534 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
535 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
536 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
537 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
540 static ssize_t pwm1_show(struct device *dev, struct device_attribute *attr,
543 struct asb100_data *data = asb100_update_device(dev);
544 return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
547 static ssize_t pwm1_store(struct device *dev, struct device_attribute *attr,
548 const char *buf, size_t count)
550 struct i2c_client *client = to_i2c_client(dev);
551 struct asb100_data *data = i2c_get_clientdata(client);
555 err = kstrtoul(buf, 10, &val);
559 mutex_lock(&data->update_lock);
560 data->pwm &= 0x80; /* keep the enable bit */
561 data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
562 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
563 mutex_unlock(&data->update_lock);
567 static ssize_t pwm1_enable_show(struct device *dev,
568 struct device_attribute *attr, char *buf)
570 struct asb100_data *data = asb100_update_device(dev);
571 return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
574 static ssize_t pwm1_enable_store(struct device *dev,
575 struct device_attribute *attr,
576 const char *buf, size_t count)
578 struct i2c_client *client = to_i2c_client(dev);
579 struct asb100_data *data = i2c_get_clientdata(client);
583 err = kstrtoul(buf, 10, &val);
587 mutex_lock(&data->update_lock);
588 data->pwm &= 0x0f; /* keep the duty cycle bits */
589 data->pwm |= (val ? 0x80 : 0x00);
590 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
591 mutex_unlock(&data->update_lock);
595 static DEVICE_ATTR_RW(pwm1);
596 static DEVICE_ATTR_RW(pwm1_enable);
598 static struct attribute *asb100_attributes[] = {
599 &sensor_dev_attr_in0_input.dev_attr.attr,
600 &sensor_dev_attr_in0_min.dev_attr.attr,
601 &sensor_dev_attr_in0_max.dev_attr.attr,
602 &sensor_dev_attr_in1_input.dev_attr.attr,
603 &sensor_dev_attr_in1_min.dev_attr.attr,
604 &sensor_dev_attr_in1_max.dev_attr.attr,
605 &sensor_dev_attr_in2_input.dev_attr.attr,
606 &sensor_dev_attr_in2_min.dev_attr.attr,
607 &sensor_dev_attr_in2_max.dev_attr.attr,
608 &sensor_dev_attr_in3_input.dev_attr.attr,
609 &sensor_dev_attr_in3_min.dev_attr.attr,
610 &sensor_dev_attr_in3_max.dev_attr.attr,
611 &sensor_dev_attr_in4_input.dev_attr.attr,
612 &sensor_dev_attr_in4_min.dev_attr.attr,
613 &sensor_dev_attr_in4_max.dev_attr.attr,
614 &sensor_dev_attr_in5_input.dev_attr.attr,
615 &sensor_dev_attr_in5_min.dev_attr.attr,
616 &sensor_dev_attr_in5_max.dev_attr.attr,
617 &sensor_dev_attr_in6_input.dev_attr.attr,
618 &sensor_dev_attr_in6_min.dev_attr.attr,
619 &sensor_dev_attr_in6_max.dev_attr.attr,
621 &sensor_dev_attr_fan1_input.dev_attr.attr,
622 &sensor_dev_attr_fan1_min.dev_attr.attr,
623 &sensor_dev_attr_fan1_div.dev_attr.attr,
624 &sensor_dev_attr_fan2_input.dev_attr.attr,
625 &sensor_dev_attr_fan2_min.dev_attr.attr,
626 &sensor_dev_attr_fan2_div.dev_attr.attr,
627 &sensor_dev_attr_fan3_input.dev_attr.attr,
628 &sensor_dev_attr_fan3_min.dev_attr.attr,
629 &sensor_dev_attr_fan3_div.dev_attr.attr,
631 &sensor_dev_attr_temp1_input.dev_attr.attr,
632 &sensor_dev_attr_temp1_max.dev_attr.attr,
633 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
634 &sensor_dev_attr_temp2_input.dev_attr.attr,
635 &sensor_dev_attr_temp2_max.dev_attr.attr,
636 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
637 &sensor_dev_attr_temp3_input.dev_attr.attr,
638 &sensor_dev_attr_temp3_max.dev_attr.attr,
639 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
640 &sensor_dev_attr_temp4_input.dev_attr.attr,
641 &sensor_dev_attr_temp4_max.dev_attr.attr,
642 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
644 &sensor_dev_attr_in0_alarm.dev_attr.attr,
645 &sensor_dev_attr_in1_alarm.dev_attr.attr,
646 &sensor_dev_attr_in2_alarm.dev_attr.attr,
647 &sensor_dev_attr_in3_alarm.dev_attr.attr,
648 &sensor_dev_attr_in4_alarm.dev_attr.attr,
649 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
650 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
651 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
652 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
653 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
654 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
656 &dev_attr_cpu0_vid.attr,
658 &dev_attr_alarms.attr,
660 &dev_attr_pwm1_enable.attr,
665 static const struct attribute_group asb100_group = {
666 .attrs = asb100_attributes,
669 static int asb100_detect_subclients(struct i2c_client *client)
672 int address = client->addr;
673 unsigned short sc_addr[2];
674 struct asb100_data *data = i2c_get_clientdata(client);
675 struct i2c_adapter *adapter = client->adapter;
677 id = i2c_adapter_id(adapter);
679 if (force_subclients[0] == id && force_subclients[1] == address) {
680 for (i = 2; i <= 3; i++) {
681 if (force_subclients[i] < 0x48 ||
682 force_subclients[i] > 0x4f) {
683 dev_err(&client->dev,
684 "invalid subclient address %d; must be 0x48-0x4f\n",
685 force_subclients[i]);
690 asb100_write_value(client, ASB100_REG_I2C_SUBADDR,
691 (force_subclients[2] & 0x07) |
692 ((force_subclients[3] & 0x07) << 4));
693 sc_addr[0] = force_subclients[2];
694 sc_addr[1] = force_subclients[3];
696 int val = asb100_read_value(client, ASB100_REG_I2C_SUBADDR);
697 sc_addr[0] = 0x48 + (val & 0x07);
698 sc_addr[1] = 0x48 + ((val >> 4) & 0x07);
701 if (sc_addr[0] == sc_addr[1]) {
702 dev_err(&client->dev,
703 "duplicate addresses 0x%x for subclients\n",
709 data->lm75[0] = i2c_new_dummy_device(adapter, sc_addr[0]);
710 if (IS_ERR(data->lm75[0])) {
711 dev_err(&client->dev,
712 "subclient %d registration at address 0x%x failed.\n",
714 err = PTR_ERR(data->lm75[0]);
718 data->lm75[1] = i2c_new_dummy_device(adapter, sc_addr[1]);
719 if (IS_ERR(data->lm75[1])) {
720 dev_err(&client->dev,
721 "subclient %d registration at address 0x%x failed.\n",
723 err = PTR_ERR(data->lm75[1]);
729 /* Undo inits in case of errors */
731 i2c_unregister_device(data->lm75[0]);
736 /* Return 0 if detection is successful, -ENODEV otherwise */
737 static int asb100_detect(struct i2c_client *client,
738 struct i2c_board_info *info)
740 struct i2c_adapter *adapter = client->adapter;
743 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
744 pr_debug("detect failed, smbus byte data not supported!\n");
748 val1 = i2c_smbus_read_byte_data(client, ASB100_REG_BANK);
749 val2 = i2c_smbus_read_byte_data(client, ASB100_REG_CHIPMAN);
751 /* If we're in bank 0 */
752 if ((!(val1 & 0x07)) &&
753 /* Check for ASB100 ID (low byte) */
754 (((!(val1 & 0x80)) && (val2 != 0x94)) ||
755 /* Check for ASB100 ID (high byte ) */
756 ((val1 & 0x80) && (val2 != 0x06)))) {
757 pr_debug("detect failed, bad chip id 0x%02x!\n", val2);
761 /* Put it now into bank 0 and Vendor ID High Byte */
762 i2c_smbus_write_byte_data(client, ASB100_REG_BANK,
763 (i2c_smbus_read_byte_data(client, ASB100_REG_BANK) & 0x78)
766 /* Determine the chip type. */
767 val1 = i2c_smbus_read_byte_data(client, ASB100_REG_WCHIPID);
768 val2 = i2c_smbus_read_byte_data(client, ASB100_REG_CHIPMAN);
770 if (val1 != 0x31 || val2 != 0x06)
773 strlcpy(info->type, "asb100", I2C_NAME_SIZE);
778 static int asb100_probe(struct i2c_client *client,
779 const struct i2c_device_id *id)
782 struct asb100_data *data;
784 data = devm_kzalloc(&client->dev, sizeof(struct asb100_data),
789 i2c_set_clientdata(client, data);
790 mutex_init(&data->lock);
791 mutex_init(&data->update_lock);
793 /* Attach secondary lm75 clients */
794 err = asb100_detect_subclients(client);
798 /* Initialize the chip */
799 asb100_init_client(client);
801 /* A few vars need to be filled upon startup */
802 data->fan_min[0] = asb100_read_value(client, ASB100_REG_FAN_MIN(0));
803 data->fan_min[1] = asb100_read_value(client, ASB100_REG_FAN_MIN(1));
804 data->fan_min[2] = asb100_read_value(client, ASB100_REG_FAN_MIN(2));
806 /* Register sysfs hooks */
807 err = sysfs_create_group(&client->dev.kobj, &asb100_group);
811 data->hwmon_dev = hwmon_device_register(&client->dev);
812 if (IS_ERR(data->hwmon_dev)) {
813 err = PTR_ERR(data->hwmon_dev);
820 sysfs_remove_group(&client->dev.kobj, &asb100_group);
822 i2c_unregister_device(data->lm75[1]);
823 i2c_unregister_device(data->lm75[0]);
827 static int asb100_remove(struct i2c_client *client)
829 struct asb100_data *data = i2c_get_clientdata(client);
831 hwmon_device_unregister(data->hwmon_dev);
832 sysfs_remove_group(&client->dev.kobj, &asb100_group);
834 i2c_unregister_device(data->lm75[1]);
835 i2c_unregister_device(data->lm75[0]);
841 * The SMBus locks itself, usually, but nothing may access the chip between
844 static int asb100_read_value(struct i2c_client *client, u16 reg)
846 struct asb100_data *data = i2c_get_clientdata(client);
847 struct i2c_client *cl;
850 mutex_lock(&data->lock);
852 bank = (reg >> 8) & 0x0f;
855 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
857 if (bank == 0 || bank > 2) {
858 res = i2c_smbus_read_byte_data(client, reg & 0xff);
860 /* switch to subclient */
861 cl = data->lm75[bank - 1];
863 /* convert from ISA to LM75 I2C addresses */
864 switch (reg & 0xff) {
865 case 0x50: /* TEMP */
866 res = i2c_smbus_read_word_swapped(cl, 0);
868 case 0x52: /* CONFIG */
869 res = i2c_smbus_read_byte_data(cl, 1);
871 case 0x53: /* HYST */
872 res = i2c_smbus_read_word_swapped(cl, 2);
876 res = i2c_smbus_read_word_swapped(cl, 3);
882 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
884 mutex_unlock(&data->lock);
889 static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
891 struct asb100_data *data = i2c_get_clientdata(client);
892 struct i2c_client *cl;
895 mutex_lock(&data->lock);
897 bank = (reg >> 8) & 0x0f;
900 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
902 if (bank == 0 || bank > 2) {
903 i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
905 /* switch to subclient */
906 cl = data->lm75[bank - 1];
908 /* convert from ISA to LM75 I2C addresses */
909 switch (reg & 0xff) {
910 case 0x52: /* CONFIG */
911 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
913 case 0x53: /* HYST */
914 i2c_smbus_write_word_swapped(cl, 2, value);
917 i2c_smbus_write_word_swapped(cl, 3, value);
923 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
925 mutex_unlock(&data->lock);
928 static void asb100_init_client(struct i2c_client *client)
930 struct asb100_data *data = i2c_get_clientdata(client);
932 data->vrm = vid_which_vrm();
934 /* Start monitoring */
935 asb100_write_value(client, ASB100_REG_CONFIG,
936 (asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
939 static struct asb100_data *asb100_update_device(struct device *dev)
941 struct i2c_client *client = to_i2c_client(dev);
942 struct asb100_data *data = i2c_get_clientdata(client);
945 mutex_lock(&data->update_lock);
947 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
950 dev_dbg(&client->dev, "starting device update...\n");
952 /* 7 voltage inputs */
953 for (i = 0; i < 7; i++) {
954 data->in[i] = asb100_read_value(client,
956 data->in_min[i] = asb100_read_value(client,
957 ASB100_REG_IN_MIN(i));
958 data->in_max[i] = asb100_read_value(client,
959 ASB100_REG_IN_MAX(i));
963 for (i = 0; i < 3; i++) {
964 data->fan[i] = asb100_read_value(client,
966 data->fan_min[i] = asb100_read_value(client,
967 ASB100_REG_FAN_MIN(i));
970 /* 4 temperature inputs */
971 for (i = 1; i <= 4; i++) {
972 data->temp[i-1] = asb100_read_value(client,
974 data->temp_max[i-1] = asb100_read_value(client,
975 ASB100_REG_TEMP_MAX(i));
976 data->temp_hyst[i-1] = asb100_read_value(client,
977 ASB100_REG_TEMP_HYST(i));
980 /* VID and fan divisors */
981 i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
982 data->vid = i & 0x0f;
983 data->vid |= (asb100_read_value(client,
984 ASB100_REG_CHIPID) & 0x01) << 4;
985 data->fan_div[0] = (i >> 4) & 0x03;
986 data->fan_div[1] = (i >> 6) & 0x03;
987 data->fan_div[2] = (asb100_read_value(client,
988 ASB100_REG_PIN) >> 6) & 0x03;
991 data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
994 data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
995 (asb100_read_value(client, ASB100_REG_ALARM2) << 8);
997 data->last_updated = jiffies;
1000 dev_dbg(&client->dev, "... device update complete\n");
1003 mutex_unlock(&data->update_lock);
1008 module_i2c_driver(asb100_driver);
1011 MODULE_DESCRIPTION("ASB100 Bach driver");
1012 MODULE_LICENSE("GPL");