0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include <linux/module.h>
0023 #include <linux/init.h>
0024 #include <linux/slab.h>
0025 #include <linux/i2c.h>
0026 #include <linux/hwmon.h>
0027 #include <linux/hwmon-vid.h>
0028 #include <linux/hwmon-sysfs.h>
0029 #include <linux/err.h>
0030 #include <linux/mutex.h>
0031 #include <linux/jiffies.h>
0032
0033 #define NUMBER_OF_VIN 10
0034 #define NUMBER_OF_FANIN 5
0035 #define NUMBER_OF_TEMPIN 3
0036 #define NUMBER_OF_PWM 5
0037
0038
0039 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
0040 I2C_CLIENT_END };
0041
0042
0043
0044 static unsigned short force_subclients[4];
0045 module_param_array(force_subclients, short, NULL, 0);
0046 MODULE_PARM_DESC(force_subclients,
0047 "List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}");
0048
0049 static bool reset;
0050 module_param(reset, bool, 0);
0051 MODULE_PARM_DESC(reset, "Set to one to force a hardware chip reset");
0052
0053 static bool init;
0054 module_param(init, bool, 0);
0055 MODULE_PARM_DESC(init, "Set to one to force extra software initialization");
0056
0057
0058 static const u8 W83791D_REG_IN[NUMBER_OF_VIN] = {
0059 0x20,
0060 0x21,
0061 0x22,
0062 0x23,
0063 0x24,
0064 0x25,
0065 0x26,
0066 0xB0,
0067 0xB1,
0068 0xB2
0069 };
0070
0071 static const u8 W83791D_REG_IN_MAX[NUMBER_OF_VIN] = {
0072 0x2B,
0073 0x2D,
0074 0x2F,
0075 0x31,
0076 0x33,
0077 0x35,
0078 0x37,
0079 0xB4,
0080 0xB6,
0081 0xB8
0082 };
0083 static const u8 W83791D_REG_IN_MIN[NUMBER_OF_VIN] = {
0084 0x2C,
0085 0x2E,
0086 0x30,
0087 0x32,
0088 0x34,
0089 0x36,
0090 0x38,
0091 0xB5,
0092 0xB7,
0093 0xB9
0094 };
0095 static const u8 W83791D_REG_FAN[NUMBER_OF_FANIN] = {
0096 0x28,
0097 0x29,
0098 0x2A,
0099 0xBA,
0100 0xBB,
0101 };
0102 static const u8 W83791D_REG_FAN_MIN[NUMBER_OF_FANIN] = {
0103 0x3B,
0104 0x3C,
0105 0x3D,
0106 0xBC,
0107 0xBD,
0108 };
0109
0110 static const u8 W83791D_REG_PWM[NUMBER_OF_PWM] = {
0111 0x81,
0112 0x83,
0113 0x94,
0114 0xA0,
0115 0xA1,
0116 };
0117
0118 static const u8 W83791D_REG_TEMP_TARGET[3] = {
0119 0x85,
0120 0x86,
0121 0x96,
0122 };
0123
0124 static const u8 W83791D_REG_TEMP_TOL[2] = {
0125 0x87,
0126 0x97,
0127 };
0128
0129 static const u8 W83791D_REG_FAN_CFG[2] = {
0130 0x84,
0131 0x95,
0132 };
0133
0134 static const u8 W83791D_REG_FAN_DIV[3] = {
0135 0x47,
0136 0x4b,
0137 0x5C,
0138 };
0139
0140 #define W83791D_REG_BANK 0x4E
0141 #define W83791D_REG_TEMP2_CONFIG 0xC2
0142 #define W83791D_REG_TEMP3_CONFIG 0xCA
0143
0144 static const u8 W83791D_REG_TEMP1[3] = {
0145 0x27,
0146 0x39,
0147 0x3A,
0148 };
0149
0150 static const u8 W83791D_REG_TEMP_ADD[2][6] = {
0151 {0xC0,
0152 0xC1,
0153 0xC5,
0154 0xC6,
0155 0xC3,
0156 0xC4},
0157 {0xC8,
0158 0xC9,
0159 0xCD,
0160 0xCE,
0161 0xCB,
0162 0xCC}
0163 };
0164
0165 #define W83791D_REG_BEEP_CONFIG 0x4D
0166
0167 static const u8 W83791D_REG_BEEP_CTRL[3] = {
0168 0x56,
0169 0x57,
0170 0xA3,
0171 };
0172
0173 #define W83791D_REG_GPIO 0x15
0174 #define W83791D_REG_CONFIG 0x40
0175 #define W83791D_REG_VID_FANDIV 0x47
0176 #define W83791D_REG_DID_VID4 0x49
0177 #define W83791D_REG_WCHIPID 0x58
0178 #define W83791D_REG_CHIPMAN 0x4F
0179 #define W83791D_REG_PIN 0x4B
0180 #define W83791D_REG_I2C_SUBADDR 0x4A
0181
0182 #define W83791D_REG_ALARM1 0xA9
0183 #define W83791D_REG_ALARM2 0xAA
0184 #define W83791D_REG_ALARM3 0xAB
0185
0186 #define W83791D_REG_VBAT 0x5D
0187 #define W83791D_REG_I2C_ADDR 0x48
0188
0189
0190
0191
0192
0193
0194
0195 static inline int w83791d_read(struct i2c_client *client, u8 reg)
0196 {
0197 return i2c_smbus_read_byte_data(client, reg);
0198 }
0199
0200 static inline int w83791d_write(struct i2c_client *client, u8 reg, u8 value)
0201 {
0202 return i2c_smbus_write_byte_data(client, reg, value);
0203 }
0204
0205
0206
0207
0208
0209
0210 #define IN_TO_REG(val) (clamp_val((((val) + 8) / 16), 0, 255))
0211 #define IN_FROM_REG(val) ((val) * 16)
0212
0213 static u8 fan_to_reg(long rpm, int div)
0214 {
0215 if (rpm == 0)
0216 return 255;
0217 rpm = clamp_val(rpm, 1, 1000000);
0218 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
0219 }
0220
0221 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \
0222 ((val) == 255 ? 0 : \
0223 1350000 / ((val) * (div))))
0224
0225
0226 #define TEMP1_FROM_REG(val) ((val) * 1000)
0227 #define TEMP1_TO_REG(val) ((val) <= -128000 ? -128 : \
0228 (val) >= 127000 ? 127 : \
0229 (val) < 0 ? ((val) - 500) / 1000 : \
0230 ((val) + 500) / 1000)
0231
0232
0233
0234
0235
0236
0237
0238 #define TEMP23_FROM_REG(val) ((val) / 128 * 500)
0239 #define TEMP23_TO_REG(val) (DIV_ROUND_CLOSEST(clamp_val((val), -128000, \
0240 127500), 500) * 128)
0241
0242
0243 #define TARGET_TEMP_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val((val), 0, 127000), \
0244 1000)
0245
0246
0247 #define TOL_TEMP_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val((val), 0, 15000), \
0248 1000)
0249
0250 #define BEEP_MASK_TO_REG(val) ((val) & 0xffffff)
0251 #define BEEP_MASK_FROM_REG(val) ((val) & 0xffffff)
0252
0253 #define DIV_FROM_REG(val) (1 << (val))
0254
0255 static u8 div_to_reg(int nr, long val)
0256 {
0257 int i;
0258
0259
0260 val = clamp_val(val, 1, 128) >> 1;
0261 for (i = 0; i < 7; i++) {
0262 if (val == 0)
0263 break;
0264 val >>= 1;
0265 }
0266 return (u8) i;
0267 }
0268
0269 struct w83791d_data {
0270 struct device *hwmon_dev;
0271 struct mutex update_lock;
0272
0273 bool valid;
0274 unsigned long last_updated;
0275
0276
0277 u8 in[NUMBER_OF_VIN];
0278 u8 in_max[NUMBER_OF_VIN];
0279 u8 in_min[NUMBER_OF_VIN];
0280
0281
0282 u8 fan[NUMBER_OF_FANIN];
0283 u8 fan_min[NUMBER_OF_FANIN];
0284 u8 fan_div[NUMBER_OF_FANIN];
0285
0286
0287
0288 s8 temp1[3];
0289 s16 temp_add[2][3];
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299 u8 pwm[5];
0300 u8 pwm_enable[3];
0301
0302
0303
0304 u8 temp_target[3];
0305 u8 temp_tolerance[3];
0306
0307
0308 u32 alarms;
0309 u8 beep_enable;
0310 u32 beep_mask;
0311 u8 vid;
0312 u8 vrm;
0313 };
0314
0315 static int w83791d_probe(struct i2c_client *client);
0316 static int w83791d_detect(struct i2c_client *client,
0317 struct i2c_board_info *info);
0318 static int w83791d_remove(struct i2c_client *client);
0319
0320 static int w83791d_read(struct i2c_client *client, u8 reg);
0321 static int w83791d_write(struct i2c_client *client, u8 reg, u8 value);
0322 static struct w83791d_data *w83791d_update_device(struct device *dev);
0323
0324 #ifdef DEBUG
0325 static void w83791d_print_debug(struct w83791d_data *data, struct device *dev);
0326 #endif
0327
0328 static void w83791d_init_client(struct i2c_client *client);
0329
0330 static const struct i2c_device_id w83791d_id[] = {
0331 { "w83791d", 0 },
0332 { }
0333 };
0334 MODULE_DEVICE_TABLE(i2c, w83791d_id);
0335
0336 static struct i2c_driver w83791d_driver = {
0337 .class = I2C_CLASS_HWMON,
0338 .driver = {
0339 .name = "w83791d",
0340 },
0341 .probe_new = w83791d_probe,
0342 .remove = w83791d_remove,
0343 .id_table = w83791d_id,
0344 .detect = w83791d_detect,
0345 .address_list = normal_i2c,
0346 };
0347
0348
0349 #define show_in_reg(reg) \
0350 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
0351 char *buf) \
0352 { \
0353 struct sensor_device_attribute *sensor_attr = \
0354 to_sensor_dev_attr(attr); \
0355 struct w83791d_data *data = w83791d_update_device(dev); \
0356 int nr = sensor_attr->index; \
0357 return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
0358 }
0359
0360 show_in_reg(in);
0361 show_in_reg(in_min);
0362 show_in_reg(in_max);
0363
0364 #define store_in_reg(REG, reg) \
0365 static ssize_t store_in_##reg(struct device *dev, \
0366 struct device_attribute *attr, \
0367 const char *buf, size_t count) \
0368 { \
0369 struct sensor_device_attribute *sensor_attr = \
0370 to_sensor_dev_attr(attr); \
0371 struct i2c_client *client = to_i2c_client(dev); \
0372 struct w83791d_data *data = i2c_get_clientdata(client); \
0373 int nr = sensor_attr->index; \
0374 unsigned long val; \
0375 int err = kstrtoul(buf, 10, &val); \
0376 if (err) \
0377 return err; \
0378 mutex_lock(&data->update_lock); \
0379 data->in_##reg[nr] = IN_TO_REG(val); \
0380 w83791d_write(client, W83791D_REG_IN_##REG[nr], data->in_##reg[nr]); \
0381 mutex_unlock(&data->update_lock); \
0382 \
0383 return count; \
0384 }
0385 store_in_reg(MIN, min);
0386 store_in_reg(MAX, max);
0387
0388 static struct sensor_device_attribute sda_in_input[] = {
0389 SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
0390 SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
0391 SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
0392 SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
0393 SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
0394 SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
0395 SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
0396 SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
0397 SENSOR_ATTR(in8_input, S_IRUGO, show_in, NULL, 8),
0398 SENSOR_ATTR(in9_input, S_IRUGO, show_in, NULL, 9),
0399 };
0400
0401 static struct sensor_device_attribute sda_in_min[] = {
0402 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0),
0403 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1),
0404 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2),
0405 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 3),
0406 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 4),
0407 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 5),
0408 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 6),
0409 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 7),
0410 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 8),
0411 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 9),
0412 };
0413
0414 static struct sensor_device_attribute sda_in_max[] = {
0415 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0),
0416 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1),
0417 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2),
0418 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 3),
0419 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 4),
0420 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 5),
0421 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 6),
0422 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 7),
0423 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 8),
0424 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 9),
0425 };
0426
0427
0428 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
0429 char *buf)
0430 {
0431 struct sensor_device_attribute *sensor_attr =
0432 to_sensor_dev_attr(attr);
0433 struct w83791d_data *data = w83791d_update_device(dev);
0434 int bitnr = sensor_attr->index;
0435
0436 return sprintf(buf, "%d\n", (data->beep_mask >> bitnr) & 1);
0437 }
0438
0439 static ssize_t store_beep(struct device *dev, struct device_attribute *attr,
0440 const char *buf, size_t count)
0441 {
0442 struct sensor_device_attribute *sensor_attr =
0443 to_sensor_dev_attr(attr);
0444 struct i2c_client *client = to_i2c_client(dev);
0445 struct w83791d_data *data = i2c_get_clientdata(client);
0446 int bitnr = sensor_attr->index;
0447 int bytenr = bitnr / 8;
0448 unsigned long val;
0449 int err;
0450
0451 err = kstrtoul(buf, 10, &val);
0452 if (err)
0453 return err;
0454
0455 val = val ? 1 : 0;
0456
0457 mutex_lock(&data->update_lock);
0458
0459 data->beep_mask &= ~(0xff << (bytenr * 8));
0460 data->beep_mask |= w83791d_read(client, W83791D_REG_BEEP_CTRL[bytenr])
0461 << (bytenr * 8);
0462
0463 data->beep_mask &= ~(1 << bitnr);
0464 data->beep_mask |= val << bitnr;
0465
0466 w83791d_write(client, W83791D_REG_BEEP_CTRL[bytenr],
0467 (data->beep_mask >> (bytenr * 8)) & 0xff);
0468
0469 mutex_unlock(&data->update_lock);
0470
0471 return count;
0472 }
0473
0474 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
0475 char *buf)
0476 {
0477 struct sensor_device_attribute *sensor_attr =
0478 to_sensor_dev_attr(attr);
0479 struct w83791d_data *data = w83791d_update_device(dev);
0480 int bitnr = sensor_attr->index;
0481
0482 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
0483 }
0484
0485
0486
0487
0488
0489 static struct sensor_device_attribute sda_in_beep[] = {
0490 SENSOR_ATTR(in0_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 0),
0491 SENSOR_ATTR(in1_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 13),
0492 SENSOR_ATTR(in2_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 2),
0493 SENSOR_ATTR(in3_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 3),
0494 SENSOR_ATTR(in4_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 8),
0495 SENSOR_ATTR(in5_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 9),
0496 SENSOR_ATTR(in6_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 10),
0497 SENSOR_ATTR(in7_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 16),
0498 SENSOR_ATTR(in8_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 17),
0499 SENSOR_ATTR(in9_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 14),
0500 };
0501
0502 static struct sensor_device_attribute sda_in_alarm[] = {
0503 SENSOR_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0),
0504 SENSOR_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1),
0505 SENSOR_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2),
0506 SENSOR_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3),
0507 SENSOR_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8),
0508 SENSOR_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9),
0509 SENSOR_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10),
0510 SENSOR_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 19),
0511 SENSOR_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 20),
0512 SENSOR_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 14),
0513 };
0514
0515 #define show_fan_reg(reg) \
0516 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
0517 char *buf) \
0518 { \
0519 struct sensor_device_attribute *sensor_attr = \
0520 to_sensor_dev_attr(attr); \
0521 struct w83791d_data *data = w83791d_update_device(dev); \
0522 int nr = sensor_attr->index; \
0523 return sprintf(buf, "%d\n", \
0524 FAN_FROM_REG(data->reg[nr], DIV_FROM_REG(data->fan_div[nr]))); \
0525 }
0526
0527 show_fan_reg(fan);
0528 show_fan_reg(fan_min);
0529
0530 static ssize_t store_fan_min(struct device *dev, struct device_attribute *attr,
0531 const char *buf, size_t count)
0532 {
0533 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
0534 struct i2c_client *client = to_i2c_client(dev);
0535 struct w83791d_data *data = i2c_get_clientdata(client);
0536 int nr = sensor_attr->index;
0537 unsigned long val;
0538 int err;
0539
0540 err = kstrtoul(buf, 10, &val);
0541 if (err)
0542 return err;
0543
0544 mutex_lock(&data->update_lock);
0545 data->fan_min[nr] = fan_to_reg(val, DIV_FROM_REG(data->fan_div[nr]));
0546 w83791d_write(client, W83791D_REG_FAN_MIN[nr], data->fan_min[nr]);
0547 mutex_unlock(&data->update_lock);
0548
0549 return count;
0550 }
0551
0552 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
0553 char *buf)
0554 {
0555 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
0556 int nr = sensor_attr->index;
0557 struct w83791d_data *data = w83791d_update_device(dev);
0558 return sprintf(buf, "%u\n", DIV_FROM_REG(data->fan_div[nr]));
0559 }
0560
0561
0562
0563
0564
0565
0566
0567 static ssize_t store_fan_div(struct device *dev, struct device_attribute *attr,
0568 const char *buf, size_t count)
0569 {
0570 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
0571 struct i2c_client *client = to_i2c_client(dev);
0572 struct w83791d_data *data = i2c_get_clientdata(client);
0573 int nr = sensor_attr->index;
0574 unsigned long min;
0575 u8 tmp_fan_div;
0576 u8 fan_div_reg;
0577 u8 vbat_reg;
0578 int indx = 0;
0579 u8 keep_mask = 0;
0580 u8 new_shift = 0;
0581 unsigned long val;
0582 int err;
0583
0584 err = kstrtoul(buf, 10, &val);
0585 if (err)
0586 return err;
0587
0588
0589 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
0590
0591 mutex_lock(&data->update_lock);
0592 data->fan_div[nr] = div_to_reg(nr, val);
0593
0594 switch (nr) {
0595 case 0:
0596 indx = 0;
0597 keep_mask = 0xcf;
0598 new_shift = 4;
0599 break;
0600 case 1:
0601 indx = 0;
0602 keep_mask = 0x3f;
0603 new_shift = 6;
0604 break;
0605 case 2:
0606 indx = 1;
0607 keep_mask = 0x3f;
0608 new_shift = 6;
0609 break;
0610 case 3:
0611 indx = 2;
0612 keep_mask = 0xf8;
0613 new_shift = 0;
0614 break;
0615 case 4:
0616 indx = 2;
0617 keep_mask = 0x8f;
0618 new_shift = 4;
0619 break;
0620 #ifdef DEBUG
0621 default:
0622 dev_warn(dev, "store_fan_div: Unexpected nr seen: %d\n", nr);
0623 count = -EINVAL;
0624 goto err_exit;
0625 #endif
0626 }
0627
0628 fan_div_reg = w83791d_read(client, W83791D_REG_FAN_DIV[indx])
0629 & keep_mask;
0630 tmp_fan_div = (data->fan_div[nr] << new_shift) & ~keep_mask;
0631
0632 w83791d_write(client, W83791D_REG_FAN_DIV[indx],
0633 fan_div_reg | tmp_fan_div);
0634
0635
0636 if (nr < 3) {
0637 keep_mask = ~(1 << (nr + 5));
0638 vbat_reg = w83791d_read(client, W83791D_REG_VBAT)
0639 & keep_mask;
0640 tmp_fan_div = (data->fan_div[nr] << (3 + nr)) & ~keep_mask;
0641 w83791d_write(client, W83791D_REG_VBAT,
0642 vbat_reg | tmp_fan_div);
0643 }
0644
0645
0646 data->fan_min[nr] = fan_to_reg(min, DIV_FROM_REG(data->fan_div[nr]));
0647 w83791d_write(client, W83791D_REG_FAN_MIN[nr], data->fan_min[nr]);
0648
0649 #ifdef DEBUG
0650 err_exit:
0651 #endif
0652 mutex_unlock(&data->update_lock);
0653
0654 return count;
0655 }
0656
0657 static struct sensor_device_attribute sda_fan_input[] = {
0658 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
0659 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
0660 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
0661 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
0662 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
0663 };
0664
0665 static struct sensor_device_attribute sda_fan_min[] = {
0666 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO,
0667 show_fan_min, store_fan_min, 0),
0668 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO,
0669 show_fan_min, store_fan_min, 1),
0670 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO,
0671 show_fan_min, store_fan_min, 2),
0672 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO,
0673 show_fan_min, store_fan_min, 3),
0674 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO,
0675 show_fan_min, store_fan_min, 4),
0676 };
0677
0678 static struct sensor_device_attribute sda_fan_div[] = {
0679 SENSOR_ATTR(fan1_div, S_IWUSR | S_IRUGO,
0680 show_fan_div, store_fan_div, 0),
0681 SENSOR_ATTR(fan2_div, S_IWUSR | S_IRUGO,
0682 show_fan_div, store_fan_div, 1),
0683 SENSOR_ATTR(fan3_div, S_IWUSR | S_IRUGO,
0684 show_fan_div, store_fan_div, 2),
0685 SENSOR_ATTR(fan4_div, S_IWUSR | S_IRUGO,
0686 show_fan_div, store_fan_div, 3),
0687 SENSOR_ATTR(fan5_div, S_IWUSR | S_IRUGO,
0688 show_fan_div, store_fan_div, 4),
0689 };
0690
0691 static struct sensor_device_attribute sda_fan_beep[] = {
0692 SENSOR_ATTR(fan1_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 6),
0693 SENSOR_ATTR(fan2_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 7),
0694 SENSOR_ATTR(fan3_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 11),
0695 SENSOR_ATTR(fan4_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 21),
0696 SENSOR_ATTR(fan5_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 22),
0697 };
0698
0699 static struct sensor_device_attribute sda_fan_alarm[] = {
0700 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6),
0701 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7),
0702 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11),
0703 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 21),
0704 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 22),
0705 };
0706
0707
0708 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
0709 char *buf)
0710 {
0711 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
0712 int nr = sensor_attr->index;
0713 struct w83791d_data *data = w83791d_update_device(dev);
0714 return sprintf(buf, "%u\n", data->pwm[nr]);
0715 }
0716
0717 static ssize_t store_pwm(struct device *dev, struct device_attribute *attr,
0718 const char *buf, size_t count)
0719 {
0720 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
0721 struct i2c_client *client = to_i2c_client(dev);
0722 struct w83791d_data *data = i2c_get_clientdata(client);
0723 int nr = sensor_attr->index;
0724 unsigned long val;
0725
0726 if (kstrtoul(buf, 10, &val))
0727 return -EINVAL;
0728
0729 mutex_lock(&data->update_lock);
0730 data->pwm[nr] = clamp_val(val, 0, 255);
0731 w83791d_write(client, W83791D_REG_PWM[nr], data->pwm[nr]);
0732 mutex_unlock(&data->update_lock);
0733 return count;
0734 }
0735
0736 static struct sensor_device_attribute sda_pwm[] = {
0737 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO,
0738 show_pwm, store_pwm, 0),
0739 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO,
0740 show_pwm, store_pwm, 1),
0741 SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO,
0742 show_pwm, store_pwm, 2),
0743 SENSOR_ATTR(pwm4, S_IWUSR | S_IRUGO,
0744 show_pwm, store_pwm, 3),
0745 SENSOR_ATTR(pwm5, S_IWUSR | S_IRUGO,
0746 show_pwm, store_pwm, 4),
0747 };
0748
0749 static ssize_t show_pwmenable(struct device *dev, struct device_attribute *attr,
0750 char *buf)
0751 {
0752 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
0753 int nr = sensor_attr->index;
0754 struct w83791d_data *data = w83791d_update_device(dev);
0755 return sprintf(buf, "%u\n", data->pwm_enable[nr] + 1);
0756 }
0757
0758 static ssize_t store_pwmenable(struct device *dev,
0759 struct device_attribute *attr, const char *buf, size_t count)
0760 {
0761 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
0762 struct i2c_client *client = to_i2c_client(dev);
0763 struct w83791d_data *data = i2c_get_clientdata(client);
0764 int nr = sensor_attr->index;
0765 unsigned long val;
0766 u8 reg_cfg_tmp;
0767 u8 reg_idx = 0;
0768 u8 val_shift = 0;
0769 u8 keep_mask = 0;
0770
0771 int ret = kstrtoul(buf, 10, &val);
0772
0773 if (ret || val < 1 || val > 3)
0774 return -EINVAL;
0775
0776 mutex_lock(&data->update_lock);
0777 data->pwm_enable[nr] = val - 1;
0778 switch (nr) {
0779 case 0:
0780 reg_idx = 0;
0781 val_shift = 2;
0782 keep_mask = 0xf3;
0783 break;
0784 case 1:
0785 reg_idx = 0;
0786 val_shift = 4;
0787 keep_mask = 0xcf;
0788 break;
0789 case 2:
0790 reg_idx = 1;
0791 val_shift = 2;
0792 keep_mask = 0xf3;
0793 break;
0794 }
0795
0796 reg_cfg_tmp = w83791d_read(client, W83791D_REG_FAN_CFG[reg_idx]);
0797 reg_cfg_tmp = (reg_cfg_tmp & keep_mask) |
0798 data->pwm_enable[nr] << val_shift;
0799
0800 w83791d_write(client, W83791D_REG_FAN_CFG[reg_idx], reg_cfg_tmp);
0801 mutex_unlock(&data->update_lock);
0802
0803 return count;
0804 }
0805 static struct sensor_device_attribute sda_pwmenable[] = {
0806 SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
0807 show_pwmenable, store_pwmenable, 0),
0808 SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO,
0809 show_pwmenable, store_pwmenable, 1),
0810 SENSOR_ATTR(pwm3_enable, S_IWUSR | S_IRUGO,
0811 show_pwmenable, store_pwmenable, 2),
0812 };
0813
0814
0815 static ssize_t show_temp_target(struct device *dev,
0816 struct device_attribute *attr, char *buf)
0817 {
0818 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
0819 struct w83791d_data *data = w83791d_update_device(dev);
0820 int nr = sensor_attr->index;
0821 return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp_target[nr]));
0822 }
0823
0824 static ssize_t store_temp_target(struct device *dev,
0825 struct device_attribute *attr, const char *buf, size_t count)
0826 {
0827 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
0828 struct i2c_client *client = to_i2c_client(dev);
0829 struct w83791d_data *data = i2c_get_clientdata(client);
0830 int nr = sensor_attr->index;
0831 long val;
0832 u8 target_mask;
0833
0834 if (kstrtol(buf, 10, &val))
0835 return -EINVAL;
0836
0837 mutex_lock(&data->update_lock);
0838 data->temp_target[nr] = TARGET_TEMP_TO_REG(val);
0839 target_mask = w83791d_read(client,
0840 W83791D_REG_TEMP_TARGET[nr]) & 0x80;
0841 w83791d_write(client, W83791D_REG_TEMP_TARGET[nr],
0842 data->temp_target[nr] | target_mask);
0843 mutex_unlock(&data->update_lock);
0844 return count;
0845 }
0846
0847 static struct sensor_device_attribute sda_temp_target[] = {
0848 SENSOR_ATTR(temp1_target, S_IWUSR | S_IRUGO,
0849 show_temp_target, store_temp_target, 0),
0850 SENSOR_ATTR(temp2_target, S_IWUSR | S_IRUGO,
0851 show_temp_target, store_temp_target, 1),
0852 SENSOR_ATTR(temp3_target, S_IWUSR | S_IRUGO,
0853 show_temp_target, store_temp_target, 2),
0854 };
0855
0856 static ssize_t show_temp_tolerance(struct device *dev,
0857 struct device_attribute *attr, char *buf)
0858 {
0859 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
0860 struct w83791d_data *data = w83791d_update_device(dev);
0861 int nr = sensor_attr->index;
0862 return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp_tolerance[nr]));
0863 }
0864
0865 static ssize_t store_temp_tolerance(struct device *dev,
0866 struct device_attribute *attr, const char *buf, size_t count)
0867 {
0868 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
0869 struct i2c_client *client = to_i2c_client(dev);
0870 struct w83791d_data *data = i2c_get_clientdata(client);
0871 int nr = sensor_attr->index;
0872 unsigned long val;
0873 u8 target_mask;
0874 u8 reg_idx = 0;
0875 u8 val_shift = 0;
0876 u8 keep_mask = 0;
0877
0878 if (kstrtoul(buf, 10, &val))
0879 return -EINVAL;
0880
0881 switch (nr) {
0882 case 0:
0883 reg_idx = 0;
0884 val_shift = 0;
0885 keep_mask = 0xf0;
0886 break;
0887 case 1:
0888 reg_idx = 0;
0889 val_shift = 4;
0890 keep_mask = 0x0f;
0891 break;
0892 case 2:
0893 reg_idx = 1;
0894 val_shift = 0;
0895 keep_mask = 0xf0;
0896 break;
0897 }
0898
0899 mutex_lock(&data->update_lock);
0900 data->temp_tolerance[nr] = TOL_TEMP_TO_REG(val);
0901 target_mask = w83791d_read(client,
0902 W83791D_REG_TEMP_TOL[reg_idx]) & keep_mask;
0903 w83791d_write(client, W83791D_REG_TEMP_TOL[reg_idx],
0904 (data->temp_tolerance[nr] << val_shift) | target_mask);
0905 mutex_unlock(&data->update_lock);
0906 return count;
0907 }
0908
0909 static struct sensor_device_attribute sda_temp_tolerance[] = {
0910 SENSOR_ATTR(temp1_tolerance, S_IWUSR | S_IRUGO,
0911 show_temp_tolerance, store_temp_tolerance, 0),
0912 SENSOR_ATTR(temp2_tolerance, S_IWUSR | S_IRUGO,
0913 show_temp_tolerance, store_temp_tolerance, 1),
0914 SENSOR_ATTR(temp3_tolerance, S_IWUSR | S_IRUGO,
0915 show_temp_tolerance, store_temp_tolerance, 2),
0916 };
0917
0918
0919 static ssize_t show_temp1(struct device *dev, struct device_attribute *devattr,
0920 char *buf)
0921 {
0922 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0923 struct w83791d_data *data = w83791d_update_device(dev);
0924 return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp1[attr->index]));
0925 }
0926
0927 static ssize_t store_temp1(struct device *dev, struct device_attribute *devattr,
0928 const char *buf, size_t count)
0929 {
0930 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0931 struct i2c_client *client = to_i2c_client(dev);
0932 struct w83791d_data *data = i2c_get_clientdata(client);
0933 int nr = attr->index;
0934 long val;
0935 int err;
0936
0937 err = kstrtol(buf, 10, &val);
0938 if (err)
0939 return err;
0940
0941 mutex_lock(&data->update_lock);
0942 data->temp1[nr] = TEMP1_TO_REG(val);
0943 w83791d_write(client, W83791D_REG_TEMP1[nr], data->temp1[nr]);
0944 mutex_unlock(&data->update_lock);
0945 return count;
0946 }
0947
0948
0949 static ssize_t show_temp23(struct device *dev, struct device_attribute *devattr,
0950 char *buf)
0951 {
0952 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
0953 struct w83791d_data *data = w83791d_update_device(dev);
0954 int nr = attr->nr;
0955 int index = attr->index;
0956 return sprintf(buf, "%d\n", TEMP23_FROM_REG(data->temp_add[nr][index]));
0957 }
0958
0959 static ssize_t store_temp23(struct device *dev,
0960 struct device_attribute *devattr,
0961 const char *buf, size_t count)
0962 {
0963 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
0964 struct i2c_client *client = to_i2c_client(dev);
0965 struct w83791d_data *data = i2c_get_clientdata(client);
0966 long val;
0967 int err;
0968 int nr = attr->nr;
0969 int index = attr->index;
0970
0971 err = kstrtol(buf, 10, &val);
0972 if (err)
0973 return err;
0974
0975 mutex_lock(&data->update_lock);
0976 data->temp_add[nr][index] = TEMP23_TO_REG(val);
0977 w83791d_write(client, W83791D_REG_TEMP_ADD[nr][index * 2],
0978 data->temp_add[nr][index] >> 8);
0979 w83791d_write(client, W83791D_REG_TEMP_ADD[nr][index * 2 + 1],
0980 data->temp_add[nr][index] & 0x80);
0981 mutex_unlock(&data->update_lock);
0982
0983 return count;
0984 }
0985
0986 static struct sensor_device_attribute_2 sda_temp_input[] = {
0987 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp1, NULL, 0, 0),
0988 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp23, NULL, 0, 0),
0989 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp23, NULL, 1, 0),
0990 };
0991
0992 static struct sensor_device_attribute_2 sda_temp_max[] = {
0993 SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR,
0994 show_temp1, store_temp1, 0, 1),
0995 SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR,
0996 show_temp23, store_temp23, 0, 1),
0997 SENSOR_ATTR_2(temp3_max, S_IRUGO | S_IWUSR,
0998 show_temp23, store_temp23, 1, 1),
0999 };
1000
1001 static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
1002 SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR,
1003 show_temp1, store_temp1, 0, 2),
1004 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR,
1005 show_temp23, store_temp23, 0, 2),
1006 SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR,
1007 show_temp23, store_temp23, 1, 2),
1008 };
1009
1010
1011
1012
1013
1014 static struct sensor_device_attribute sda_temp_beep[] = {
1015 SENSOR_ATTR(temp1_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 4),
1016 SENSOR_ATTR(temp2_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 5),
1017 SENSOR_ATTR(temp3_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 1),
1018 };
1019
1020 static struct sensor_device_attribute sda_temp_alarm[] = {
1021 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4),
1022 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5),
1023 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13),
1024 };
1025
1026
1027 static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
1028 char *buf)
1029 {
1030 struct w83791d_data *data = w83791d_update_device(dev);
1031 return sprintf(buf, "%u\n", data->alarms);
1032 }
1033
1034 static DEVICE_ATTR_RO(alarms);
1035
1036
1037
1038 #define GLOBAL_BEEP_ENABLE_SHIFT 15
1039 #define GLOBAL_BEEP_ENABLE_MASK (1 << GLOBAL_BEEP_ENABLE_SHIFT)
1040
1041 static ssize_t show_beep_enable(struct device *dev,
1042 struct device_attribute *attr, char *buf)
1043 {
1044 struct w83791d_data *data = w83791d_update_device(dev);
1045 return sprintf(buf, "%d\n", data->beep_enable);
1046 }
1047
1048 static ssize_t show_beep_mask(struct device *dev,
1049 struct device_attribute *attr, char *buf)
1050 {
1051 struct w83791d_data *data = w83791d_update_device(dev);
1052 return sprintf(buf, "%d\n", BEEP_MASK_FROM_REG(data->beep_mask));
1053 }
1054
1055
1056 static ssize_t store_beep_mask(struct device *dev,
1057 struct device_attribute *attr,
1058 const char *buf, size_t count)
1059 {
1060 struct i2c_client *client = to_i2c_client(dev);
1061 struct w83791d_data *data = i2c_get_clientdata(client);
1062 int i;
1063 long val;
1064 int err;
1065
1066 err = kstrtol(buf, 10, &val);
1067 if (err)
1068 return err;
1069
1070 mutex_lock(&data->update_lock);
1071
1072
1073
1074
1075
1076 data->beep_mask = BEEP_MASK_TO_REG(val) & ~GLOBAL_BEEP_ENABLE_MASK;
1077 data->beep_mask |= (data->beep_enable << GLOBAL_BEEP_ENABLE_SHIFT);
1078
1079 val = data->beep_mask;
1080
1081 for (i = 0; i < 3; i++) {
1082 w83791d_write(client, W83791D_REG_BEEP_CTRL[i], (val & 0xff));
1083 val >>= 8;
1084 }
1085
1086 mutex_unlock(&data->update_lock);
1087
1088 return count;
1089 }
1090
1091 static ssize_t store_beep_enable(struct device *dev,
1092 struct device_attribute *attr,
1093 const char *buf, size_t count)
1094 {
1095 struct i2c_client *client = to_i2c_client(dev);
1096 struct w83791d_data *data = i2c_get_clientdata(client);
1097 long val;
1098 int err;
1099
1100 err = kstrtol(buf, 10, &val);
1101 if (err)
1102 return err;
1103
1104 mutex_lock(&data->update_lock);
1105
1106 data->beep_enable = val ? 1 : 0;
1107
1108
1109 data->beep_mask &= ~GLOBAL_BEEP_ENABLE_MASK;
1110 data->beep_mask |= (data->beep_enable << GLOBAL_BEEP_ENABLE_SHIFT);
1111
1112
1113
1114
1115
1116 val = (data->beep_mask >> 8) & 0xff;
1117
1118 w83791d_write(client, W83791D_REG_BEEP_CTRL[1], val);
1119
1120 mutex_unlock(&data->update_lock);
1121
1122 return count;
1123 }
1124
1125 static struct sensor_device_attribute sda_beep_ctrl[] = {
1126 SENSOR_ATTR(beep_enable, S_IRUGO | S_IWUSR,
1127 show_beep_enable, store_beep_enable, 0),
1128 SENSOR_ATTR(beep_mask, S_IRUGO | S_IWUSR,
1129 show_beep_mask, store_beep_mask, 1)
1130 };
1131
1132
1133 static ssize_t cpu0_vid_show(struct device *dev,
1134 struct device_attribute *attr, char *buf)
1135 {
1136 struct w83791d_data *data = w83791d_update_device(dev);
1137 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1138 }
1139
1140 static DEVICE_ATTR_RO(cpu0_vid);
1141
1142 static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
1143 char *buf)
1144 {
1145 struct w83791d_data *data = dev_get_drvdata(dev);
1146 return sprintf(buf, "%d\n", data->vrm);
1147 }
1148
1149 static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
1150 const char *buf, size_t count)
1151 {
1152 struct w83791d_data *data = dev_get_drvdata(dev);
1153 unsigned long val;
1154 int err;
1155
1156
1157
1158
1159
1160
1161
1162 err = kstrtoul(buf, 10, &val);
1163 if (err)
1164 return err;
1165
1166 if (val > 255)
1167 return -EINVAL;
1168
1169 data->vrm = val;
1170 return count;
1171 }
1172
1173 static DEVICE_ATTR_RW(vrm);
1174
1175 #define IN_UNIT_ATTRS(X) \
1176 &sda_in_input[X].dev_attr.attr, \
1177 &sda_in_min[X].dev_attr.attr, \
1178 &sda_in_max[X].dev_attr.attr, \
1179 &sda_in_beep[X].dev_attr.attr, \
1180 &sda_in_alarm[X].dev_attr.attr
1181
1182 #define FAN_UNIT_ATTRS(X) \
1183 &sda_fan_input[X].dev_attr.attr, \
1184 &sda_fan_min[X].dev_attr.attr, \
1185 &sda_fan_div[X].dev_attr.attr, \
1186 &sda_fan_beep[X].dev_attr.attr, \
1187 &sda_fan_alarm[X].dev_attr.attr
1188
1189 #define TEMP_UNIT_ATTRS(X) \
1190 &sda_temp_input[X].dev_attr.attr, \
1191 &sda_temp_max[X].dev_attr.attr, \
1192 &sda_temp_max_hyst[X].dev_attr.attr, \
1193 &sda_temp_beep[X].dev_attr.attr, \
1194 &sda_temp_alarm[X].dev_attr.attr
1195
1196 static struct attribute *w83791d_attributes[] = {
1197 IN_UNIT_ATTRS(0),
1198 IN_UNIT_ATTRS(1),
1199 IN_UNIT_ATTRS(2),
1200 IN_UNIT_ATTRS(3),
1201 IN_UNIT_ATTRS(4),
1202 IN_UNIT_ATTRS(5),
1203 IN_UNIT_ATTRS(6),
1204 IN_UNIT_ATTRS(7),
1205 IN_UNIT_ATTRS(8),
1206 IN_UNIT_ATTRS(9),
1207 FAN_UNIT_ATTRS(0),
1208 FAN_UNIT_ATTRS(1),
1209 FAN_UNIT_ATTRS(2),
1210 TEMP_UNIT_ATTRS(0),
1211 TEMP_UNIT_ATTRS(1),
1212 TEMP_UNIT_ATTRS(2),
1213 &dev_attr_alarms.attr,
1214 &sda_beep_ctrl[0].dev_attr.attr,
1215 &sda_beep_ctrl[1].dev_attr.attr,
1216 &dev_attr_cpu0_vid.attr,
1217 &dev_attr_vrm.attr,
1218 &sda_pwm[0].dev_attr.attr,
1219 &sda_pwm[1].dev_attr.attr,
1220 &sda_pwm[2].dev_attr.attr,
1221 &sda_pwmenable[0].dev_attr.attr,
1222 &sda_pwmenable[1].dev_attr.attr,
1223 &sda_pwmenable[2].dev_attr.attr,
1224 &sda_temp_target[0].dev_attr.attr,
1225 &sda_temp_target[1].dev_attr.attr,
1226 &sda_temp_target[2].dev_attr.attr,
1227 &sda_temp_tolerance[0].dev_attr.attr,
1228 &sda_temp_tolerance[1].dev_attr.attr,
1229 &sda_temp_tolerance[2].dev_attr.attr,
1230 NULL
1231 };
1232
1233 static const struct attribute_group w83791d_group = {
1234 .attrs = w83791d_attributes,
1235 };
1236
1237
1238
1239
1240
1241
1242 static struct attribute *w83791d_attributes_fanpwm45[] = {
1243 FAN_UNIT_ATTRS(3),
1244 FAN_UNIT_ATTRS(4),
1245 &sda_pwm[3].dev_attr.attr,
1246 &sda_pwm[4].dev_attr.attr,
1247 NULL
1248 };
1249
1250 static const struct attribute_group w83791d_group_fanpwm45 = {
1251 .attrs = w83791d_attributes_fanpwm45,
1252 };
1253
1254 static int w83791d_detect_subclients(struct i2c_client *client)
1255 {
1256 struct i2c_adapter *adapter = client->adapter;
1257 int address = client->addr;
1258 int i, id;
1259 u8 val;
1260
1261 id = i2c_adapter_id(adapter);
1262 if (force_subclients[0] == id && force_subclients[1] == address) {
1263 for (i = 2; i <= 3; i++) {
1264 if (force_subclients[i] < 0x48 ||
1265 force_subclients[i] > 0x4f) {
1266 dev_err(&client->dev,
1267 "invalid subclient "
1268 "address %d; must be 0x48-0x4f\n",
1269 force_subclients[i]);
1270 return -ENODEV;
1271 }
1272 }
1273 w83791d_write(client, W83791D_REG_I2C_SUBADDR,
1274 (force_subclients[2] & 0x07) |
1275 ((force_subclients[3] & 0x07) << 4));
1276 }
1277
1278 val = w83791d_read(client, W83791D_REG_I2C_SUBADDR);
1279
1280 if (!(val & 0x88) && (val & 0x7) == ((val >> 4) & 0x7)) {
1281 dev_err(&client->dev,
1282 "duplicate addresses 0x%x, use force_subclient\n", 0x48 + (val & 0x7));
1283 return -ENODEV;
1284 }
1285
1286 if (!(val & 0x08))
1287 devm_i2c_new_dummy_device(&client->dev, adapter, 0x48 + (val & 0x7));
1288
1289 if (!(val & 0x80))
1290 devm_i2c_new_dummy_device(&client->dev, adapter, 0x48 + ((val >> 4) & 0x7));
1291
1292 return 0;
1293 }
1294
1295
1296
1297 static int w83791d_detect(struct i2c_client *client,
1298 struct i2c_board_info *info)
1299 {
1300 struct i2c_adapter *adapter = client->adapter;
1301 int val1, val2;
1302 unsigned short address = client->addr;
1303
1304 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1305 return -ENODEV;
1306
1307 if (w83791d_read(client, W83791D_REG_CONFIG) & 0x80)
1308 return -ENODEV;
1309
1310 val1 = w83791d_read(client, W83791D_REG_BANK);
1311 val2 = w83791d_read(client, W83791D_REG_CHIPMAN);
1312
1313 if (!(val1 & 0x07)) {
1314 if ((!(val1 & 0x80) && val2 != 0xa3) ||
1315 ((val1 & 0x80) && val2 != 0x5c)) {
1316 return -ENODEV;
1317 }
1318 }
1319
1320
1321
1322
1323 if (w83791d_read(client, W83791D_REG_I2C_ADDR) != address)
1324 return -ENODEV;
1325
1326
1327 val1 = w83791d_read(client, W83791D_REG_BANK) & 0x78;
1328 w83791d_write(client, W83791D_REG_BANK, val1 | 0x80);
1329
1330
1331 val1 = w83791d_read(client, W83791D_REG_WCHIPID);
1332 val2 = w83791d_read(client, W83791D_REG_CHIPMAN);
1333 if (val1 != 0x71 || val2 != 0x5c)
1334 return -ENODEV;
1335
1336 strlcpy(info->type, "w83791d", I2C_NAME_SIZE);
1337
1338 return 0;
1339 }
1340
1341 static int w83791d_probe(struct i2c_client *client)
1342 {
1343 struct w83791d_data *data;
1344 struct device *dev = &client->dev;
1345 int i, err;
1346 u8 has_fanpwm45;
1347
1348 #ifdef DEBUG
1349 int val1;
1350 val1 = w83791d_read(client, W83791D_REG_DID_VID4);
1351 dev_dbg(dev, "Device ID version: %d.%d (0x%02x)\n",
1352 (val1 >> 5) & 0x07, (val1 >> 1) & 0x0f, val1);
1353 #endif
1354
1355 data = devm_kzalloc(&client->dev, sizeof(struct w83791d_data),
1356 GFP_KERNEL);
1357 if (!data)
1358 return -ENOMEM;
1359
1360 i2c_set_clientdata(client, data);
1361 mutex_init(&data->update_lock);
1362
1363 err = w83791d_detect_subclients(client);
1364 if (err)
1365 return err;
1366
1367
1368 w83791d_init_client(client);
1369
1370
1371
1372
1373
1374 for (i = 0; i < NUMBER_OF_FANIN; i++)
1375 data->fan_min[i] = w83791d_read(client, W83791D_REG_FAN_MIN[i]);
1376
1377
1378 err = sysfs_create_group(&client->dev.kobj, &w83791d_group);
1379 if (err)
1380 return err;
1381
1382
1383 has_fanpwm45 = w83791d_read(client, W83791D_REG_GPIO) & 0x10;
1384 if (has_fanpwm45) {
1385 err = sysfs_create_group(&client->dev.kobj,
1386 &w83791d_group_fanpwm45);
1387 if (err)
1388 goto error4;
1389 }
1390
1391
1392 data->hwmon_dev = hwmon_device_register(dev);
1393 if (IS_ERR(data->hwmon_dev)) {
1394 err = PTR_ERR(data->hwmon_dev);
1395 goto error5;
1396 }
1397
1398 return 0;
1399
1400 error5:
1401 if (has_fanpwm45)
1402 sysfs_remove_group(&client->dev.kobj, &w83791d_group_fanpwm45);
1403 error4:
1404 sysfs_remove_group(&client->dev.kobj, &w83791d_group);
1405 return err;
1406 }
1407
1408 static int w83791d_remove(struct i2c_client *client)
1409 {
1410 struct w83791d_data *data = i2c_get_clientdata(client);
1411
1412 hwmon_device_unregister(data->hwmon_dev);
1413 sysfs_remove_group(&client->dev.kobj, &w83791d_group);
1414
1415 return 0;
1416 }
1417
1418 static void w83791d_init_client(struct i2c_client *client)
1419 {
1420 struct w83791d_data *data = i2c_get_clientdata(client);
1421 u8 tmp;
1422 u8 old_beep;
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438 if (reset || init) {
1439
1440 old_beep = w83791d_read(client, W83791D_REG_BEEP_CONFIG);
1441
1442 if (reset) {
1443
1444 w83791d_write(client, W83791D_REG_CONFIG, 0x80);
1445 }
1446
1447
1448 w83791d_write(client, W83791D_REG_BEEP_CONFIG, old_beep | 0x80);
1449
1450
1451 tmp = w83791d_read(client, W83791D_REG_BEEP_CTRL[1]);
1452 w83791d_write(client, W83791D_REG_BEEP_CTRL[1], tmp & 0xef);
1453
1454 if (init) {
1455
1456 tmp = w83791d_read(client, W83791D_REG_TEMP2_CONFIG);
1457 if (tmp & 1) {
1458 w83791d_write(client, W83791D_REG_TEMP2_CONFIG,
1459 tmp & 0xfe);
1460 }
1461
1462 tmp = w83791d_read(client, W83791D_REG_TEMP3_CONFIG);
1463 if (tmp & 1) {
1464 w83791d_write(client, W83791D_REG_TEMP3_CONFIG,
1465 tmp & 0xfe);
1466 }
1467
1468
1469 tmp = w83791d_read(client, W83791D_REG_CONFIG) & 0xf7;
1470 w83791d_write(client, W83791D_REG_CONFIG, tmp | 0x01);
1471 }
1472 }
1473
1474 data->vrm = vid_which_vrm();
1475 }
1476
1477 static struct w83791d_data *w83791d_update_device(struct device *dev)
1478 {
1479 struct i2c_client *client = to_i2c_client(dev);
1480 struct w83791d_data *data = i2c_get_clientdata(client);
1481 int i, j;
1482 u8 reg_array_tmp[3];
1483 u8 vbat_reg;
1484
1485 mutex_lock(&data->update_lock);
1486
1487 if (time_after(jiffies, data->last_updated + (HZ * 3))
1488 || !data->valid) {
1489 dev_dbg(dev, "Starting w83791d device update\n");
1490
1491
1492 for (i = 0; i < NUMBER_OF_VIN; i++) {
1493 data->in[i] = w83791d_read(client,
1494 W83791D_REG_IN[i]);
1495 data->in_max[i] = w83791d_read(client,
1496 W83791D_REG_IN_MAX[i]);
1497 data->in_min[i] = w83791d_read(client,
1498 W83791D_REG_IN_MIN[i]);
1499 }
1500
1501
1502 for (i = 0; i < NUMBER_OF_FANIN; i++) {
1503
1504 data->fan[i] = w83791d_read(client,
1505 W83791D_REG_FAN[i]);
1506 data->fan_min[i] = w83791d_read(client,
1507 W83791D_REG_FAN_MIN[i]);
1508 }
1509
1510
1511 for (i = 0; i < 3; i++) {
1512 reg_array_tmp[i] = w83791d_read(client,
1513 W83791D_REG_FAN_DIV[i]);
1514 }
1515 data->fan_div[0] = (reg_array_tmp[0] >> 4) & 0x03;
1516 data->fan_div[1] = (reg_array_tmp[0] >> 6) & 0x03;
1517 data->fan_div[2] = (reg_array_tmp[1] >> 6) & 0x03;
1518 data->fan_div[3] = reg_array_tmp[2] & 0x07;
1519 data->fan_div[4] = (reg_array_tmp[2] >> 4) & 0x07;
1520
1521
1522
1523
1524
1525 vbat_reg = w83791d_read(client, W83791D_REG_VBAT);
1526 for (i = 0; i < 3; i++)
1527 data->fan_div[i] |= (vbat_reg >> (3 + i)) & 0x04;
1528
1529
1530 for (i = 0; i < NUMBER_OF_PWM; i++) {
1531 data->pwm[i] = w83791d_read(client,
1532 W83791D_REG_PWM[i]);
1533 }
1534
1535
1536 for (i = 0; i < 2; i++) {
1537 reg_array_tmp[i] = w83791d_read(client,
1538 W83791D_REG_FAN_CFG[i]);
1539 }
1540 data->pwm_enable[0] = (reg_array_tmp[0] >> 2) & 0x03;
1541 data->pwm_enable[1] = (reg_array_tmp[0] >> 4) & 0x03;
1542 data->pwm_enable[2] = (reg_array_tmp[1] >> 2) & 0x03;
1543
1544
1545 for (i = 0; i < 3; i++) {
1546 data->temp_target[i] = w83791d_read(client,
1547 W83791D_REG_TEMP_TARGET[i]) & 0x7f;
1548 }
1549
1550
1551 for (i = 0; i < 2; i++) {
1552 reg_array_tmp[i] = w83791d_read(client,
1553 W83791D_REG_TEMP_TOL[i]);
1554 }
1555 data->temp_tolerance[0] = reg_array_tmp[0] & 0x0f;
1556 data->temp_tolerance[1] = (reg_array_tmp[0] >> 4) & 0x0f;
1557 data->temp_tolerance[2] = reg_array_tmp[1] & 0x0f;
1558
1559
1560 for (i = 0; i < 3; i++) {
1561 data->temp1[i] = w83791d_read(client,
1562 W83791D_REG_TEMP1[i]);
1563 }
1564
1565
1566 for (i = 0; i < 2; i++) {
1567 for (j = 0; j < 3; j++) {
1568 data->temp_add[i][j] =
1569 (w83791d_read(client,
1570 W83791D_REG_TEMP_ADD[i][j * 2]) << 8) |
1571 w83791d_read(client,
1572 W83791D_REG_TEMP_ADD[i][j * 2 + 1]);
1573 }
1574 }
1575
1576
1577 data->alarms =
1578 w83791d_read(client, W83791D_REG_ALARM1) +
1579 (w83791d_read(client, W83791D_REG_ALARM2) << 8) +
1580 (w83791d_read(client, W83791D_REG_ALARM3) << 16);
1581
1582
1583 data->beep_mask =
1584 w83791d_read(client, W83791D_REG_BEEP_CTRL[0]) +
1585 (w83791d_read(client, W83791D_REG_BEEP_CTRL[1]) << 8) +
1586 (w83791d_read(client, W83791D_REG_BEEP_CTRL[2]) << 16);
1587
1588
1589 data->beep_enable =
1590 (data->beep_mask >> GLOBAL_BEEP_ENABLE_SHIFT) & 0x01;
1591
1592
1593 i = w83791d_read(client, W83791D_REG_VID_FANDIV);
1594 data->vid = i & 0x0f;
1595 data->vid |= (w83791d_read(client, W83791D_REG_DID_VID4) & 0x01)
1596 << 4;
1597
1598 data->last_updated = jiffies;
1599 data->valid = true;
1600 }
1601
1602 mutex_unlock(&data->update_lock);
1603
1604 #ifdef DEBUG
1605 w83791d_print_debug(data, dev);
1606 #endif
1607
1608 return data;
1609 }
1610
1611 #ifdef DEBUG
1612 static void w83791d_print_debug(struct w83791d_data *data, struct device *dev)
1613 {
1614 int i = 0, j = 0;
1615
1616 dev_dbg(dev, "======Start of w83791d debug values======\n");
1617 dev_dbg(dev, "%d set of Voltages: ===>\n", NUMBER_OF_VIN);
1618 for (i = 0; i < NUMBER_OF_VIN; i++) {
1619 dev_dbg(dev, "vin[%d] is: 0x%02x\n", i, data->in[i]);
1620 dev_dbg(dev, "vin[%d] min is: 0x%02x\n", i, data->in_min[i]);
1621 dev_dbg(dev, "vin[%d] max is: 0x%02x\n", i, data->in_max[i]);
1622 }
1623 dev_dbg(dev, "%d set of Fan Counts/Divisors: ===>\n", NUMBER_OF_FANIN);
1624 for (i = 0; i < NUMBER_OF_FANIN; i++) {
1625 dev_dbg(dev, "fan[%d] is: 0x%02x\n", i, data->fan[i]);
1626 dev_dbg(dev, "fan[%d] min is: 0x%02x\n", i, data->fan_min[i]);
1627 dev_dbg(dev, "fan_div[%d] is: 0x%02x\n", i, data->fan_div[i]);
1628 }
1629
1630
1631
1632
1633
1634 dev_dbg(dev, "%d set of Temperatures: ===>\n", NUMBER_OF_TEMPIN);
1635 for (i = 0; i < 3; i++)
1636 dev_dbg(dev, "temp1[%d] is: 0x%02x\n", i, (u8) data->temp1[i]);
1637 for (i = 0; i < 2; i++) {
1638 for (j = 0; j < 3; j++) {
1639 dev_dbg(dev, "temp_add[%d][%d] is: 0x%04x\n", i, j,
1640 (u16) data->temp_add[i][j]);
1641 }
1642 }
1643
1644 dev_dbg(dev, "Misc Information: ===>\n");
1645 dev_dbg(dev, "alarm is: 0x%08x\n", data->alarms);
1646 dev_dbg(dev, "beep_mask is: 0x%08x\n", data->beep_mask);
1647 dev_dbg(dev, "beep_enable is: %d\n", data->beep_enable);
1648 dev_dbg(dev, "vid is: 0x%02x\n", data->vid);
1649 dev_dbg(dev, "vrm is: 0x%02x\n", data->vrm);
1650 dev_dbg(dev, "=======End of w83791d debug values========\n");
1651 dev_dbg(dev, "\n");
1652 }
1653 #endif
1654
1655 module_i2c_driver(w83791d_driver);
1656
1657 MODULE_AUTHOR("Charles Spirakis <bezaur@gmail.com>");
1658 MODULE_DESCRIPTION("W83791D driver");
1659 MODULE_LICENSE("GPL");