35 #include <linux/module.h>
37 #include <linux/slab.h>
38 #include <linux/i2c.h>
46 #define NUMBER_OF_VIN 10
47 #define NUMBER_OF_FANIN 5
48 #define NUMBER_OF_TEMPIN 3
49 #define NUMBER_OF_PWM 5
52 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
57 static unsigned short force_subclients[4];
60 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
131 static const u8 W83791D_REG_TEMP_TARGET[3] = {
137 static const u8 W83791D_REG_TEMP_TOL[2] = {
142 static const u8 W83791D_REG_FAN_CFG[2] = {
147 static const u8 W83791D_REG_FAN_DIV[3] = {
153 #define W83791D_REG_BANK 0x4E
154 #define W83791D_REG_TEMP2_CONFIG 0xC2
155 #define W83791D_REG_TEMP3_CONFIG 0xCA
157 static const u8 W83791D_REG_TEMP1[3] = {
163 static const u8 W83791D_REG_TEMP_ADD[2][6] = {
178 #define W83791D_REG_BEEP_CONFIG 0x4D
180 static const u8 W83791D_REG_BEEP_CTRL[3] = {
186 #define W83791D_REG_GPIO 0x15
187 #define W83791D_REG_CONFIG 0x40
188 #define W83791D_REG_VID_FANDIV 0x47
189 #define W83791D_REG_DID_VID4 0x49
190 #define W83791D_REG_WCHIPID 0x58
191 #define W83791D_REG_CHIPMAN 0x4F
192 #define W83791D_REG_PIN 0x4B
193 #define W83791D_REG_I2C_SUBADDR 0x4A
195 #define W83791D_REG_ALARM1 0xA9
196 #define W83791D_REG_ALARM2 0xAA
197 #define W83791D_REG_ALARM3 0xAB
199 #define W83791D_REG_VBAT 0x5D
200 #define W83791D_REG_I2C_ADDR 0x48
223 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8) / 16), 0, 255))
224 #define IN_FROM_REG(val) ((val) * 16)
226 static u8 fan_to_reg(
long rpm,
int div)
230 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
231 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
234 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \
235 ((val) == 255 ? 0 : \
236 1350000 / ((val) * (div))))
239 #define TEMP1_FROM_REG(val) ((val) * 1000)
240 #define TEMP1_TO_REG(val) ((val) <= -128000 ? -128 : \
241 (val) >= 127000 ? 127 : \
242 (val) < 0 ? ((val) - 500) / 1000 : \
243 ((val) + 500) / 1000)
251 #define TEMP23_FROM_REG(val) ((val) / 128 * 500)
252 #define TEMP23_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
253 (val) >= 127500 ? 0x7F80 : \
254 (val) < 0 ? ((val) - 250) / 500 * 128 : \
255 ((val) + 250) / 500 * 128)
258 #define TARGET_TEMP_TO_REG(val) ((val) < 0 ? 0 : \
259 (val) >= 127000 ? 127 : \
260 ((val) + 500) / 1000)
263 #define TOL_TEMP_TO_REG(val) ((val) >= 15000 ? 15 : \
264 ((val) + 500) / 1000)
266 #define BEEP_MASK_TO_REG(val) ((val) & 0xffffff)
267 #define BEEP_MASK_FROM_REG(val) ((val) & 0xffffff)
269 #define DIV_FROM_REG(val) (1 << (val))
271 static u8 div_to_reg(
int nr,
long val)
276 val = SENSORS_LIMIT(val, 1, 128) >> 1;
277 for (i = 0; i < 7; i++) {
361 .probe = w83791d_probe,
362 .remove = w83791d_remove,
363 .id_table = w83791d_id,
364 .detect = w83791d_detect,
365 .address_list = normal_i2c,
369 #define show_in_reg(reg) \
370 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
373 struct sensor_device_attribute *sensor_attr = \
374 to_sensor_dev_attr(attr); \
375 struct w83791d_data *data = w83791d_update_device(dev); \
376 int nr = sensor_attr->index; \
377 return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
384 #define store_in_reg(REG, reg) \
385 static ssize_t store_in_##reg(struct device *dev, \
386 struct device_attribute *attr, \
387 const char *buf, size_t count) \
389 struct sensor_device_attribute *sensor_attr = \
390 to_sensor_dev_attr(attr); \
391 struct i2c_client *client = to_i2c_client(dev); \
392 struct w83791d_data *data = i2c_get_clientdata(client); \
393 int nr = sensor_attr->index; \
395 int err = kstrtoul(buf, 10, &val); \
398 mutex_lock(&data->update_lock); \
399 data->in_##reg[nr] = IN_TO_REG(val); \
400 w83791d_write(client, W83791D_REG_IN_##REG[nr], data->in_##reg[nr]); \
401 mutex_unlock(&data->update_lock); \
454 int bitnr = sensor_attr->
index;
460 const char *buf,
size_t count)
466 int bitnr = sensor_attr->
index;
471 err = kstrtoul(buf, 10, &val);
479 data->
beep_mask &= ~(0xff << (bytenr * 8));
480 data->
beep_mask |= w83791d_read(client, W83791D_REG_BEEP_CTRL[bytenr])
486 w83791d_write(client, W83791D_REG_BEEP_CTRL[bytenr],
487 (data->
beep_mask >> (bytenr * 8)) & 0xff);
500 int bitnr = sensor_attr->
index;
535 #define show_fan_reg(reg) \
536 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
539 struct sensor_device_attribute *sensor_attr = \
540 to_sensor_dev_attr(attr); \
541 struct w83791d_data *data = w83791d_update_device(dev); \
542 int nr = sensor_attr->index; \
543 return sprintf(buf, "%d\n", \
544 FAN_FROM_REG(data->reg[nr], DIV_FROM_REG(data->fan_div[nr]))); \
551 const char *buf,
size_t count)
560 err = kstrtoul(buf, 10, &val);
566 w83791d_write(client, W83791D_REG_FAN_MIN[nr], data->
fan_min[nr]);
576 int nr = sensor_attr->
index;
588 const char *buf,
size_t count)
593 int nr = sensor_attr->
index;
604 err = kstrtoul(buf, 10, &val);
642 dev_warn(dev,
"store_fan_div: Unexpected nr seen: %d\n", nr);
648 fan_div_reg = w83791d_read(client, W83791D_REG_FAN_DIV[indx])
650 tmp_fan_div = (data->
fan_div[
nr] << new_shift) & ~keep_mask;
652 w83791d_write(client, W83791D_REG_FAN_DIV[indx],
653 fan_div_reg | tmp_fan_div);
657 keep_mask = ~(1 << (nr + 5));
660 tmp_fan_div = (data->
fan_div[
nr] << (3 +
nr)) & ~keep_mask;
662 vbat_reg | tmp_fan_div);
667 w83791d_write(client, W83791D_REG_FAN_MIN[nr], data->
fan_min[nr]);
687 show_fan_min, store_fan_min, 0),
689 show_fan_min, store_fan_min, 1),
691 show_fan_min, store_fan_min, 2),
693 show_fan_min, store_fan_min, 3),
695 show_fan_min, store_fan_min, 4),
700 show_fan_div, store_fan_div, 0),
702 show_fan_div, store_fan_div, 1),
704 show_fan_div, store_fan_div, 2),
706 show_fan_div, store_fan_div, 3),
708 show_fan_div, store_fan_div, 4),
732 int nr = sensor_attr->
index;
738 const char *buf,
size_t count)
743 int nr = sensor_attr->
index;
746 if (kstrtoul(buf, 10, &val))
750 data->
pwm[
nr] = SENSORS_LIMIT(val, 0, 255);
751 w83791d_write(client, W83791D_REG_PWM[nr], data->
pwm[nr]);
758 show_pwm, store_pwm, 0),
760 show_pwm, store_pwm, 1),
762 show_pwm, store_pwm, 2),
764 show_pwm, store_pwm, 3),
766 show_pwm, store_pwm, 4),
773 int nr = sensor_attr->
index;
784 int nr = sensor_attr->
index;
791 int ret = kstrtoul(buf, 10, &val);
793 if (ret || val < 1 || val > 3)
816 reg_cfg_tmp = w83791d_read(client, W83791D_REG_FAN_CFG[reg_idx]);
817 reg_cfg_tmp = (reg_cfg_tmp & keep_mask) |
820 w83791d_write(client, W83791D_REG_FAN_CFG[reg_idx], reg_cfg_tmp);
827 show_pwmenable, store_pwmenable, 0),
829 show_pwmenable, store_pwmenable, 1),
831 show_pwmenable, store_pwmenable, 2),
840 int nr = sensor_attr->
index;
850 int nr = sensor_attr->
index;
854 if (kstrtol(buf, 10, &val))
859 target_mask = w83791d_read(client,
860 W83791D_REG_TEMP_TARGET[nr]) & 0x80;
861 w83791d_write(client, W83791D_REG_TEMP_TARGET[nr],
869 show_temp_target, store_temp_target, 0),
871 show_temp_target, store_temp_target, 1),
873 show_temp_target, store_temp_target, 2),
881 int nr = sensor_attr->
index;
891 int nr = sensor_attr->
index;
898 if (kstrtoul(buf, 10, &val))
921 target_mask = w83791d_read(client,
922 W83791D_REG_TEMP_TOL[reg_idx]) & keep_mask;
923 w83791d_write(client, W83791D_REG_TEMP_TOL[reg_idx],
931 show_temp_tolerance, store_temp_tolerance, 0),
933 show_temp_tolerance, store_temp_tolerance, 1),
935 show_temp_tolerance, store_temp_tolerance, 2),
948 const char *buf,
size_t count)
953 int nr = attr->
index;
957 err = kstrtol(buf, 10, &val);
963 w83791d_write(client, W83791D_REG_TEMP1[nr], data->
temp1[nr]);
981 const char *buf,
size_t count)
989 int index = attr->
index;
991 err = kstrtol(buf, 10, &val);
997 w83791d_write(client, W83791D_REG_TEMP_ADD[nr][index * 2],
999 w83791d_write(client, W83791D_REG_TEMP_ADD[nr][index * 2 + 1],
1014 show_temp1, store_temp1, 0, 1),
1016 show_temp23, store_temp23, 0, 1),
1018 show_temp23, store_temp23, 1, 1),
1023 show_temp1, store_temp1, 0, 2),
1025 show_temp23, store_temp23, 0, 2),
1027 show_temp23, store_temp23, 1, 2),
1050 struct w83791d_data *data = w83791d_update_device(dev);
1058 #define GLOBAL_BEEP_ENABLE_SHIFT 15
1059 #define GLOBAL_BEEP_ENABLE_MASK (1 << GLOBAL_BEEP_ENABLE_SHIFT)
1064 struct w83791d_data *data = w83791d_update_device(dev);
1071 struct w83791d_data *data = w83791d_update_device(dev);
1078 const char *buf,
size_t count)
1081 struct w83791d_data *data = i2c_get_clientdata(client);
1086 err = kstrtol(buf, 10, &val);
1101 for (i = 0; i < 3; i++) {
1102 w83791d_write(client, W83791D_REG_BEEP_CTRL[i], (val & 0xff));
1113 const char *buf,
size_t count)
1116 struct w83791d_data *data = i2c_get_clientdata(client);
1120 err = kstrtol(buf, 10, &val);
1138 w83791d_write(client, W83791D_REG_BEEP_CTRL[1], val);
1147 show_beep_enable, store_beep_enable, 0),
1149 show_beep_mask, store_beep_mask, 1)
1156 struct w83791d_data *data = w83791d_update_device(dev);
1171 const char *buf,
size_t count)
1183 err = kstrtoul(buf, 10, &val);
1193 #define IN_UNIT_ATTRS(X) \
1194 &sda_in_input[X].dev_attr.attr, \
1195 &sda_in_min[X].dev_attr.attr, \
1196 &sda_in_max[X].dev_attr.attr, \
1197 &sda_in_beep[X].dev_attr.attr, \
1198 &sda_in_alarm[X].dev_attr.attr
1200 #define FAN_UNIT_ATTRS(X) \
1201 &sda_fan_input[X].dev_attr.attr, \
1202 &sda_fan_min[X].dev_attr.attr, \
1203 &sda_fan_div[X].dev_attr.attr, \
1204 &sda_fan_beep[X].dev_attr.attr, \
1205 &sda_fan_alarm[X].dev_attr.attr
1207 #define TEMP_UNIT_ATTRS(X) \
1208 &sda_temp_input[X].dev_attr.attr, \
1209 &sda_temp_max[X].dev_attr.attr, \
1210 &sda_temp_max_hyst[X].dev_attr.attr, \
1211 &sda_temp_beep[X].dev_attr.attr, \
1212 &sda_temp_alarm[X].dev_attr.attr
1214 static struct attribute *w83791d_attributes[] = {
1231 &dev_attr_alarms.attr,
1234 &dev_attr_cpu0_vid.attr,
1245 &sda_temp_tolerance[0].
dev_attr.attr,
1246 &sda_temp_tolerance[1].
dev_attr.attr,
1247 &sda_temp_tolerance[2].
dev_attr.attr,
1252 .attrs = w83791d_attributes,
1260 static struct attribute *w83791d_attributes_fanpwm45[] = {
1269 .attrs = w83791d_attributes_fanpwm45,
1272 static int w83791d_detect_subclients(
struct i2c_client *client)
1275 struct w83791d_data *data = i2c_get_clientdata(client);
1280 id = i2c_adapter_id(adapter);
1281 if (force_subclients[0] ==
id && force_subclients[1] == address) {
1282 for (i = 2; i <= 3; i++) {
1283 if (force_subclients[i] < 0x48 ||
1284 force_subclients[i] > 0x4f) {
1286 "invalid subclient "
1287 "address %d; must be 0x48-0x4f\n",
1288 force_subclients[i]);
1294 (force_subclients[2] & 0x07) |
1295 ((force_subclients[3] & 0x07) << 4));
1301 if (!(val & 0x80)) {
1303 ((val & 0x7) == ((val >> 4) & 0x7))) {
1305 "duplicate addresses 0x%x, "
1306 "use force_subclient\n",
1307 data->
lm75[0]->addr);
1312 0x48 + ((val >> 4) & 0x7));
1328 static int w83791d_detect(
struct i2c_client *client,
1333 unsigned short address = client->
addr;
1344 if (!(val1 & 0x07)) {
1345 if ((!(val1 & 0x80) && val2 != 0xa3) ||
1346 ((val1 & 0x80) && val2 != 0x5c)) {
1364 if (val1 != 0x71 || val2 != 0x5c)
1372 static int w83791d_probe(
struct i2c_client *client,
1383 dev_dbg(dev,
"Device ID version: %d.%d (0x%02x)\n",
1384 (val1 >> 5) & 0x07, (val1 >> 1) & 0x0f, val1);
1392 i2c_set_clientdata(client, data);
1395 err = w83791d_detect_subclients(client);
1400 w83791d_init_client(client);
1407 data->
fan_min[i] = w83791d_read(client, W83791D_REG_FAN_MIN[i]);
1418 &w83791d_group_fanpwm45);
1445 static int w83791d_remove(
struct i2c_client *client)
1447 struct w83791d_data *data = i2c_get_clientdata(client);
1460 static void w83791d_init_client(
struct i2c_client *client)
1462 struct w83791d_data *data = i2c_get_clientdata(client);
1480 if (reset ||
init) {
1493 tmp = w83791d_read(client, W83791D_REG_BEEP_CTRL[1]);
1494 w83791d_write(client, W83791D_REG_BEEP_CTRL[1], tmp & 0xef);
1522 struct w83791d_data *data = i2c_get_clientdata(client);
1524 u8 reg_array_tmp[3];
1531 dev_dbg(dev,
"Starting w83791d device update\n");
1535 data->
in[
i] = w83791d_read(client,
1537 data->
in_max[
i] = w83791d_read(client,
1538 W83791D_REG_IN_MAX[i]);
1539 data->
in_min[
i] = w83791d_read(client,
1540 W83791D_REG_IN_MIN[i]);
1546 data->
fan[
i] = w83791d_read(client,
1547 W83791D_REG_FAN[i]);
1548 data->
fan_min[
i] = w83791d_read(client,
1549 W83791D_REG_FAN_MIN[i]);
1553 for (i = 0; i < 3; i++) {
1554 reg_array_tmp[
i] = w83791d_read(client,
1555 W83791D_REG_FAN_DIV[i]);
1557 data->
fan_div[0] = (reg_array_tmp[0] >> 4) & 0x03;
1558 data->
fan_div[1] = (reg_array_tmp[0] >> 6) & 0x03;
1559 data->
fan_div[2] = (reg_array_tmp[1] >> 6) & 0x03;
1560 data->
fan_div[3] = reg_array_tmp[2] & 0x07;
1561 data->
fan_div[4] = (reg_array_tmp[2] >> 4) & 0x07;
1568 for (i = 0; i < 3; i++)
1569 data->
fan_div[i] |= (vbat_reg >> (3 + i)) & 0x04;
1573 data->
pwm[
i] = w83791d_read(client,
1574 W83791D_REG_PWM[i]);
1578 for (i = 0; i < 2; i++) {
1579 reg_array_tmp[
i] = w83791d_read(client,
1580 W83791D_REG_FAN_CFG[i]);
1582 data->
pwm_enable[0] = (reg_array_tmp[0] >> 2) & 0x03;
1583 data->
pwm_enable[1] = (reg_array_tmp[0] >> 4) & 0x03;
1584 data->
pwm_enable[2] = (reg_array_tmp[1] >> 2) & 0x03;
1587 for (i = 0; i < 3; i++) {
1589 W83791D_REG_TEMP_TARGET[i]) & 0x7f;
1593 for (i = 0; i < 2; i++) {
1594 reg_array_tmp[
i] = w83791d_read(client,
1595 W83791D_REG_TEMP_TOL[i]);
1602 for (i = 0; i < 3; i++) {
1603 data->
temp1[
i] = w83791d_read(client,
1604 W83791D_REG_TEMP1[i]);
1608 for (i = 0; i < 2; i++) {
1609 for (j = 0; j < 3; j++) {
1611 (w83791d_read(client,
1612 W83791D_REG_TEMP_ADD[i][j * 2]) << 8) |
1613 w83791d_read(client,
1614 W83791D_REG_TEMP_ADD[i][j * 2 + 1]);
1626 w83791d_read(client, W83791D_REG_BEEP_CTRL[0]) +
1627 (w83791d_read(client, W83791D_REG_BEEP_CTRL[1]) << 8) +
1628 (w83791d_read(client, W83791D_REG_BEEP_CTRL[2]) << 16);
1636 data->
vid = i & 0x0f;
1647 w83791d_print_debug(data, dev);
1658 dev_dbg(dev,
"======Start of w83791d debug values======\n");
1659 dev_dbg(dev,
"%d set of Voltages: ===>\n", NUMBER_OF_VIN);
1661 dev_dbg(dev,
"vin[%d] is: 0x%02x\n", i, data->
in[i]);
1662 dev_dbg(dev,
"vin[%d] min is: 0x%02x\n", i, data->
in_min[i]);
1663 dev_dbg(dev,
"vin[%d] max is: 0x%02x\n", i, data->
in_max[i]);
1665 dev_dbg(dev,
"%d set of Fan Counts/Divisors: ===>\n", NUMBER_OF_FANIN);
1667 dev_dbg(dev,
"fan[%d] is: 0x%02x\n", i, data->
fan[i]);
1677 for (i = 0; i < 3; i++)
1679 for (i = 0; i < 2; i++) {
1680 for (j = 0; j < 3; j++) {
1681 dev_dbg(dev,
"temp_add[%d][%d] is: 0x%04x\n", i, j,
1686 dev_dbg(dev,
"Misc Information: ===>\n");
1692 dev_dbg(dev,
"=======End of w83791d debug values========\n");