Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * sis5595.c - Part of lm_sensors, Linux kernel modules
0004  *         for hardware monitoring
0005  *
0006  * Copyright (C) 1998 - 2001 Frodo Looijaard <frodol@dds.nl>,
0007  *               Kyösti Mälkki <kmalkki@cc.hut.fi>, and
0008  *               Mark D. Studebaker <mdsxyz123@yahoo.com>
0009  * Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
0010  * the help of Jean Delvare <jdelvare@suse.de>
0011  */
0012 
0013 /*
0014  * SiS southbridge has a LM78-like chip integrated on the same IC.
0015  * This driver is a customized copy of lm78.c
0016  *
0017  * Supports following revisions:
0018  *  Version     PCI ID      PCI Revision
0019  *  1       1039/0008   AF or less
0020  *  2       1039/0008   B0 or greater
0021  *
0022  *  Note: these chips contain a 0008 device which is incompatible with the
0023  *   5595. We recognize these by the presence of the listed
0024  *   "blacklist" PCI ID and refuse to load.
0025  *
0026  * NOT SUPPORTED    PCI ID      BLACKLIST PCI ID
0027  *   540        0008        0540
0028  *   550        0008        0550
0029  *  5513        0008        5511
0030  *  5581        0008        5597
0031  *  5582        0008        5597
0032  *  5597        0008        5597
0033  *  5598        0008        5597/5598
0034  *   630        0008        0630
0035  *   645        0008        0645
0036  *   730        0008        0730
0037  *   735        0008        0735
0038  */
0039 
0040 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0041 
0042 #include <linux/module.h>
0043 #include <linux/slab.h>
0044 #include <linux/ioport.h>
0045 #include <linux/pci.h>
0046 #include <linux/platform_device.h>
0047 #include <linux/hwmon.h>
0048 #include <linux/hwmon-sysfs.h>
0049 #include <linux/err.h>
0050 #include <linux/init.h>
0051 #include <linux/jiffies.h>
0052 #include <linux/mutex.h>
0053 #include <linux/sysfs.h>
0054 #include <linux/acpi.h>
0055 #include <linux/io.h>
0056 
0057 /*
0058  * If force_addr is set to anything different from 0, we forcibly enable
0059  * the device at the given address.
0060  */
0061 static u16 force_addr;
0062 module_param(force_addr, ushort, 0);
0063 MODULE_PARM_DESC(force_addr,
0064          "Initialize the base address of the sensors");
0065 
0066 static struct platform_device *pdev;
0067 
0068 /* Many SIS5595 constants specified below */
0069 
0070 /* Length of ISA address segment */
0071 #define SIS5595_EXTENT 8
0072 /* PCI Config Registers */
0073 #define SIS5595_BASE_REG 0x68
0074 #define SIS5595_PIN_REG 0x7A
0075 #define SIS5595_ENABLE_REG 0x7B
0076 
0077 /* Where are the ISA address/data registers relative to the base address */
0078 #define SIS5595_ADDR_REG_OFFSET 5
0079 #define SIS5595_DATA_REG_OFFSET 6
0080 
0081 /* The SIS5595 registers */
0082 #define SIS5595_REG_IN_MAX(nr) (0x2b + (nr) * 2)
0083 #define SIS5595_REG_IN_MIN(nr) (0x2c + (nr) * 2)
0084 #define SIS5595_REG_IN(nr) (0x20 + (nr))
0085 
0086 #define SIS5595_REG_FAN_MIN(nr) (0x3b + (nr))
0087 #define SIS5595_REG_FAN(nr) (0x28 + (nr))
0088 
0089 /*
0090  * On the first version of the chip, the temp registers are separate.
0091  * On the second version,
0092  * TEMP pin is shared with IN4, configured in PCI register 0x7A.
0093  * The registers are the same as well.
0094  * OVER and HYST are really MAX and MIN.
0095  */
0096 
0097 #define REV2MIN 0xb0
0098 #define SIS5595_REG_TEMP    (((data->revision) >= REV2MIN) ? \
0099                     SIS5595_REG_IN(4) : 0x27)
0100 #define SIS5595_REG_TEMP_OVER   (((data->revision) >= REV2MIN) ? \
0101                     SIS5595_REG_IN_MAX(4) : 0x39)
0102 #define SIS5595_REG_TEMP_HYST   (((data->revision) >= REV2MIN) ? \
0103                     SIS5595_REG_IN_MIN(4) : 0x3a)
0104 
0105 #define SIS5595_REG_CONFIG 0x40
0106 #define SIS5595_REG_ALARM1 0x41
0107 #define SIS5595_REG_ALARM2 0x42
0108 #define SIS5595_REG_FANDIV 0x47
0109 
0110 /*
0111  * Conversions. Limit checking is only done on the TO_REG
0112  * variants.
0113  */
0114 
0115 /*
0116  * IN: mV, (0V to 4.08V)
0117  * REG: 16mV/bit
0118  */
0119 static inline u8 IN_TO_REG(unsigned long val)
0120 {
0121     unsigned long nval = clamp_val(val, 0, 4080);
0122     return (nval + 8) / 16;
0123 }
0124 #define IN_FROM_REG(val) ((val) *  16)
0125 
0126 static inline u8 FAN_TO_REG(long rpm, int div)
0127 {
0128     if (rpm <= 0)
0129         return 255;
0130     if (rpm > 1350000)
0131         return 1;
0132     return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
0133 }
0134 
0135 static inline int FAN_FROM_REG(u8 val, int div)
0136 {
0137     return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
0138 }
0139 
0140 /*
0141  * TEMP: mC (-54.12C to +157.53C)
0142  * REG: 0.83C/bit + 52.12, two's complement
0143  */
0144 static inline int TEMP_FROM_REG(s8 val)
0145 {
0146     return val * 830 + 52120;
0147 }
0148 static inline s8 TEMP_TO_REG(long val)
0149 {
0150     int nval = clamp_val(val, -54120, 157530) ;
0151     return nval < 0 ? (nval - 5212 - 415) / 830 : (nval - 5212 + 415) / 830;
0152 }
0153 
0154 /*
0155  * FAN DIV: 1, 2, 4, or 8 (defaults to 2)
0156  * REG: 0, 1, 2, or 3 (respectively) (defaults to 1)
0157  */
0158 static inline u8 DIV_TO_REG(int val)
0159 {
0160     return val == 8 ? 3 : val == 4 ? 2 : val == 1 ? 0 : 1;
0161 }
0162 #define DIV_FROM_REG(val) (1 << (val))
0163 
0164 /*
0165  * For each registered chip, we need to keep some data in memory.
0166  * The structure is dynamically allocated.
0167  */
0168 struct sis5595_data {
0169     unsigned short addr;
0170     const char *name;
0171     struct device *hwmon_dev;
0172     struct mutex lock;
0173 
0174     struct mutex update_lock;
0175     bool valid;     /* true if following fields are valid */
0176     unsigned long last_updated; /* In jiffies */
0177     char maxins;        /* == 3 if temp enabled, otherwise == 4 */
0178     u8 revision;        /* Reg. value */
0179 
0180     u8 in[5];       /* Register value */
0181     u8 in_max[5];       /* Register value */
0182     u8 in_min[5];       /* Register value */
0183     u8 fan[2];      /* Register value */
0184     u8 fan_min[2];      /* Register value */
0185     s8 temp;        /* Register value */
0186     s8 temp_over;       /* Register value */
0187     s8 temp_hyst;       /* Register value */
0188     u8 fan_div[2];      /* Register encoding, shifted right */
0189     u16 alarms;     /* Register encoding, combined */
0190 };
0191 
0192 static struct pci_dev *s_bridge;    /* pointer to the (only) sis5595 */
0193 
0194 static int sis5595_probe(struct platform_device *pdev);
0195 static int sis5595_remove(struct platform_device *pdev);
0196 
0197 static int sis5595_read_value(struct sis5595_data *data, u8 reg);
0198 static void sis5595_write_value(struct sis5595_data *data, u8 reg, u8 value);
0199 static struct sis5595_data *sis5595_update_device(struct device *dev);
0200 static void sis5595_init_device(struct sis5595_data *data);
0201 
0202 static struct platform_driver sis5595_driver = {
0203     .driver = {
0204         .name   = "sis5595",
0205     },
0206     .probe      = sis5595_probe,
0207     .remove     = sis5595_remove,
0208 };
0209 
0210 /* 4 Voltages */
0211 static ssize_t in_show(struct device *dev, struct device_attribute *da,
0212                char *buf)
0213 {
0214     struct sis5595_data *data = sis5595_update_device(dev);
0215     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0216     int nr = attr->index;
0217     return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
0218 }
0219 
0220 static ssize_t in_min_show(struct device *dev, struct device_attribute *da,
0221                char *buf)
0222 {
0223     struct sis5595_data *data = sis5595_update_device(dev);
0224     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0225     int nr = attr->index;
0226     return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
0227 }
0228 
0229 static ssize_t in_max_show(struct device *dev, struct device_attribute *da,
0230                char *buf)
0231 {
0232     struct sis5595_data *data = sis5595_update_device(dev);
0233     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0234     int nr = attr->index;
0235     return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
0236 }
0237 
0238 static ssize_t in_min_store(struct device *dev, struct device_attribute *da,
0239                 const char *buf, size_t count)
0240 {
0241     struct sis5595_data *data = dev_get_drvdata(dev);
0242     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0243     int nr = attr->index;
0244     unsigned long val;
0245     int err;
0246 
0247     err = kstrtoul(buf, 10, &val);
0248     if (err)
0249         return err;
0250 
0251     mutex_lock(&data->update_lock);
0252     data->in_min[nr] = IN_TO_REG(val);
0253     sis5595_write_value(data, SIS5595_REG_IN_MIN(nr), data->in_min[nr]);
0254     mutex_unlock(&data->update_lock);
0255     return count;
0256 }
0257 
0258 static ssize_t in_max_store(struct device *dev, struct device_attribute *da,
0259                 const char *buf, size_t count)
0260 {
0261     struct sis5595_data *data = dev_get_drvdata(dev);
0262     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0263     int nr = attr->index;
0264     unsigned long val;
0265     int err;
0266 
0267     err = kstrtoul(buf, 10, &val);
0268     if (err)
0269         return err;
0270 
0271     mutex_lock(&data->update_lock);
0272     data->in_max[nr] = IN_TO_REG(val);
0273     sis5595_write_value(data, SIS5595_REG_IN_MAX(nr), data->in_max[nr]);
0274     mutex_unlock(&data->update_lock);
0275     return count;
0276 }
0277 
0278 static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
0279 static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
0280 static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
0281 static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
0282 static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
0283 static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
0284 static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
0285 static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
0286 static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
0287 static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
0288 static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
0289 static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
0290 static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
0291 static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
0292 static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
0293 
0294 /* Temperature */
0295 static ssize_t temp1_input_show(struct device *dev,
0296                 struct device_attribute *attr, char *buf)
0297 {
0298     struct sis5595_data *data = sis5595_update_device(dev);
0299     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
0300 }
0301 
0302 static ssize_t temp1_max_show(struct device *dev, struct device_attribute *attr,
0303                   char *buf)
0304 {
0305     struct sis5595_data *data = sis5595_update_device(dev);
0306     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
0307 }
0308 
0309 static ssize_t temp1_max_store(struct device *dev,
0310                    struct device_attribute *attr, const char *buf,
0311                    size_t count)
0312 {
0313     struct sis5595_data *data = dev_get_drvdata(dev);
0314     long val;
0315     int err;
0316 
0317     err = kstrtol(buf, 10, &val);
0318     if (err)
0319         return err;
0320 
0321     mutex_lock(&data->update_lock);
0322     data->temp_over = TEMP_TO_REG(val);
0323     sis5595_write_value(data, SIS5595_REG_TEMP_OVER, data->temp_over);
0324     mutex_unlock(&data->update_lock);
0325     return count;
0326 }
0327 
0328 static ssize_t temp1_max_hyst_show(struct device *dev,
0329                    struct device_attribute *attr, char *buf)
0330 {
0331     struct sis5595_data *data = sis5595_update_device(dev);
0332     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
0333 }
0334 
0335 static ssize_t temp1_max_hyst_store(struct device *dev,
0336                     struct device_attribute *attr,
0337                     const char *buf, size_t count)
0338 {
0339     struct sis5595_data *data = dev_get_drvdata(dev);
0340     long val;
0341     int err;
0342 
0343     err = kstrtol(buf, 10, &val);
0344     if (err)
0345         return err;
0346 
0347     mutex_lock(&data->update_lock);
0348     data->temp_hyst = TEMP_TO_REG(val);
0349     sis5595_write_value(data, SIS5595_REG_TEMP_HYST, data->temp_hyst);
0350     mutex_unlock(&data->update_lock);
0351     return count;
0352 }
0353 
0354 static DEVICE_ATTR_RO(temp1_input);
0355 static DEVICE_ATTR_RW(temp1_max);
0356 static DEVICE_ATTR_RW(temp1_max_hyst);
0357 
0358 /* 2 Fans */
0359 static ssize_t fan_show(struct device *dev, struct device_attribute *da,
0360             char *buf)
0361 {
0362     struct sis5595_data *data = sis5595_update_device(dev);
0363     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0364     int nr = attr->index;
0365     return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
0366         DIV_FROM_REG(data->fan_div[nr])));
0367 }
0368 
0369 static ssize_t fan_min_show(struct device *dev, struct device_attribute *da,
0370                 char *buf)
0371 {
0372     struct sis5595_data *data = sis5595_update_device(dev);
0373     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0374     int nr = attr->index;
0375     return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
0376         DIV_FROM_REG(data->fan_div[nr])));
0377 }
0378 
0379 static ssize_t fan_min_store(struct device *dev, struct device_attribute *da,
0380                  const char *buf, size_t count)
0381 {
0382     struct sis5595_data *data = dev_get_drvdata(dev);
0383     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0384     int nr = attr->index;
0385     unsigned long val;
0386     int err;
0387 
0388     err = kstrtoul(buf, 10, &val);
0389     if (err)
0390         return err;
0391 
0392     mutex_lock(&data->update_lock);
0393     data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
0394     sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
0395     mutex_unlock(&data->update_lock);
0396     return count;
0397 }
0398 
0399 static ssize_t fan_div_show(struct device *dev, struct device_attribute *da,
0400                 char *buf)
0401 {
0402     struct sis5595_data *data = sis5595_update_device(dev);
0403     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0404     int nr = attr->index;
0405     return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
0406 }
0407 
0408 /*
0409  * Note: we save and restore the fan minimum here, because its value is
0410  * determined in part by the fan divisor.  This follows the principle of
0411  * least surprise; the user doesn't expect the fan minimum to change just
0412  * because the divisor changed.
0413  */
0414 static ssize_t fan_div_store(struct device *dev, struct device_attribute *da,
0415                  const char *buf, size_t count)
0416 {
0417     struct sis5595_data *data = dev_get_drvdata(dev);
0418     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0419     int nr = attr->index;
0420     unsigned long min;
0421     int reg;
0422     unsigned long val;
0423     int err;
0424 
0425     err = kstrtoul(buf, 10, &val);
0426     if (err)
0427         return err;
0428 
0429     mutex_lock(&data->update_lock);
0430     min = FAN_FROM_REG(data->fan_min[nr],
0431             DIV_FROM_REG(data->fan_div[nr]));
0432     reg = sis5595_read_value(data, SIS5595_REG_FANDIV);
0433 
0434     switch (val) {
0435     case 1:
0436         data->fan_div[nr] = 0;
0437         break;
0438     case 2:
0439         data->fan_div[nr] = 1;
0440         break;
0441     case 4:
0442         data->fan_div[nr] = 2;
0443         break;
0444     case 8:
0445         data->fan_div[nr] = 3;
0446         break;
0447     default:
0448         dev_err(dev,
0449             "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
0450             val);
0451         mutex_unlock(&data->update_lock);
0452         return -EINVAL;
0453     }
0454 
0455     switch (nr) {
0456     case 0:
0457         reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
0458         break;
0459     case 1:
0460         reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
0461         break;
0462     }
0463     sis5595_write_value(data, SIS5595_REG_FANDIV, reg);
0464     data->fan_min[nr] =
0465         FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
0466     sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
0467     mutex_unlock(&data->update_lock);
0468     return count;
0469 }
0470 
0471 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
0472 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
0473 static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
0474 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
0475 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
0476 static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
0477 
0478 /* Alarms */
0479 static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
0480                char *buf)
0481 {
0482     struct sis5595_data *data = sis5595_update_device(dev);
0483     return sprintf(buf, "%d\n", data->alarms);
0484 }
0485 static DEVICE_ATTR_RO(alarms);
0486 
0487 static ssize_t alarm_show(struct device *dev, struct device_attribute *da,
0488               char *buf)
0489 {
0490     struct sis5595_data *data = sis5595_update_device(dev);
0491     int nr = to_sensor_dev_attr(da)->index;
0492     return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
0493 }
0494 static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
0495 static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
0496 static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
0497 static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
0498 static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 15);
0499 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
0500 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
0501 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 15);
0502 
0503 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
0504              char *buf)
0505 {
0506     struct sis5595_data *data = dev_get_drvdata(dev);
0507     return sprintf(buf, "%s\n", data->name);
0508 }
0509 static DEVICE_ATTR_RO(name);
0510 
0511 static struct attribute *sis5595_attributes[] = {
0512     &sensor_dev_attr_in0_input.dev_attr.attr,
0513     &sensor_dev_attr_in0_min.dev_attr.attr,
0514     &sensor_dev_attr_in0_max.dev_attr.attr,
0515     &sensor_dev_attr_in0_alarm.dev_attr.attr,
0516     &sensor_dev_attr_in1_input.dev_attr.attr,
0517     &sensor_dev_attr_in1_min.dev_attr.attr,
0518     &sensor_dev_attr_in1_max.dev_attr.attr,
0519     &sensor_dev_attr_in1_alarm.dev_attr.attr,
0520     &sensor_dev_attr_in2_input.dev_attr.attr,
0521     &sensor_dev_attr_in2_min.dev_attr.attr,
0522     &sensor_dev_attr_in2_max.dev_attr.attr,
0523     &sensor_dev_attr_in2_alarm.dev_attr.attr,
0524     &sensor_dev_attr_in3_input.dev_attr.attr,
0525     &sensor_dev_attr_in3_min.dev_attr.attr,
0526     &sensor_dev_attr_in3_max.dev_attr.attr,
0527     &sensor_dev_attr_in3_alarm.dev_attr.attr,
0528 
0529     &sensor_dev_attr_fan1_input.dev_attr.attr,
0530     &sensor_dev_attr_fan1_min.dev_attr.attr,
0531     &sensor_dev_attr_fan1_div.dev_attr.attr,
0532     &sensor_dev_attr_fan1_alarm.dev_attr.attr,
0533     &sensor_dev_attr_fan2_input.dev_attr.attr,
0534     &sensor_dev_attr_fan2_min.dev_attr.attr,
0535     &sensor_dev_attr_fan2_div.dev_attr.attr,
0536     &sensor_dev_attr_fan2_alarm.dev_attr.attr,
0537 
0538     &dev_attr_alarms.attr,
0539     &dev_attr_name.attr,
0540     NULL
0541 };
0542 
0543 static const struct attribute_group sis5595_group = {
0544     .attrs = sis5595_attributes,
0545 };
0546 
0547 static struct attribute *sis5595_attributes_in4[] = {
0548     &sensor_dev_attr_in4_input.dev_attr.attr,
0549     &sensor_dev_attr_in4_min.dev_attr.attr,
0550     &sensor_dev_attr_in4_max.dev_attr.attr,
0551     &sensor_dev_attr_in4_alarm.dev_attr.attr,
0552     NULL
0553 };
0554 
0555 static const struct attribute_group sis5595_group_in4 = {
0556     .attrs = sis5595_attributes_in4,
0557 };
0558 
0559 static struct attribute *sis5595_attributes_temp1[] = {
0560     &dev_attr_temp1_input.attr,
0561     &dev_attr_temp1_max.attr,
0562     &dev_attr_temp1_max_hyst.attr,
0563     &sensor_dev_attr_temp1_alarm.dev_attr.attr,
0564     NULL
0565 };
0566 
0567 static const struct attribute_group sis5595_group_temp1 = {
0568     .attrs = sis5595_attributes_temp1,
0569 };
0570 
0571 /* This is called when the module is loaded */
0572 static int sis5595_probe(struct platform_device *pdev)
0573 {
0574     int err = 0;
0575     int i;
0576     struct sis5595_data *data;
0577     struct resource *res;
0578     char val;
0579 
0580     /* Reserve the ISA region */
0581     res = platform_get_resource(pdev, IORESOURCE_IO, 0);
0582     if (!devm_request_region(&pdev->dev, res->start, SIS5595_EXTENT,
0583                  sis5595_driver.driver.name))
0584         return -EBUSY;
0585 
0586     data = devm_kzalloc(&pdev->dev, sizeof(struct sis5595_data),
0587                 GFP_KERNEL);
0588     if (!data)
0589         return -ENOMEM;
0590 
0591     mutex_init(&data->lock);
0592     mutex_init(&data->update_lock);
0593     data->addr = res->start;
0594     data->name = "sis5595";
0595     platform_set_drvdata(pdev, data);
0596 
0597     /*
0598      * Check revision and pin registers to determine whether 4 or 5 voltages
0599      */
0600     data->revision = s_bridge->revision;
0601     /* 4 voltages, 1 temp */
0602     data->maxins = 3;
0603     if (data->revision >= REV2MIN) {
0604         pci_read_config_byte(s_bridge, SIS5595_PIN_REG, &val);
0605         if (!(val & 0x80))
0606             /* 5 voltages, no temps */
0607             data->maxins = 4;
0608     }
0609 
0610     /* Initialize the SIS5595 chip */
0611     sis5595_init_device(data);
0612 
0613     /* A few vars need to be filled upon startup */
0614     for (i = 0; i < 2; i++) {
0615         data->fan_min[i] = sis5595_read_value(data,
0616                     SIS5595_REG_FAN_MIN(i));
0617     }
0618 
0619     /* Register sysfs hooks */
0620     err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group);
0621     if (err)
0622         return err;
0623     if (data->maxins == 4) {
0624         err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group_in4);
0625         if (err)
0626             goto exit_remove_files;
0627     } else {
0628         err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group_temp1);
0629         if (err)
0630             goto exit_remove_files;
0631     }
0632 
0633     data->hwmon_dev = hwmon_device_register(&pdev->dev);
0634     if (IS_ERR(data->hwmon_dev)) {
0635         err = PTR_ERR(data->hwmon_dev);
0636         goto exit_remove_files;
0637     }
0638 
0639     return 0;
0640 
0641 exit_remove_files:
0642     sysfs_remove_group(&pdev->dev.kobj, &sis5595_group);
0643     sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_in4);
0644     sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_temp1);
0645     return err;
0646 }
0647 
0648 static int sis5595_remove(struct platform_device *pdev)
0649 {
0650     struct sis5595_data *data = platform_get_drvdata(pdev);
0651 
0652     hwmon_device_unregister(data->hwmon_dev);
0653     sysfs_remove_group(&pdev->dev.kobj, &sis5595_group);
0654     sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_in4);
0655     sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_temp1);
0656 
0657     return 0;
0658 }
0659 
0660 /* ISA access must be locked explicitly. */
0661 static int sis5595_read_value(struct sis5595_data *data, u8 reg)
0662 {
0663     int res;
0664 
0665     mutex_lock(&data->lock);
0666     outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
0667     res = inb_p(data->addr + SIS5595_DATA_REG_OFFSET);
0668     mutex_unlock(&data->lock);
0669     return res;
0670 }
0671 
0672 static void sis5595_write_value(struct sis5595_data *data, u8 reg, u8 value)
0673 {
0674     mutex_lock(&data->lock);
0675     outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
0676     outb_p(value, data->addr + SIS5595_DATA_REG_OFFSET);
0677     mutex_unlock(&data->lock);
0678 }
0679 
0680 /* Called when we have found a new SIS5595. */
0681 static void sis5595_init_device(struct sis5595_data *data)
0682 {
0683     u8 config = sis5595_read_value(data, SIS5595_REG_CONFIG);
0684     if (!(config & 0x01))
0685         sis5595_write_value(data, SIS5595_REG_CONFIG,
0686                 (config & 0xf7) | 0x01);
0687 }
0688 
0689 static struct sis5595_data *sis5595_update_device(struct device *dev)
0690 {
0691     struct sis5595_data *data = dev_get_drvdata(dev);
0692     int i;
0693 
0694     mutex_lock(&data->update_lock);
0695 
0696     if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
0697         || !data->valid) {
0698 
0699         for (i = 0; i <= data->maxins; i++) {
0700             data->in[i] =
0701                 sis5595_read_value(data, SIS5595_REG_IN(i));
0702             data->in_min[i] =
0703                 sis5595_read_value(data,
0704                            SIS5595_REG_IN_MIN(i));
0705             data->in_max[i] =
0706                 sis5595_read_value(data,
0707                            SIS5595_REG_IN_MAX(i));
0708         }
0709         for (i = 0; i < 2; i++) {
0710             data->fan[i] =
0711                 sis5595_read_value(data, SIS5595_REG_FAN(i));
0712             data->fan_min[i] =
0713                 sis5595_read_value(data,
0714                            SIS5595_REG_FAN_MIN(i));
0715         }
0716         if (data->maxins == 3) {
0717             data->temp =
0718                 sis5595_read_value(data, SIS5595_REG_TEMP);
0719             data->temp_over =
0720                 sis5595_read_value(data, SIS5595_REG_TEMP_OVER);
0721             data->temp_hyst =
0722                 sis5595_read_value(data, SIS5595_REG_TEMP_HYST);
0723         }
0724         i = sis5595_read_value(data, SIS5595_REG_FANDIV);
0725         data->fan_div[0] = (i >> 4) & 0x03;
0726         data->fan_div[1] = i >> 6;
0727         data->alarms =
0728             sis5595_read_value(data, SIS5595_REG_ALARM1) |
0729             (sis5595_read_value(data, SIS5595_REG_ALARM2) << 8);
0730         data->last_updated = jiffies;
0731         data->valid = true;
0732     }
0733 
0734     mutex_unlock(&data->update_lock);
0735 
0736     return data;
0737 }
0738 
0739 static const struct pci_device_id sis5595_pci_ids[] = {
0740     { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
0741     { 0, }
0742 };
0743 
0744 MODULE_DEVICE_TABLE(pci, sis5595_pci_ids);
0745 
0746 static int blacklist[] = {
0747     PCI_DEVICE_ID_SI_540,
0748     PCI_DEVICE_ID_SI_550,
0749     PCI_DEVICE_ID_SI_630,
0750     PCI_DEVICE_ID_SI_645,
0751     PCI_DEVICE_ID_SI_730,
0752     PCI_DEVICE_ID_SI_735,
0753     PCI_DEVICE_ID_SI_5511, /*
0754                 * 5513 chip has the 0008 device but
0755                 * that ID shows up in other chips so we
0756                 * use the 5511 ID for recognition
0757                 */
0758     PCI_DEVICE_ID_SI_5597,
0759     PCI_DEVICE_ID_SI_5598,
0760     0 };
0761 
0762 static int sis5595_device_add(unsigned short address)
0763 {
0764     struct resource res = {
0765         .start  = address,
0766         .end    = address + SIS5595_EXTENT - 1,
0767         .name   = "sis5595",
0768         .flags  = IORESOURCE_IO,
0769     };
0770     int err;
0771 
0772     err = acpi_check_resource_conflict(&res);
0773     if (err)
0774         goto exit;
0775 
0776     pdev = platform_device_alloc("sis5595", address);
0777     if (!pdev) {
0778         err = -ENOMEM;
0779         pr_err("Device allocation failed\n");
0780         goto exit;
0781     }
0782 
0783     err = platform_device_add_resources(pdev, &res, 1);
0784     if (err) {
0785         pr_err("Device resource addition failed (%d)\n", err);
0786         goto exit_device_put;
0787     }
0788 
0789     err = platform_device_add(pdev);
0790     if (err) {
0791         pr_err("Device addition failed (%d)\n", err);
0792         goto exit_device_put;
0793     }
0794 
0795     return 0;
0796 
0797 exit_device_put:
0798     platform_device_put(pdev);
0799 exit:
0800     return err;
0801 }
0802 
0803 static int sis5595_pci_probe(struct pci_dev *dev,
0804                        const struct pci_device_id *id)
0805 {
0806     u16 address;
0807     u8 enable;
0808     int *i;
0809 
0810     for (i = blacklist; *i != 0; i++) {
0811         struct pci_dev *d;
0812         d = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
0813         if (d) {
0814             dev_err(&d->dev,
0815                 "Looked for SIS5595 but found unsupported device %.4x\n",
0816                 *i);
0817             pci_dev_put(d);
0818             return -ENODEV;
0819         }
0820     }
0821 
0822     force_addr &= ~(SIS5595_EXTENT - 1);
0823     if (force_addr) {
0824         dev_warn(&dev->dev, "Forcing ISA address 0x%x\n", force_addr);
0825         pci_write_config_word(dev, SIS5595_BASE_REG, force_addr);
0826     }
0827 
0828     if (PCIBIOS_SUCCESSFUL !=
0829         pci_read_config_word(dev, SIS5595_BASE_REG, &address)) {
0830         dev_err(&dev->dev, "Failed to read ISA address\n");
0831         return -ENODEV;
0832     }
0833 
0834     address &= ~(SIS5595_EXTENT - 1);
0835     if (!address) {
0836         dev_err(&dev->dev,
0837             "Base address not set - upgrade BIOS or use force_addr=0xaddr\n");
0838         return -ENODEV;
0839     }
0840     if (force_addr && address != force_addr) {
0841         /* doesn't work for some chips? */
0842         dev_err(&dev->dev, "Failed to force ISA address\n");
0843         return -ENODEV;
0844     }
0845 
0846     if (PCIBIOS_SUCCESSFUL !=
0847         pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable)) {
0848         dev_err(&dev->dev, "Failed to read enable register\n");
0849         return -ENODEV;
0850     }
0851     if (!(enable & 0x80)) {
0852         if ((PCIBIOS_SUCCESSFUL !=
0853              pci_write_config_byte(dev, SIS5595_ENABLE_REG,
0854                        enable | 0x80))
0855          || (PCIBIOS_SUCCESSFUL !=
0856              pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable))
0857          || (!(enable & 0x80))) {
0858             /* doesn't work for some chips! */
0859             dev_err(&dev->dev, "Failed to enable HWM device\n");
0860             return -ENODEV;
0861         }
0862     }
0863 
0864     if (platform_driver_register(&sis5595_driver)) {
0865         dev_dbg(&dev->dev, "Failed to register sis5595 driver\n");
0866         goto exit;
0867     }
0868 
0869     s_bridge = pci_dev_get(dev);
0870     /* Sets global pdev as a side effect */
0871     if (sis5595_device_add(address))
0872         goto exit_unregister;
0873 
0874     /*
0875      * Always return failure here.  This is to allow other drivers to bind
0876      * to this pci device.  We don't really want to have control over the
0877      * pci device, we only wanted to read as few register values from it.
0878      */
0879     return -ENODEV;
0880 
0881 exit_unregister:
0882     pci_dev_put(dev);
0883     platform_driver_unregister(&sis5595_driver);
0884 exit:
0885     return -ENODEV;
0886 }
0887 
0888 static struct pci_driver sis5595_pci_driver = {
0889     .name            = "sis5595",
0890     .id_table        = sis5595_pci_ids,
0891     .probe           = sis5595_pci_probe,
0892 };
0893 
0894 static int __init sm_sis5595_init(void)
0895 {
0896     return pci_register_driver(&sis5595_pci_driver);
0897 }
0898 
0899 static void __exit sm_sis5595_exit(void)
0900 {
0901     pci_unregister_driver(&sis5595_pci_driver);
0902     if (s_bridge != NULL) {
0903         platform_device_unregister(pdev);
0904         platform_driver_unregister(&sis5595_driver);
0905         pci_dev_put(s_bridge);
0906         s_bridge = NULL;
0907     }
0908 }
0909 
0910 MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
0911 MODULE_DESCRIPTION("SiS 5595 Sensor device");
0912 MODULE_LICENSE("GPL");
0913 
0914 module_init(sm_sis5595_init);
0915 module_exit(sm_sis5595_exit);