0001
0002
0003
0004
0005
0006
0007
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009
0010 #include <linux/err.h>
0011 #include <linux/i2c.h>
0012 #include <linux/init.h>
0013 #include <linux/hwmon.h>
0014 #include <linux/hwmon-sysfs.h>
0015 #include <linux/jiffies.h>
0016 #include <linux/module.h>
0017 #include <linux/mutex.h>
0018 #include <linux/regmap.h>
0019 #include <linux/slab.h>
0020
0021 #define DRVNAME "nct7802"
0022
0023 static const u8 REG_VOLTAGE[5] = { 0x09, 0x0a, 0x0c, 0x0d, 0x0e };
0024
0025 static const u8 REG_VOLTAGE_LIMIT_LSB[2][5] = {
0026 { 0x46, 0x00, 0x40, 0x42, 0x44 },
0027 { 0x45, 0x00, 0x3f, 0x41, 0x43 },
0028 };
0029
0030 static const u8 REG_VOLTAGE_LIMIT_MSB[5] = { 0x48, 0x00, 0x47, 0x47, 0x48 };
0031
0032 static const u8 REG_VOLTAGE_LIMIT_MSB_SHIFT[2][5] = {
0033 { 0, 0, 4, 0, 4 },
0034 { 2, 0, 6, 2, 6 },
0035 };
0036
0037 #define REG_BANK 0x00
0038 #define REG_TEMP_LSB 0x05
0039 #define REG_TEMP_PECI_LSB 0x08
0040 #define REG_VOLTAGE_LOW 0x0f
0041 #define REG_FANCOUNT_LOW 0x13
0042 #define REG_START 0x21
0043 #define REG_MODE 0x22
0044 #define REG_PECI_ENABLE 0x23
0045 #define REG_FAN_ENABLE 0x24
0046 #define REG_VMON_ENABLE 0x25
0047 #define REG_PWM(x) (0x60 + (x))
0048 #define REG_SMARTFAN_EN(x) (0x64 + (x) / 2)
0049 #define SMARTFAN_EN_SHIFT(x) ((x) % 2 * 4)
0050 #define REG_VENDOR_ID 0xfd
0051 #define REG_CHIP_ID 0xfe
0052 #define REG_VERSION_ID 0xff
0053
0054
0055
0056
0057
0058 #define RTD_MODE_CURRENT 0x1
0059 #define RTD_MODE_THERMISTOR 0x2
0060 #define RTD_MODE_VOLTAGE 0x3
0061
0062 #define MODE_RTD_MASK 0x3
0063 #define MODE_LTD_EN 0x40
0064
0065
0066
0067
0068
0069 #define MODE_BIT_OFFSET_RTD(index) ((index) * 2)
0070
0071
0072
0073
0074
0075 struct nct7802_data {
0076 struct regmap *regmap;
0077 struct mutex access_lock;
0078 u8 in_status;
0079 struct mutex in_alarm_lock;
0080 };
0081
0082 static ssize_t temp_type_show(struct device *dev,
0083 struct device_attribute *attr, char *buf)
0084 {
0085 struct nct7802_data *data = dev_get_drvdata(dev);
0086 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
0087 unsigned int mode;
0088 int ret;
0089
0090 ret = regmap_read(data->regmap, REG_MODE, &mode);
0091 if (ret < 0)
0092 return ret;
0093
0094 return sprintf(buf, "%u\n", (mode >> (2 * sattr->index) & 3) + 2);
0095 }
0096
0097 static ssize_t temp_type_store(struct device *dev,
0098 struct device_attribute *attr, const char *buf,
0099 size_t count)
0100 {
0101 struct nct7802_data *data = dev_get_drvdata(dev);
0102 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
0103 unsigned int type;
0104 int err;
0105
0106 err = kstrtouint(buf, 0, &type);
0107 if (err < 0)
0108 return err;
0109 if (sattr->index == 2 && type != 4)
0110 return -EINVAL;
0111 if (type < 3 || type > 4)
0112 return -EINVAL;
0113 err = regmap_update_bits(data->regmap, REG_MODE,
0114 3 << 2 * sattr->index, (type - 2) << 2 * sattr->index);
0115 return err ? : count;
0116 }
0117
0118 static ssize_t pwm_mode_show(struct device *dev,
0119 struct device_attribute *attr, char *buf)
0120 {
0121 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
0122 struct nct7802_data *data = dev_get_drvdata(dev);
0123 unsigned int regval;
0124 int ret;
0125
0126 if (sattr->index > 1)
0127 return sprintf(buf, "1\n");
0128
0129 ret = regmap_read(data->regmap, 0x5E, ®val);
0130 if (ret < 0)
0131 return ret;
0132
0133 return sprintf(buf, "%u\n", !(regval & (1 << sattr->index)));
0134 }
0135
0136 static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
0137 char *buf)
0138 {
0139 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0140 struct nct7802_data *data = dev_get_drvdata(dev);
0141 unsigned int val;
0142 int ret;
0143
0144 if (!attr->index)
0145 return sprintf(buf, "255\n");
0146
0147 ret = regmap_read(data->regmap, attr->index, &val);
0148 if (ret < 0)
0149 return ret;
0150
0151 return sprintf(buf, "%d\n", val);
0152 }
0153
0154 static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
0155 const char *buf, size_t count)
0156 {
0157 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0158 struct nct7802_data *data = dev_get_drvdata(dev);
0159 int err;
0160 u8 val;
0161
0162 err = kstrtou8(buf, 0, &val);
0163 if (err < 0)
0164 return err;
0165
0166 err = regmap_write(data->regmap, attr->index, val);
0167 return err ? : count;
0168 }
0169
0170 static ssize_t pwm_enable_show(struct device *dev,
0171 struct device_attribute *attr, char *buf)
0172 {
0173 struct nct7802_data *data = dev_get_drvdata(dev);
0174 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
0175 unsigned int reg, enabled;
0176 int ret;
0177
0178 ret = regmap_read(data->regmap, REG_SMARTFAN_EN(sattr->index), ®);
0179 if (ret < 0)
0180 return ret;
0181 enabled = reg >> SMARTFAN_EN_SHIFT(sattr->index) & 1;
0182 return sprintf(buf, "%u\n", enabled + 1);
0183 }
0184
0185 static ssize_t pwm_enable_store(struct device *dev,
0186 struct device_attribute *attr,
0187 const char *buf, size_t count)
0188 {
0189 struct nct7802_data *data = dev_get_drvdata(dev);
0190 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
0191 u8 val;
0192 int ret;
0193
0194 ret = kstrtou8(buf, 0, &val);
0195 if (ret < 0)
0196 return ret;
0197 if (val < 1 || val > 2)
0198 return -EINVAL;
0199 ret = regmap_update_bits(data->regmap, REG_SMARTFAN_EN(sattr->index),
0200 1 << SMARTFAN_EN_SHIFT(sattr->index),
0201 (val - 1) << SMARTFAN_EN_SHIFT(sattr->index));
0202 return ret ? : count;
0203 }
0204
0205 static int nct7802_read_temp(struct nct7802_data *data,
0206 u8 reg_temp, u8 reg_temp_low, int *temp)
0207 {
0208 unsigned int t1, t2 = 0;
0209 int err;
0210
0211 *temp = 0;
0212
0213 mutex_lock(&data->access_lock);
0214 err = regmap_read(data->regmap, reg_temp, &t1);
0215 if (err < 0)
0216 goto abort;
0217 t1 <<= 8;
0218 if (reg_temp_low) {
0219 err = regmap_read(data->regmap, reg_temp_low, &t2);
0220 if (err < 0)
0221 goto abort;
0222 }
0223 t1 |= t2 & 0xe0;
0224 *temp = (s16)t1 / 32 * 125;
0225 abort:
0226 mutex_unlock(&data->access_lock);
0227 return err;
0228 }
0229
0230 static int nct7802_read_fan(struct nct7802_data *data, u8 reg_fan)
0231 {
0232 unsigned int f1, f2;
0233 int ret;
0234
0235 mutex_lock(&data->access_lock);
0236 ret = regmap_read(data->regmap, reg_fan, &f1);
0237 if (ret < 0)
0238 goto abort;
0239 ret = regmap_read(data->regmap, REG_FANCOUNT_LOW, &f2);
0240 if (ret < 0)
0241 goto abort;
0242 ret = (f1 << 5) | (f2 >> 3);
0243
0244 if (ret == 0x1fff)
0245 ret = 0;
0246 else if (ret)
0247 ret = DIV_ROUND_CLOSEST(1350000U, ret);
0248 abort:
0249 mutex_unlock(&data->access_lock);
0250 return ret;
0251 }
0252
0253 static int nct7802_read_fan_min(struct nct7802_data *data, u8 reg_fan_low,
0254 u8 reg_fan_high)
0255 {
0256 unsigned int f1, f2;
0257 int ret;
0258
0259 mutex_lock(&data->access_lock);
0260 ret = regmap_read(data->regmap, reg_fan_low, &f1);
0261 if (ret < 0)
0262 goto abort;
0263 ret = regmap_read(data->regmap, reg_fan_high, &f2);
0264 if (ret < 0)
0265 goto abort;
0266 ret = f1 | ((f2 & 0xf8) << 5);
0267
0268 if (ret == 0x1fff)
0269 ret = 0;
0270 else if (ret)
0271 ret = DIV_ROUND_CLOSEST(1350000U, ret);
0272 else
0273 ret = 1350000U;
0274 abort:
0275 mutex_unlock(&data->access_lock);
0276 return ret;
0277 }
0278
0279 static int nct7802_write_fan_min(struct nct7802_data *data, u8 reg_fan_low,
0280 u8 reg_fan_high, unsigned long limit)
0281 {
0282 int err;
0283
0284 if (limit)
0285 limit = DIV_ROUND_CLOSEST(1350000U, limit);
0286 else
0287 limit = 0x1fff;
0288 limit = clamp_val(limit, 0, 0x1fff);
0289
0290 mutex_lock(&data->access_lock);
0291 err = regmap_write(data->regmap, reg_fan_low, limit & 0xff);
0292 if (err < 0)
0293 goto abort;
0294
0295 err = regmap_write(data->regmap, reg_fan_high, (limit & 0x1f00) >> 5);
0296 abort:
0297 mutex_unlock(&data->access_lock);
0298 return err;
0299 }
0300
0301 static u8 nct7802_vmul[] = { 4, 2, 2, 2, 2 };
0302
0303 static int nct7802_read_voltage(struct nct7802_data *data, int nr, int index)
0304 {
0305 unsigned int v1, v2;
0306 int ret;
0307
0308 mutex_lock(&data->access_lock);
0309 if (index == 0) {
0310 ret = regmap_read(data->regmap, REG_VOLTAGE[nr], &v1);
0311 if (ret < 0)
0312 goto abort;
0313 ret = regmap_read(data->regmap, REG_VOLTAGE_LOW, &v2);
0314 if (ret < 0)
0315 goto abort;
0316 ret = ((v1 << 2) | (v2 >> 6)) * nct7802_vmul[nr];
0317 } else {
0318 int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
0319
0320 ret = regmap_read(data->regmap,
0321 REG_VOLTAGE_LIMIT_LSB[index - 1][nr], &v1);
0322 if (ret < 0)
0323 goto abort;
0324 ret = regmap_read(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
0325 &v2);
0326 if (ret < 0)
0327 goto abort;
0328 ret = (v1 | ((v2 << shift) & 0x300)) * nct7802_vmul[nr];
0329 }
0330 abort:
0331 mutex_unlock(&data->access_lock);
0332 return ret;
0333 }
0334
0335 static int nct7802_write_voltage(struct nct7802_data *data, int nr, int index,
0336 unsigned long voltage)
0337 {
0338 int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
0339 int err;
0340
0341 voltage = clamp_val(voltage, 0, 0x3ff * nct7802_vmul[nr]);
0342 voltage = DIV_ROUND_CLOSEST(voltage, nct7802_vmul[nr]);
0343
0344 mutex_lock(&data->access_lock);
0345 err = regmap_write(data->regmap,
0346 REG_VOLTAGE_LIMIT_LSB[index - 1][nr],
0347 voltage & 0xff);
0348 if (err < 0)
0349 goto abort;
0350
0351 err = regmap_update_bits(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
0352 0x0300 >> shift, (voltage & 0x0300) >> shift);
0353 abort:
0354 mutex_unlock(&data->access_lock);
0355 return err;
0356 }
0357
0358 static ssize_t in_show(struct device *dev, struct device_attribute *attr,
0359 char *buf)
0360 {
0361 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
0362 struct nct7802_data *data = dev_get_drvdata(dev);
0363 int voltage;
0364
0365 voltage = nct7802_read_voltage(data, sattr->nr, sattr->index);
0366 if (voltage < 0)
0367 return voltage;
0368
0369 return sprintf(buf, "%d\n", voltage);
0370 }
0371
0372 static ssize_t in_store(struct device *dev, struct device_attribute *attr,
0373 const char *buf, size_t count)
0374 {
0375 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
0376 struct nct7802_data *data = dev_get_drvdata(dev);
0377 int index = sattr->index;
0378 int nr = sattr->nr;
0379 unsigned long val;
0380 int err;
0381
0382 err = kstrtoul(buf, 10, &val);
0383 if (err < 0)
0384 return err;
0385
0386 err = nct7802_write_voltage(data, nr, index, val);
0387 return err ? : count;
0388 }
0389
0390 static ssize_t in_alarm_show(struct device *dev, struct device_attribute *attr,
0391 char *buf)
0392 {
0393 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
0394 struct nct7802_data *data = dev_get_drvdata(dev);
0395 int volt, min, max, ret;
0396 unsigned int val;
0397
0398 mutex_lock(&data->in_alarm_lock);
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412 ret = regmap_read(data->regmap, 0x1e, &val);
0413 if (ret < 0)
0414 goto abort;
0415
0416
0417 data->in_status &= ~((val & 0x0f) << 4);
0418
0419
0420 if (!(data->in_status & (0x10 << sattr->index))) {
0421 ret = nct7802_read_voltage(data, sattr->nr, 0);
0422 if (ret < 0)
0423 goto abort;
0424 volt = ret;
0425
0426 ret = nct7802_read_voltage(data, sattr->nr, 1);
0427 if (ret < 0)
0428 goto abort;
0429 min = ret;
0430
0431 ret = nct7802_read_voltage(data, sattr->nr, 2);
0432 if (ret < 0)
0433 goto abort;
0434 max = ret;
0435
0436 if (volt < min || volt > max)
0437 data->in_status |= (1 << sattr->index);
0438 else
0439 data->in_status &= ~(1 << sattr->index);
0440
0441 data->in_status |= 0x10 << sattr->index;
0442 }
0443
0444 ret = sprintf(buf, "%u\n", !!(data->in_status & (1 << sattr->index)));
0445 abort:
0446 mutex_unlock(&data->in_alarm_lock);
0447 return ret;
0448 }
0449
0450 static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
0451 char *buf)
0452 {
0453 struct nct7802_data *data = dev_get_drvdata(dev);
0454 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
0455 int err, temp;
0456
0457 err = nct7802_read_temp(data, sattr->nr, sattr->index, &temp);
0458 if (err < 0)
0459 return err;
0460
0461 return sprintf(buf, "%d\n", temp);
0462 }
0463
0464 static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
0465 const char *buf, size_t count)
0466 {
0467 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
0468 struct nct7802_data *data = dev_get_drvdata(dev);
0469 int nr = sattr->nr;
0470 long val;
0471 int err;
0472
0473 err = kstrtol(buf, 10, &val);
0474 if (err < 0)
0475 return err;
0476
0477 val = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
0478
0479 err = regmap_write(data->regmap, nr, val & 0xff);
0480 return err ? : count;
0481 }
0482
0483 static ssize_t fan_show(struct device *dev, struct device_attribute *attr,
0484 char *buf)
0485 {
0486 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
0487 struct nct7802_data *data = dev_get_drvdata(dev);
0488 int speed;
0489
0490 speed = nct7802_read_fan(data, sattr->index);
0491 if (speed < 0)
0492 return speed;
0493
0494 return sprintf(buf, "%d\n", speed);
0495 }
0496
0497 static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
0498 char *buf)
0499 {
0500 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
0501 struct nct7802_data *data = dev_get_drvdata(dev);
0502 int speed;
0503
0504 speed = nct7802_read_fan_min(data, sattr->nr, sattr->index);
0505 if (speed < 0)
0506 return speed;
0507
0508 return sprintf(buf, "%d\n", speed);
0509 }
0510
0511 static ssize_t fan_min_store(struct device *dev,
0512 struct device_attribute *attr, const char *buf,
0513 size_t count)
0514 {
0515 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
0516 struct nct7802_data *data = dev_get_drvdata(dev);
0517 unsigned long val;
0518 int err;
0519
0520 err = kstrtoul(buf, 10, &val);
0521 if (err < 0)
0522 return err;
0523
0524 err = nct7802_write_fan_min(data, sattr->nr, sattr->index, val);
0525 return err ? : count;
0526 }
0527
0528 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
0529 char *buf)
0530 {
0531 struct nct7802_data *data = dev_get_drvdata(dev);
0532 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
0533 int bit = sattr->index;
0534 unsigned int val;
0535 int ret;
0536
0537 ret = regmap_read(data->regmap, sattr->nr, &val);
0538 if (ret < 0)
0539 return ret;
0540
0541 return sprintf(buf, "%u\n", !!(val & (1 << bit)));
0542 }
0543
0544 static ssize_t
0545 beep_show(struct device *dev, struct device_attribute *attr, char *buf)
0546 {
0547 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
0548 struct nct7802_data *data = dev_get_drvdata(dev);
0549 unsigned int regval;
0550 int err;
0551
0552 err = regmap_read(data->regmap, sattr->nr, ®val);
0553 if (err)
0554 return err;
0555
0556 return sprintf(buf, "%u\n", !!(regval & (1 << sattr->index)));
0557 }
0558
0559 static ssize_t
0560 beep_store(struct device *dev, struct device_attribute *attr, const char *buf,
0561 size_t count)
0562 {
0563 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
0564 struct nct7802_data *data = dev_get_drvdata(dev);
0565 unsigned long val;
0566 int err;
0567
0568 err = kstrtoul(buf, 10, &val);
0569 if (err < 0)
0570 return err;
0571 if (val > 1)
0572 return -EINVAL;
0573
0574 err = regmap_update_bits(data->regmap, sattr->nr, 1 << sattr->index,
0575 val ? 1 << sattr->index : 0);
0576 return err ? : count;
0577 }
0578
0579 static SENSOR_DEVICE_ATTR_RW(temp1_type, temp_type, 0);
0580 static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0x01, REG_TEMP_LSB);
0581 static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, 0x31, 0);
0582 static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, 0x30, 0);
0583 static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, 0x3a, 0);
0584
0585 static SENSOR_DEVICE_ATTR_RW(temp2_type, temp_type, 1);
0586 static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 0x02, REG_TEMP_LSB);
0587 static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, 0x33, 0);
0588 static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, 0x32, 0);
0589 static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, 0x3b, 0);
0590
0591 static SENSOR_DEVICE_ATTR_RW(temp3_type, temp_type, 2);
0592 static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 0x03, REG_TEMP_LSB);
0593 static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, 0x35, 0);
0594 static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, 0x34, 0);
0595 static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, 0x3c, 0);
0596
0597 static SENSOR_DEVICE_ATTR_2_RO(temp4_input, temp, 0x04, 0);
0598 static SENSOR_DEVICE_ATTR_2_RW(temp4_min, temp, 0x37, 0);
0599 static SENSOR_DEVICE_ATTR_2_RW(temp4_max, temp, 0x36, 0);
0600 static SENSOR_DEVICE_ATTR_2_RW(temp4_crit, temp, 0x3d, 0);
0601
0602 static SENSOR_DEVICE_ATTR_2_RO(temp5_input, temp, 0x06, REG_TEMP_PECI_LSB);
0603 static SENSOR_DEVICE_ATTR_2_RW(temp5_min, temp, 0x39, 0);
0604 static SENSOR_DEVICE_ATTR_2_RW(temp5_max, temp, 0x38, 0);
0605 static SENSOR_DEVICE_ATTR_2_RW(temp5_crit, temp, 0x3e, 0);
0606
0607 static SENSOR_DEVICE_ATTR_2_RO(temp6_input, temp, 0x07, REG_TEMP_PECI_LSB);
0608
0609 static SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, alarm, 0x18, 0);
0610 static SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, alarm, 0x18, 1);
0611 static SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, alarm, 0x18, 2);
0612 static SENSOR_DEVICE_ATTR_2_RO(temp4_min_alarm, alarm, 0x18, 3);
0613 static SENSOR_DEVICE_ATTR_2_RO(temp5_min_alarm, alarm, 0x18, 4);
0614
0615 static SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, alarm, 0x19, 0);
0616 static SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, alarm, 0x19, 1);
0617 static SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, alarm, 0x19, 2);
0618 static SENSOR_DEVICE_ATTR_2_RO(temp4_max_alarm, alarm, 0x19, 3);
0619 static SENSOR_DEVICE_ATTR_2_RO(temp5_max_alarm, alarm, 0x19, 4);
0620
0621 static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, alarm, 0x1b, 0);
0622 static SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, alarm, 0x1b, 1);
0623 static SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, alarm, 0x1b, 2);
0624 static SENSOR_DEVICE_ATTR_2_RO(temp4_crit_alarm, alarm, 0x1b, 3);
0625 static SENSOR_DEVICE_ATTR_2_RO(temp5_crit_alarm, alarm, 0x1b, 4);
0626
0627 static SENSOR_DEVICE_ATTR_2_RO(temp1_fault, alarm, 0x17, 0);
0628 static SENSOR_DEVICE_ATTR_2_RO(temp2_fault, alarm, 0x17, 1);
0629 static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, alarm, 0x17, 2);
0630
0631 static SENSOR_DEVICE_ATTR_2_RW(temp1_beep, beep, 0x5c, 0);
0632 static SENSOR_DEVICE_ATTR_2_RW(temp2_beep, beep, 0x5c, 1);
0633 static SENSOR_DEVICE_ATTR_2_RW(temp3_beep, beep, 0x5c, 2);
0634 static SENSOR_DEVICE_ATTR_2_RW(temp4_beep, beep, 0x5c, 3);
0635 static SENSOR_DEVICE_ATTR_2_RW(temp5_beep, beep, 0x5c, 4);
0636 static SENSOR_DEVICE_ATTR_2_RW(temp6_beep, beep, 0x5c, 5);
0637
0638 static struct attribute *nct7802_temp_attrs[] = {
0639 &sensor_dev_attr_temp1_type.dev_attr.attr,
0640 &sensor_dev_attr_temp1_input.dev_attr.attr,
0641 &sensor_dev_attr_temp1_min.dev_attr.attr,
0642 &sensor_dev_attr_temp1_max.dev_attr.attr,
0643 &sensor_dev_attr_temp1_crit.dev_attr.attr,
0644 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
0645 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
0646 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
0647 &sensor_dev_attr_temp1_fault.dev_attr.attr,
0648 &sensor_dev_attr_temp1_beep.dev_attr.attr,
0649
0650 &sensor_dev_attr_temp2_type.dev_attr.attr,
0651 &sensor_dev_attr_temp2_input.dev_attr.attr,
0652 &sensor_dev_attr_temp2_min.dev_attr.attr,
0653 &sensor_dev_attr_temp2_max.dev_attr.attr,
0654 &sensor_dev_attr_temp2_crit.dev_attr.attr,
0655 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
0656 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
0657 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
0658 &sensor_dev_attr_temp2_fault.dev_attr.attr,
0659 &sensor_dev_attr_temp2_beep.dev_attr.attr,
0660
0661 &sensor_dev_attr_temp3_type.dev_attr.attr,
0662 &sensor_dev_attr_temp3_input.dev_attr.attr,
0663 &sensor_dev_attr_temp3_min.dev_attr.attr,
0664 &sensor_dev_attr_temp3_max.dev_attr.attr,
0665 &sensor_dev_attr_temp3_crit.dev_attr.attr,
0666 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
0667 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
0668 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
0669 &sensor_dev_attr_temp3_fault.dev_attr.attr,
0670 &sensor_dev_attr_temp3_beep.dev_attr.attr,
0671
0672 &sensor_dev_attr_temp4_input.dev_attr.attr,
0673 &sensor_dev_attr_temp4_min.dev_attr.attr,
0674 &sensor_dev_attr_temp4_max.dev_attr.attr,
0675 &sensor_dev_attr_temp4_crit.dev_attr.attr,
0676 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
0677 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
0678 &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
0679 &sensor_dev_attr_temp4_beep.dev_attr.attr,
0680
0681 &sensor_dev_attr_temp5_input.dev_attr.attr,
0682 &sensor_dev_attr_temp5_min.dev_attr.attr,
0683 &sensor_dev_attr_temp5_max.dev_attr.attr,
0684 &sensor_dev_attr_temp5_crit.dev_attr.attr,
0685 &sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
0686 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
0687 &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr,
0688 &sensor_dev_attr_temp5_beep.dev_attr.attr,
0689
0690 &sensor_dev_attr_temp6_input.dev_attr.attr,
0691 &sensor_dev_attr_temp6_beep.dev_attr.attr,
0692
0693 NULL
0694 };
0695
0696 static umode_t nct7802_temp_is_visible(struct kobject *kobj,
0697 struct attribute *attr, int index)
0698 {
0699 struct device *dev = kobj_to_dev(kobj);
0700 struct nct7802_data *data = dev_get_drvdata(dev);
0701 unsigned int reg;
0702 int err;
0703
0704 err = regmap_read(data->regmap, REG_MODE, ®);
0705 if (err < 0)
0706 return 0;
0707
0708 if (index < 10 &&
0709 (reg & 03) != 0x01 && (reg & 0x03) != 0x02)
0710 return 0;
0711
0712 if (index >= 10 && index < 20 &&
0713 (reg & 0x0c) != 0x04 && (reg & 0x0c) != 0x08)
0714 return 0;
0715 if (index >= 20 && index < 30 && (reg & 0x30) != 0x20)
0716 return 0;
0717
0718 if (index >= 30 && index < 38)
0719 return attr->mode;
0720
0721 err = regmap_read(data->regmap, REG_PECI_ENABLE, ®);
0722 if (err < 0)
0723 return 0;
0724
0725 if (index >= 38 && index < 46 && !(reg & 0x01))
0726 return 0;
0727
0728 if (index >= 0x46 && (!(reg & 0x02)))
0729 return 0;
0730
0731 return attr->mode;
0732 }
0733
0734 static const struct attribute_group nct7802_temp_group = {
0735 .attrs = nct7802_temp_attrs,
0736 .is_visible = nct7802_temp_is_visible,
0737 };
0738
0739 static SENSOR_DEVICE_ATTR_2_RO(in0_input, in, 0, 0);
0740 static SENSOR_DEVICE_ATTR_2_RW(in0_min, in, 0, 1);
0741 static SENSOR_DEVICE_ATTR_2_RW(in0_max, in, 0, 2);
0742 static SENSOR_DEVICE_ATTR_2_RO(in0_alarm, in_alarm, 0, 3);
0743 static SENSOR_DEVICE_ATTR_2_RW(in0_beep, beep, 0x5a, 3);
0744
0745 static SENSOR_DEVICE_ATTR_2_RO(in1_input, in, 1, 0);
0746
0747 static SENSOR_DEVICE_ATTR_2_RO(in2_input, in, 2, 0);
0748 static SENSOR_DEVICE_ATTR_2_RW(in2_min, in, 2, 1);
0749 static SENSOR_DEVICE_ATTR_2_RW(in2_max, in, 2, 2);
0750 static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, in_alarm, 2, 0);
0751 static SENSOR_DEVICE_ATTR_2_RW(in2_beep, beep, 0x5a, 0);
0752
0753 static SENSOR_DEVICE_ATTR_2_RO(in3_input, in, 3, 0);
0754 static SENSOR_DEVICE_ATTR_2_RW(in3_min, in, 3, 1);
0755 static SENSOR_DEVICE_ATTR_2_RW(in3_max, in, 3, 2);
0756 static SENSOR_DEVICE_ATTR_2_RO(in3_alarm, in_alarm, 3, 1);
0757 static SENSOR_DEVICE_ATTR_2_RW(in3_beep, beep, 0x5a, 1);
0758
0759 static SENSOR_DEVICE_ATTR_2_RO(in4_input, in, 4, 0);
0760 static SENSOR_DEVICE_ATTR_2_RW(in4_min, in, 4, 1);
0761 static SENSOR_DEVICE_ATTR_2_RW(in4_max, in, 4, 2);
0762 static SENSOR_DEVICE_ATTR_2_RO(in4_alarm, in_alarm, 4, 2);
0763 static SENSOR_DEVICE_ATTR_2_RW(in4_beep, beep, 0x5a, 2);
0764
0765 static struct attribute *nct7802_in_attrs[] = {
0766 &sensor_dev_attr_in0_input.dev_attr.attr,
0767 &sensor_dev_attr_in0_min.dev_attr.attr,
0768 &sensor_dev_attr_in0_max.dev_attr.attr,
0769 &sensor_dev_attr_in0_alarm.dev_attr.attr,
0770 &sensor_dev_attr_in0_beep.dev_attr.attr,
0771
0772 &sensor_dev_attr_in1_input.dev_attr.attr,
0773
0774 &sensor_dev_attr_in2_input.dev_attr.attr,
0775 &sensor_dev_attr_in2_min.dev_attr.attr,
0776 &sensor_dev_attr_in2_max.dev_attr.attr,
0777 &sensor_dev_attr_in2_alarm.dev_attr.attr,
0778 &sensor_dev_attr_in2_beep.dev_attr.attr,
0779
0780 &sensor_dev_attr_in3_input.dev_attr.attr,
0781 &sensor_dev_attr_in3_min.dev_attr.attr,
0782 &sensor_dev_attr_in3_max.dev_attr.attr,
0783 &sensor_dev_attr_in3_alarm.dev_attr.attr,
0784 &sensor_dev_attr_in3_beep.dev_attr.attr,
0785
0786 &sensor_dev_attr_in4_input.dev_attr.attr,
0787 &sensor_dev_attr_in4_min.dev_attr.attr,
0788 &sensor_dev_attr_in4_max.dev_attr.attr,
0789 &sensor_dev_attr_in4_alarm.dev_attr.attr,
0790 &sensor_dev_attr_in4_beep.dev_attr.attr,
0791
0792 NULL,
0793 };
0794
0795 static umode_t nct7802_in_is_visible(struct kobject *kobj,
0796 struct attribute *attr, int index)
0797 {
0798 struct device *dev = kobj_to_dev(kobj);
0799 struct nct7802_data *data = dev_get_drvdata(dev);
0800 unsigned int reg;
0801 int err;
0802
0803 if (index < 6)
0804 return attr->mode;
0805
0806 err = regmap_read(data->regmap, REG_MODE, ®);
0807 if (err < 0)
0808 return 0;
0809
0810 if (index >= 6 && index < 11 && (reg & 0x03) != 0x03)
0811 return 0;
0812 if (index >= 11 && index < 16 && (reg & 0x0c) != 0x0c)
0813 return 0;
0814 if (index >= 16 && (reg & 0x30) != 0x30)
0815 return 0;
0816
0817 return attr->mode;
0818 }
0819
0820 static const struct attribute_group nct7802_in_group = {
0821 .attrs = nct7802_in_attrs,
0822 .is_visible = nct7802_in_is_visible,
0823 };
0824
0825 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0x10);
0826 static SENSOR_DEVICE_ATTR_2_RW(fan1_min, fan_min, 0x49, 0x4c);
0827 static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, alarm, 0x1a, 0);
0828 static SENSOR_DEVICE_ATTR_2_RW(fan1_beep, beep, 0x5b, 0);
0829 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 0x11);
0830 static SENSOR_DEVICE_ATTR_2_RW(fan2_min, fan_min, 0x4a, 0x4d);
0831 static SENSOR_DEVICE_ATTR_2_RO(fan2_alarm, alarm, 0x1a, 1);
0832 static SENSOR_DEVICE_ATTR_2_RW(fan2_beep, beep, 0x5b, 1);
0833 static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 0x12);
0834 static SENSOR_DEVICE_ATTR_2_RW(fan3_min, fan_min, 0x4b, 0x4e);
0835 static SENSOR_DEVICE_ATTR_2_RO(fan3_alarm, alarm, 0x1a, 2);
0836 static SENSOR_DEVICE_ATTR_2_RW(fan3_beep, beep, 0x5b, 2);
0837
0838
0839 static SENSOR_DEVICE_ATTR_RO(pwm1_mode, pwm_mode, 0);
0840 static SENSOR_DEVICE_ATTR_RO(pwm2_mode, pwm_mode, 1);
0841 static SENSOR_DEVICE_ATTR_RO(pwm3_mode, pwm_mode, 2);
0842
0843
0844 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, REG_PWM(0));
0845 static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, REG_PWM(1));
0846 static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, REG_PWM(2));
0847
0848
0849 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_enable, 0);
0850 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_enable, 1);
0851 static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_enable, 2);
0852
0853 static struct attribute *nct7802_fan_attrs[] = {
0854 &sensor_dev_attr_fan1_input.dev_attr.attr,
0855 &sensor_dev_attr_fan1_min.dev_attr.attr,
0856 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
0857 &sensor_dev_attr_fan1_beep.dev_attr.attr,
0858 &sensor_dev_attr_fan2_input.dev_attr.attr,
0859 &sensor_dev_attr_fan2_min.dev_attr.attr,
0860 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
0861 &sensor_dev_attr_fan2_beep.dev_attr.attr,
0862 &sensor_dev_attr_fan3_input.dev_attr.attr,
0863 &sensor_dev_attr_fan3_min.dev_attr.attr,
0864 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
0865 &sensor_dev_attr_fan3_beep.dev_attr.attr,
0866
0867 NULL
0868 };
0869
0870 static umode_t nct7802_fan_is_visible(struct kobject *kobj,
0871 struct attribute *attr, int index)
0872 {
0873 struct device *dev = kobj_to_dev(kobj);
0874 struct nct7802_data *data = dev_get_drvdata(dev);
0875 int fan = index / 4;
0876 unsigned int reg;
0877 int err;
0878
0879 err = regmap_read(data->regmap, REG_FAN_ENABLE, ®);
0880 if (err < 0 || !(reg & (1 << fan)))
0881 return 0;
0882
0883 return attr->mode;
0884 }
0885
0886 static const struct attribute_group nct7802_fan_group = {
0887 .attrs = nct7802_fan_attrs,
0888 .is_visible = nct7802_fan_is_visible,
0889 };
0890
0891 static struct attribute *nct7802_pwm_attrs[] = {
0892 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
0893 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
0894 &sensor_dev_attr_pwm1.dev_attr.attr,
0895 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
0896 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
0897 &sensor_dev_attr_pwm2.dev_attr.attr,
0898 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
0899 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
0900 &sensor_dev_attr_pwm3.dev_attr.attr,
0901 NULL
0902 };
0903
0904 static const struct attribute_group nct7802_pwm_group = {
0905 .attrs = nct7802_pwm_attrs,
0906 };
0907
0908
0909 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, temp, 0x80, 0);
0910 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, temp, 0x81, 0);
0911 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, temp, 0x82, 0);
0912 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, temp, 0x83, 0);
0913 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, temp, 0x84, 0);
0914
0915
0916 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_pwm, pwm, 0x85);
0917 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm, 0x86);
0918 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point3_pwm, pwm, 0x87);
0919 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point4_pwm, pwm, 0x88);
0920 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point5_pwm, pwm, 0);
0921
0922
0923 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, temp, 0x90, 0);
0924 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, temp, 0x91, 0);
0925 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, temp, 0x92, 0);
0926 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, temp, 0x93, 0);
0927 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, temp, 0x94, 0);
0928
0929
0930 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_pwm, pwm, 0x95);
0931 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point2_pwm, pwm, 0x96);
0932 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point3_pwm, pwm, 0x97);
0933 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point4_pwm, pwm, 0x98);
0934 static SENSOR_DEVICE_ATTR_RO(pwm2_auto_point5_pwm, pwm, 0);
0935
0936
0937 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_temp, temp, 0xA0, 0);
0938 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_temp, temp, 0xA1, 0);
0939 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_temp, temp, 0xA2, 0);
0940 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_temp, temp, 0xA3, 0);
0941 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_temp, temp, 0xA4, 0);
0942
0943
0944 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_pwm, pwm, 0xA5);
0945 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point2_pwm, pwm, 0xA6);
0946 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point3_pwm, pwm, 0xA7);
0947 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point4_pwm, pwm, 0xA8);
0948 static SENSOR_DEVICE_ATTR_RO(pwm3_auto_point5_pwm, pwm, 0);
0949
0950 static struct attribute *nct7802_auto_point_attrs[] = {
0951 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
0952 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
0953 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
0954 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
0955 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
0956
0957 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
0958 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
0959 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
0960 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
0961 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
0962
0963 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
0964 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
0965 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
0966 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
0967 &sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr,
0968
0969 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
0970 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
0971 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
0972 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
0973 &sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr,
0974
0975 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
0976 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
0977 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
0978 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
0979 &sensor_dev_attr_pwm3_auto_point5_temp.dev_attr.attr,
0980
0981 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
0982 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
0983 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
0984 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
0985 &sensor_dev_attr_pwm3_auto_point5_pwm.dev_attr.attr,
0986
0987 NULL
0988 };
0989
0990 static const struct attribute_group nct7802_auto_point_group = {
0991 .attrs = nct7802_auto_point_attrs,
0992 };
0993
0994 static const struct attribute_group *nct7802_groups[] = {
0995 &nct7802_temp_group,
0996 &nct7802_in_group,
0997 &nct7802_fan_group,
0998 &nct7802_pwm_group,
0999 &nct7802_auto_point_group,
1000 NULL
1001 };
1002
1003 static int nct7802_detect(struct i2c_client *client,
1004 struct i2c_board_info *info)
1005 {
1006 int reg;
1007
1008
1009
1010
1011
1012 reg = i2c_smbus_read_byte_data(client, REG_BANK);
1013 if (reg != 0x00)
1014 return -ENODEV;
1015
1016 reg = i2c_smbus_read_byte_data(client, REG_VENDOR_ID);
1017 if (reg != 0x50)
1018 return -ENODEV;
1019
1020 reg = i2c_smbus_read_byte_data(client, REG_CHIP_ID);
1021 if (reg != 0xc3)
1022 return -ENODEV;
1023
1024 reg = i2c_smbus_read_byte_data(client, REG_VERSION_ID);
1025 if (reg < 0 || (reg & 0xf0) != 0x20)
1026 return -ENODEV;
1027
1028
1029 reg = i2c_smbus_read_byte_data(client, REG_TEMP_LSB);
1030 if (reg < 0 || (reg & 0x1f))
1031 return -ENODEV;
1032
1033 reg = i2c_smbus_read_byte_data(client, REG_TEMP_PECI_LSB);
1034 if (reg < 0 || (reg & 0x3f))
1035 return -ENODEV;
1036
1037 reg = i2c_smbus_read_byte_data(client, REG_VOLTAGE_LOW);
1038 if (reg < 0 || (reg & 0x3f))
1039 return -ENODEV;
1040
1041 strlcpy(info->type, "nct7802", I2C_NAME_SIZE);
1042 return 0;
1043 }
1044
1045 static bool nct7802_regmap_is_volatile(struct device *dev, unsigned int reg)
1046 {
1047 return (reg != REG_BANK && reg <= 0x20) ||
1048 (reg >= REG_PWM(0) && reg <= REG_PWM(2));
1049 }
1050
1051 static const struct regmap_config nct7802_regmap_config = {
1052 .reg_bits = 8,
1053 .val_bits = 8,
1054 .cache_type = REGCACHE_RBTREE,
1055 .volatile_reg = nct7802_regmap_is_volatile,
1056 };
1057
1058 static int nct7802_get_channel_config(struct device *dev,
1059 struct device_node *node, u8 *mode_mask,
1060 u8 *mode_val)
1061 {
1062 u32 reg;
1063 const char *type_str, *md_str;
1064 u8 md;
1065
1066 if (!node->name || of_node_cmp(node->name, "channel"))
1067 return 0;
1068
1069 if (of_property_read_u32(node, "reg", ®)) {
1070 dev_err(dev, "Could not read reg value for '%s'\n",
1071 node->full_name);
1072 return -EINVAL;
1073 }
1074
1075 if (reg > 3) {
1076 dev_err(dev, "Invalid reg (%u) in '%s'\n", reg,
1077 node->full_name);
1078 return -EINVAL;
1079 }
1080
1081 if (reg == 0) {
1082 if (!of_device_is_available(node))
1083 *mode_val &= ~MODE_LTD_EN;
1084 else
1085 *mode_val |= MODE_LTD_EN;
1086 *mode_mask |= MODE_LTD_EN;
1087 return 0;
1088 }
1089
1090
1091
1092 if (!of_device_is_available(node)) {
1093 *mode_val &= ~(MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1));
1094 *mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1);
1095 return 0;
1096 }
1097
1098 if (of_property_read_string(node, "sensor-type", &type_str)) {
1099 dev_err(dev, "No type for '%s'\n", node->full_name);
1100 return -EINVAL;
1101 }
1102
1103 if (!strcmp(type_str, "voltage")) {
1104 *mode_val |= (RTD_MODE_VOLTAGE & MODE_RTD_MASK)
1105 << MODE_BIT_OFFSET_RTD(reg - 1);
1106 *mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1);
1107 return 0;
1108 }
1109
1110 if (strcmp(type_str, "temperature")) {
1111 dev_err(dev, "Invalid type '%s' for '%s'\n", type_str,
1112 node->full_name);
1113 return -EINVAL;
1114 }
1115
1116 if (reg == 3) {
1117
1118 md = RTD_MODE_THERMISTOR;
1119 } else {
1120 if (of_property_read_string(node, "temperature-mode",
1121 &md_str)) {
1122 dev_err(dev, "No mode for '%s'\n", node->full_name);
1123 return -EINVAL;
1124 }
1125
1126 if (!strcmp(md_str, "thermal-diode"))
1127 md = RTD_MODE_CURRENT;
1128 else if (!strcmp(md_str, "thermistor"))
1129 md = RTD_MODE_THERMISTOR;
1130 else {
1131 dev_err(dev, "Invalid mode '%s' for '%s'\n", md_str,
1132 node->full_name);
1133 return -EINVAL;
1134 }
1135 }
1136
1137 *mode_val |= (md & MODE_RTD_MASK) << MODE_BIT_OFFSET_RTD(reg - 1);
1138 *mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1);
1139
1140 return 0;
1141 }
1142
1143 static int nct7802_configure_channels(struct device *dev,
1144 struct nct7802_data *data)
1145 {
1146
1147 u8 mode_mask = MODE_LTD_EN, mode_val = MODE_LTD_EN;
1148 struct device_node *node;
1149 int err;
1150
1151 if (dev->of_node) {
1152 for_each_child_of_node(dev->of_node, node) {
1153 err = nct7802_get_channel_config(dev, node, &mode_mask,
1154 &mode_val);
1155 if (err) {
1156 of_node_put(node);
1157 return err;
1158 }
1159 }
1160 }
1161
1162 return regmap_update_bits(data->regmap, REG_MODE, mode_mask, mode_val);
1163 }
1164
1165 static int nct7802_init_chip(struct device *dev, struct nct7802_data *data)
1166 {
1167 int err;
1168
1169
1170 err = regmap_update_bits(data->regmap, REG_START, 0x01, 0x01);
1171 if (err)
1172 return err;
1173
1174 err = nct7802_configure_channels(dev, data);
1175 if (err)
1176 return err;
1177
1178
1179 return regmap_update_bits(data->regmap, REG_VMON_ENABLE, 0x03, 0x03);
1180 }
1181
1182 static int nct7802_probe(struct i2c_client *client)
1183 {
1184 struct device *dev = &client->dev;
1185 struct nct7802_data *data;
1186 struct device *hwmon_dev;
1187 int ret;
1188
1189 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
1190 if (data == NULL)
1191 return -ENOMEM;
1192
1193 data->regmap = devm_regmap_init_i2c(client, &nct7802_regmap_config);
1194 if (IS_ERR(data->regmap))
1195 return PTR_ERR(data->regmap);
1196
1197 mutex_init(&data->access_lock);
1198 mutex_init(&data->in_alarm_lock);
1199
1200 ret = nct7802_init_chip(dev, data);
1201 if (ret < 0)
1202 return ret;
1203
1204 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1205 data,
1206 nct7802_groups);
1207 return PTR_ERR_OR_ZERO(hwmon_dev);
1208 }
1209
1210 static const unsigned short nct7802_address_list[] = {
1211 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
1212 };
1213
1214 static const struct i2c_device_id nct7802_idtable[] = {
1215 { "nct7802", 0 },
1216 { }
1217 };
1218 MODULE_DEVICE_TABLE(i2c, nct7802_idtable);
1219
1220 static struct i2c_driver nct7802_driver = {
1221 .class = I2C_CLASS_HWMON,
1222 .driver = {
1223 .name = DRVNAME,
1224 },
1225 .detect = nct7802_detect,
1226 .probe_new = nct7802_probe,
1227 .id_table = nct7802_idtable,
1228 .address_list = nct7802_address_list,
1229 };
1230
1231 module_i2c_driver(nct7802_driver);
1232
1233 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
1234 MODULE_DESCRIPTION("NCT7802Y Hardware Monitoring Driver");
1235 MODULE_LICENSE("GPL v2");