Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  pc87360.c - Part of lm_sensors, Linux kernel modules
0004  *              for hardware monitoring
0005  *  Copyright (C) 2004, 2007 Jean Delvare <jdelvare@suse.de>
0006  *
0007  *  Copied from smsc47m1.c:
0008  *  Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
0009  *
0010  *  Supports the following chips:
0011  *
0012  *  Chip        #vin    #fan    #pwm    #temp   devid
0013  *  PC87360     -       2       2       -       0xE1
0014  *  PC87363     -       2       2       -       0xE8
0015  *  PC87364     -       3       3       -       0xE4
0016  *  PC87365     11      3       3       2       0xE5
0017  *  PC87366     11      3       3       3-4     0xE9
0018  *
0019  *  This driver assumes that no more than one chip is present, and one of
0020  *  the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
0021  */
0022 
0023 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0024 
0025 #include <linux/module.h>
0026 #include <linux/init.h>
0027 #include <linux/slab.h>
0028 #include <linux/jiffies.h>
0029 #include <linux/platform_device.h>
0030 #include <linux/hwmon.h>
0031 #include <linux/hwmon-sysfs.h>
0032 #include <linux/hwmon-vid.h>
0033 #include <linux/err.h>
0034 #include <linux/mutex.h>
0035 #include <linux/acpi.h>
0036 #include <linux/io.h>
0037 
0038 static u8 devid;
0039 static struct platform_device *pdev;
0040 static unsigned short extra_isa[3];
0041 static u8 confreg[4];
0042 
0043 static int init = 1;
0044 module_param(init, int, 0);
0045 MODULE_PARM_DESC(init,
0046 "Chip initialization level:\n"
0047 " 0: None\n"
0048 "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
0049 " 2: Forcibly enable all voltage and temperature channels, except in9\n"
0050 " 3: Forcibly enable all voltage and temperature channels, including in9");
0051 
0052 static unsigned short force_id;
0053 module_param(force_id, ushort, 0);
0054 MODULE_PARM_DESC(force_id, "Override the detected device ID");
0055 
0056 /*
0057  * Super-I/O registers and operations
0058  */
0059 
0060 #define DEV 0x07    /* Register: Logical device select */
0061 #define DEVID   0x20    /* Register: Device ID */
0062 #define ACT 0x30    /* Register: Device activation */
0063 #define BASE    0x60    /* Register: Base address */
0064 
0065 #define FSCM    0x09    /* Logical device: fans */
0066 #define VLM 0x0d    /* Logical device: voltages */
0067 #define TMS 0x0e    /* Logical device: temperatures */
0068 #define LDNI_MAX 3
0069 static const u8 logdev[LDNI_MAX] = { FSCM, VLM, TMS };
0070 
0071 #define LD_FAN      0
0072 #define LD_IN       1
0073 #define LD_TEMP     2
0074 
0075 static inline void superio_outb(int sioaddr, int reg, int val)
0076 {
0077     outb(reg, sioaddr);
0078     outb(val, sioaddr + 1);
0079 }
0080 
0081 static inline int superio_inb(int sioaddr, int reg)
0082 {
0083     outb(reg, sioaddr);
0084     return inb(sioaddr + 1);
0085 }
0086 
0087 static inline void superio_exit(int sioaddr)
0088 {
0089     outb(0x02, sioaddr);
0090     outb(0x02, sioaddr + 1);
0091 }
0092 
0093 /*
0094  * Logical devices
0095  */
0096 
0097 #define PC87360_EXTENT      0x10
0098 #define PC87365_REG_BANK    0x09
0099 #define NO_BANK         0xff
0100 
0101 /*
0102  * Fan registers and conversions
0103  */
0104 
0105 /* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
0106 #define PC87360_REG_PRESCALE(nr)    (0x00 + 2 * (nr))
0107 #define PC87360_REG_PWM(nr)     (0x01 + 2 * (nr))
0108 #define PC87360_REG_FAN_MIN(nr)     (0x06 + 3 * (nr))
0109 #define PC87360_REG_FAN(nr)     (0x07 + 3 * (nr))
0110 #define PC87360_REG_FAN_STATUS(nr)  (0x08 + 3 * (nr))
0111 
0112 #define FAN_FROM_REG(val, div)      ((val) == 0 ? 0 : \
0113                      480000 / ((val) * (div)))
0114 #define FAN_TO_REG(val, div)        ((val) <= 100 ? 0 : \
0115                      480000 / ((val) * (div)))
0116 #define FAN_DIV_FROM_REG(val)       (1 << (((val) >> 5) & 0x03))
0117 #define FAN_STATUS_FROM_REG(val)    ((val) & 0x07)
0118 
0119 #define FAN_CONFIG_MONITOR(val, nr) (((val) >> (2 + (nr) * 3)) & 1)
0120 #define FAN_CONFIG_CONTROL(val, nr) (((val) >> (3 + (nr) * 3)) & 1)
0121 #define FAN_CONFIG_INVERT(val, nr)  (((val) >> (4 + (nr) * 3)) & 1)
0122 
0123 #define PWM_FROM_REG(val, inv)      ((inv) ? 255 - (val) : (val))
0124 static inline u8 PWM_TO_REG(int val, int inv)
0125 {
0126     if (inv)
0127         val = 255 - val;
0128     if (val < 0)
0129         return 0;
0130     if (val > 255)
0131         return 255;
0132     return val;
0133 }
0134 
0135 /*
0136  * Voltage registers and conversions
0137  */
0138 
0139 #define PC87365_REG_IN_CONVRATE     0x07
0140 #define PC87365_REG_IN_CONFIG       0x08
0141 #define PC87365_REG_IN          0x0B
0142 #define PC87365_REG_IN_MIN      0x0D
0143 #define PC87365_REG_IN_MAX      0x0C
0144 #define PC87365_REG_IN_STATUS       0x0A
0145 #define PC87365_REG_IN_ALARMS1      0x00
0146 #define PC87365_REG_IN_ALARMS2      0x01
0147 #define PC87365_REG_VID         0x06
0148 
0149 #define IN_FROM_REG(val, ref)       (((val) * (ref) + 128) / 256)
0150 #define IN_TO_REG(val, ref)     ((val) < 0 ? 0 : \
0151                      (val) * 256 >= (ref) * 255 ? 255 : \
0152                      ((val) * 256 + (ref) / 2) / (ref))
0153 
0154 /*
0155  * Temperature registers and conversions
0156  */
0157 
0158 #define PC87365_REG_TEMP_CONFIG     0x08
0159 #define PC87365_REG_TEMP        0x0B
0160 #define PC87365_REG_TEMP_MIN        0x0D
0161 #define PC87365_REG_TEMP_MAX        0x0C
0162 #define PC87365_REG_TEMP_CRIT       0x0E
0163 #define PC87365_REG_TEMP_STATUS     0x0A
0164 #define PC87365_REG_TEMP_ALARMS     0x00
0165 
0166 #define TEMP_FROM_REG(val)      ((val) * 1000)
0167 #define TEMP_TO_REG(val)        ((val) < -55000 ? -55 : \
0168                      (val) > 127000 ? 127 : \
0169                      (val) < 0 ? ((val) - 500) / 1000 : \
0170                      ((val) + 500) / 1000)
0171 
0172 /*
0173  * Device data
0174  */
0175 
0176 struct pc87360_data {
0177     const char *name;
0178     struct device *hwmon_dev;
0179     struct mutex lock;
0180     struct mutex update_lock;
0181     bool valid;     /* true if following fields are valid */
0182     unsigned long last_updated; /* In jiffies */
0183 
0184     int address[3];
0185 
0186     u8 fannr, innr, tempnr;
0187 
0188     u8 fan[3];      /* Register value */
0189     u8 fan_min[3];      /* Register value */
0190     u8 fan_status[3];   /* Register value */
0191     u8 pwm[3];      /* Register value */
0192     u16 fan_conf;       /* Configuration register values, combined */
0193 
0194     u16 in_vref;        /* 1 mV/bit */
0195     u8 in[14];      /* Register value */
0196     u8 in_min[14];      /* Register value */
0197     u8 in_max[14];      /* Register value */
0198     u8 in_crit[3];      /* Register value */
0199     u8 in_status[14];   /* Register value */
0200     u16 in_alarms;      /* Register values, combined, masked */
0201     u8 vid_conf;        /* Configuration register value */
0202     u8 vrm;
0203     u8 vid;         /* Register value */
0204 
0205     s8 temp[3];     /* Register value */
0206     s8 temp_min[3];     /* Register value */
0207     s8 temp_max[3];     /* Register value */
0208     s8 temp_crit[3];    /* Register value */
0209     u8 temp_status[3];  /* Register value */
0210     u8 temp_alarms;     /* Register value, masked */
0211 };
0212 
0213 /*
0214  * Functions declaration
0215  */
0216 
0217 static int pc87360_probe(struct platform_device *pdev);
0218 static int pc87360_remove(struct platform_device *pdev);
0219 
0220 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
0221                   u8 reg);
0222 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
0223                 u8 reg, u8 value);
0224 static void pc87360_init_device(struct platform_device *pdev,
0225                 int use_thermistors);
0226 static struct pc87360_data *pc87360_update_device(struct device *dev);
0227 
0228 /*
0229  * Driver data
0230  */
0231 
0232 static struct platform_driver pc87360_driver = {
0233     .driver = {
0234         .name   = "pc87360",
0235     },
0236     .probe      = pc87360_probe,
0237     .remove     = pc87360_remove,
0238 };
0239 
0240 /*
0241  * Sysfs stuff
0242  */
0243 
0244 static ssize_t fan_input_show(struct device *dev,
0245                   struct device_attribute *devattr, char *buf)
0246 {
0247     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0248     struct pc87360_data *data = pc87360_update_device(dev);
0249     return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
0250                FAN_DIV_FROM_REG(data->fan_status[attr->index])));
0251 }
0252 static ssize_t fan_min_show(struct device *dev,
0253                 struct device_attribute *devattr, char *buf)
0254 {
0255     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0256     struct pc87360_data *data = pc87360_update_device(dev);
0257     return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
0258                FAN_DIV_FROM_REG(data->fan_status[attr->index])));
0259 }
0260 static ssize_t fan_div_show(struct device *dev,
0261                 struct device_attribute *devattr, char *buf)
0262 {
0263     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0264     struct pc87360_data *data = pc87360_update_device(dev);
0265     return sprintf(buf, "%u\n",
0266                FAN_DIV_FROM_REG(data->fan_status[attr->index]));
0267 }
0268 static ssize_t fan_status_show(struct device *dev,
0269                    struct device_attribute *devattr, char *buf)
0270 {
0271     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0272     struct pc87360_data *data = pc87360_update_device(dev);
0273     return sprintf(buf, "%u\n",
0274                FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
0275 }
0276 static ssize_t fan_min_store(struct device *dev,
0277                  struct device_attribute *devattr,
0278                  const char *buf, size_t count)
0279 {
0280     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0281     struct pc87360_data *data = dev_get_drvdata(dev);
0282     long fan_min;
0283     int err;
0284 
0285     err = kstrtol(buf, 10, &fan_min);
0286     if (err)
0287         return err;
0288 
0289     mutex_lock(&data->update_lock);
0290     fan_min = FAN_TO_REG(fan_min,
0291                  FAN_DIV_FROM_REG(data->fan_status[attr->index]));
0292 
0293     /* If it wouldn't fit, change clock divisor */
0294     while (fan_min > 255
0295         && (data->fan_status[attr->index] & 0x60) != 0x60) {
0296         fan_min >>= 1;
0297         data->fan[attr->index] >>= 1;
0298         data->fan_status[attr->index] += 0x20;
0299     }
0300     data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
0301     pc87360_write_value(data, LD_FAN, NO_BANK,
0302                 PC87360_REG_FAN_MIN(attr->index),
0303                 data->fan_min[attr->index]);
0304 
0305     /* Write new divider, preserve alarm bits */
0306     pc87360_write_value(data, LD_FAN, NO_BANK,
0307                 PC87360_REG_FAN_STATUS(attr->index),
0308                 data->fan_status[attr->index] & 0xF9);
0309     mutex_unlock(&data->update_lock);
0310 
0311     return count;
0312 }
0313 
0314 static struct sensor_device_attribute fan_input[] = {
0315     SENSOR_ATTR_RO(fan1_input, fan_input, 0),
0316     SENSOR_ATTR_RO(fan2_input, fan_input, 1),
0317     SENSOR_ATTR_RO(fan3_input, fan_input, 2),
0318 };
0319 static struct sensor_device_attribute fan_status[] = {
0320     SENSOR_ATTR_RO(fan1_status, fan_status, 0),
0321     SENSOR_ATTR_RO(fan2_status, fan_status, 1),
0322     SENSOR_ATTR_RO(fan3_status, fan_status, 2),
0323 };
0324 static struct sensor_device_attribute fan_div[] = {
0325     SENSOR_ATTR_RO(fan1_div, fan_div, 0),
0326     SENSOR_ATTR_RO(fan2_div, fan_div, 1),
0327     SENSOR_ATTR_RO(fan3_div, fan_div, 2),
0328 };
0329 static struct sensor_device_attribute fan_min[] = {
0330     SENSOR_ATTR_RW(fan1_min, fan_min, 0),
0331     SENSOR_ATTR_RW(fan2_min, fan_min, 1),
0332     SENSOR_ATTR_RW(fan3_min, fan_min, 2),
0333 };
0334 
0335 #define FAN_UNIT_ATTRS(X)       \
0336 {   &fan_input[X].dev_attr.attr,    \
0337     &fan_status[X].dev_attr.attr,   \
0338     &fan_div[X].dev_attr.attr,  \
0339     &fan_min[X].dev_attr.attr,  \
0340     NULL                \
0341 }
0342 
0343 static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
0344             char *buf)
0345 {
0346     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0347     struct pc87360_data *data = pc87360_update_device(dev);
0348     return sprintf(buf, "%u\n",
0349                PWM_FROM_REG(data->pwm[attr->index],
0350                     FAN_CONFIG_INVERT(data->fan_conf,
0351                               attr->index)));
0352 }
0353 static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
0354              const char *buf, size_t count)
0355 {
0356     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0357     struct pc87360_data *data = dev_get_drvdata(dev);
0358     long val;
0359     int err;
0360 
0361     err = kstrtol(buf, 10, &val);
0362     if (err)
0363         return err;
0364 
0365     mutex_lock(&data->update_lock);
0366     data->pwm[attr->index] = PWM_TO_REG(val,
0367                   FAN_CONFIG_INVERT(data->fan_conf, attr->index));
0368     pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
0369                 data->pwm[attr->index]);
0370     mutex_unlock(&data->update_lock);
0371     return count;
0372 }
0373 
0374 static struct sensor_device_attribute pwm[] = {
0375     SENSOR_ATTR_RW(pwm1, pwm, 0),
0376     SENSOR_ATTR_RW(pwm2, pwm, 1),
0377     SENSOR_ATTR_RW(pwm3, pwm, 2),
0378 };
0379 
0380 static struct attribute *pc8736x_fan_attr[][5] = {
0381     FAN_UNIT_ATTRS(0),
0382     FAN_UNIT_ATTRS(1),
0383     FAN_UNIT_ATTRS(2)
0384 };
0385 
0386 static const struct attribute_group pc8736x_fan_attr_group[] = {
0387     { .attrs = pc8736x_fan_attr[0], },
0388     { .attrs = pc8736x_fan_attr[1], },
0389     { .attrs = pc8736x_fan_attr[2], },
0390 };
0391 
0392 static ssize_t in_input_show(struct device *dev,
0393                  struct device_attribute *devattr, char *buf)
0394 {
0395     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0396     struct pc87360_data *data = pc87360_update_device(dev);
0397     return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
0398                data->in_vref));
0399 }
0400 static ssize_t in_min_show(struct device *dev,
0401                struct device_attribute *devattr, char *buf)
0402 {
0403     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0404     struct pc87360_data *data = pc87360_update_device(dev);
0405     return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
0406                data->in_vref));
0407 }
0408 static ssize_t in_max_show(struct device *dev,
0409                struct device_attribute *devattr, char *buf)
0410 {
0411     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0412     struct pc87360_data *data = pc87360_update_device(dev);
0413     return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
0414                data->in_vref));
0415 }
0416 static ssize_t in_status_show(struct device *dev,
0417                   struct device_attribute *devattr, char *buf)
0418 {
0419     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0420     struct pc87360_data *data = pc87360_update_device(dev);
0421     return sprintf(buf, "%u\n", data->in_status[attr->index]);
0422 }
0423 static ssize_t in_min_store(struct device *dev,
0424                 struct device_attribute *devattr, const char *buf,
0425                 size_t count)
0426 {
0427     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0428     struct pc87360_data *data = dev_get_drvdata(dev);
0429     long val;
0430     int err;
0431 
0432     err = kstrtol(buf, 10, &val);
0433     if (err)
0434         return err;
0435 
0436     mutex_lock(&data->update_lock);
0437     data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
0438     pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
0439                 data->in_min[attr->index]);
0440     mutex_unlock(&data->update_lock);
0441     return count;
0442 }
0443 static ssize_t in_max_store(struct device *dev,
0444                 struct device_attribute *devattr, const char *buf,
0445                 size_t count)
0446 {
0447     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0448     struct pc87360_data *data = dev_get_drvdata(dev);
0449     long val;
0450     int err;
0451 
0452     err = kstrtol(buf, 10, &val);
0453     if (err)
0454         return err;
0455 
0456     mutex_lock(&data->update_lock);
0457     data->in_max[attr->index] = IN_TO_REG(val,
0458                    data->in_vref);
0459     pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
0460                 data->in_max[attr->index]);
0461     mutex_unlock(&data->update_lock);
0462     return count;
0463 }
0464 
0465 static struct sensor_device_attribute in_input[] = {
0466     SENSOR_ATTR_RO(in0_input, in_input, 0),
0467     SENSOR_ATTR_RO(in1_input, in_input, 1),
0468     SENSOR_ATTR_RO(in2_input, in_input, 2),
0469     SENSOR_ATTR_RO(in3_input, in_input, 3),
0470     SENSOR_ATTR_RO(in4_input, in_input, 4),
0471     SENSOR_ATTR_RO(in5_input, in_input, 5),
0472     SENSOR_ATTR_RO(in6_input, in_input, 6),
0473     SENSOR_ATTR_RO(in7_input, in_input, 7),
0474     SENSOR_ATTR_RO(in8_input, in_input, 8),
0475     SENSOR_ATTR_RO(in9_input, in_input, 9),
0476     SENSOR_ATTR_RO(in10_input, in_input, 10),
0477 };
0478 static struct sensor_device_attribute in_status[] = {
0479     SENSOR_ATTR_RO(in0_status, in_status, 0),
0480     SENSOR_ATTR_RO(in1_status, in_status, 1),
0481     SENSOR_ATTR_RO(in2_status, in_status, 2),
0482     SENSOR_ATTR_RO(in3_status, in_status, 3),
0483     SENSOR_ATTR_RO(in4_status, in_status, 4),
0484     SENSOR_ATTR_RO(in5_status, in_status, 5),
0485     SENSOR_ATTR_RO(in6_status, in_status, 6),
0486     SENSOR_ATTR_RO(in7_status, in_status, 7),
0487     SENSOR_ATTR_RO(in8_status, in_status, 8),
0488     SENSOR_ATTR_RO(in9_status, in_status, 9),
0489     SENSOR_ATTR_RO(in10_status, in_status, 10),
0490 };
0491 static struct sensor_device_attribute in_min[] = {
0492     SENSOR_ATTR_RW(in0_min, in_min, 0),
0493     SENSOR_ATTR_RW(in1_min, in_min, 1),
0494     SENSOR_ATTR_RW(in2_min, in_min, 2),
0495     SENSOR_ATTR_RW(in3_min, in_min, 3),
0496     SENSOR_ATTR_RW(in4_min, in_min, 4),
0497     SENSOR_ATTR_RW(in5_min, in_min, 5),
0498     SENSOR_ATTR_RW(in6_min, in_min, 6),
0499     SENSOR_ATTR_RW(in7_min, in_min, 7),
0500     SENSOR_ATTR_RW(in8_min, in_min, 8),
0501     SENSOR_ATTR_RW(in9_min, in_min, 9),
0502     SENSOR_ATTR_RW(in10_min, in_min, 10),
0503 };
0504 static struct sensor_device_attribute in_max[] = {
0505     SENSOR_ATTR_RW(in0_max, in_max, 0),
0506     SENSOR_ATTR_RW(in1_max, in_max, 1),
0507     SENSOR_ATTR_RW(in2_max, in_max, 2),
0508     SENSOR_ATTR_RW(in3_max, in_max, 3),
0509     SENSOR_ATTR_RW(in4_max, in_max, 4),
0510     SENSOR_ATTR_RW(in5_max, in_max, 5),
0511     SENSOR_ATTR_RW(in6_max, in_max, 6),
0512     SENSOR_ATTR_RW(in7_max, in_max, 7),
0513     SENSOR_ATTR_RW(in8_max, in_max, 8),
0514     SENSOR_ATTR_RW(in9_max, in_max, 9),
0515     SENSOR_ATTR_RW(in10_max, in_max, 10),
0516 };
0517 
0518 /* (temp & vin) channel status register alarm bits (pdf sec.11.5.12) */
0519 #define CHAN_ALM_MIN    0x02    /* min limit crossed */
0520 #define CHAN_ALM_MAX    0x04    /* max limit exceeded */
0521 #define TEMP_ALM_CRIT   0x08    /* temp crit exceeded (temp only) */
0522 
0523 /*
0524  * show_in_min/max_alarm() reads data from the per-channel status
0525  * register (sec 11.5.12), not the vin event status registers (sec
0526  * 11.5.2) that (legacy) show_in_alarm() resds (via data->in_alarms)
0527  */
0528 
0529 static ssize_t in_min_alarm_show(struct device *dev,
0530                  struct device_attribute *devattr, char *buf)
0531 {
0532     struct pc87360_data *data = pc87360_update_device(dev);
0533     unsigned nr = to_sensor_dev_attr(devattr)->index;
0534 
0535     return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
0536 }
0537 static ssize_t in_max_alarm_show(struct device *dev,
0538                  struct device_attribute *devattr, char *buf)
0539 {
0540     struct pc87360_data *data = pc87360_update_device(dev);
0541     unsigned nr = to_sensor_dev_attr(devattr)->index;
0542 
0543     return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
0544 }
0545 
0546 static struct sensor_device_attribute in_min_alarm[] = {
0547     SENSOR_ATTR_RO(in0_min_alarm, in_min_alarm, 0),
0548     SENSOR_ATTR_RO(in1_min_alarm, in_min_alarm, 1),
0549     SENSOR_ATTR_RO(in2_min_alarm, in_min_alarm, 2),
0550     SENSOR_ATTR_RO(in3_min_alarm, in_min_alarm, 3),
0551     SENSOR_ATTR_RO(in4_min_alarm, in_min_alarm, 4),
0552     SENSOR_ATTR_RO(in5_min_alarm, in_min_alarm, 5),
0553     SENSOR_ATTR_RO(in6_min_alarm, in_min_alarm, 6),
0554     SENSOR_ATTR_RO(in7_min_alarm, in_min_alarm, 7),
0555     SENSOR_ATTR_RO(in8_min_alarm, in_min_alarm, 8),
0556     SENSOR_ATTR_RO(in9_min_alarm, in_min_alarm, 9),
0557     SENSOR_ATTR_RO(in10_min_alarm, in_min_alarm, 10),
0558 };
0559 static struct sensor_device_attribute in_max_alarm[] = {
0560     SENSOR_ATTR_RO(in0_max_alarm, in_max_alarm, 0),
0561     SENSOR_ATTR_RO(in1_max_alarm, in_max_alarm, 1),
0562     SENSOR_ATTR_RO(in2_max_alarm, in_max_alarm, 2),
0563     SENSOR_ATTR_RO(in3_max_alarm, in_max_alarm, 3),
0564     SENSOR_ATTR_RO(in4_max_alarm, in_max_alarm, 4),
0565     SENSOR_ATTR_RO(in5_max_alarm, in_max_alarm, 5),
0566     SENSOR_ATTR_RO(in6_max_alarm, in_max_alarm, 6),
0567     SENSOR_ATTR_RO(in7_max_alarm, in_max_alarm, 7),
0568     SENSOR_ATTR_RO(in8_max_alarm, in_max_alarm, 8),
0569     SENSOR_ATTR_RO(in9_max_alarm, in_max_alarm, 9),
0570     SENSOR_ATTR_RO(in10_max_alarm, in_max_alarm, 10),
0571 };
0572 
0573 #define VIN_UNIT_ATTRS(X) \
0574     &in_input[X].dev_attr.attr, \
0575     &in_status[X].dev_attr.attr,    \
0576     &in_min[X].dev_attr.attr,   \
0577     &in_max[X].dev_attr.attr,   \
0578     &in_min_alarm[X].dev_attr.attr, \
0579     &in_max_alarm[X].dev_attr.attr
0580 
0581 static ssize_t cpu0_vid_show(struct device *dev,
0582                  struct device_attribute *attr, char *buf)
0583 {
0584     struct pc87360_data *data = pc87360_update_device(dev);
0585     return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
0586 }
0587 static DEVICE_ATTR_RO(cpu0_vid);
0588 
0589 static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
0590             char *buf)
0591 {
0592     struct pc87360_data *data = dev_get_drvdata(dev);
0593     return sprintf(buf, "%u\n", data->vrm);
0594 }
0595 static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
0596              const char *buf, size_t count)
0597 {
0598     struct pc87360_data *data = dev_get_drvdata(dev);
0599     unsigned long val;
0600     int err;
0601 
0602     err = kstrtoul(buf, 10, &val);
0603     if (err)
0604         return err;
0605 
0606     if (val > 255)
0607         return -EINVAL;
0608 
0609     data->vrm = val;
0610     return count;
0611 }
0612 static DEVICE_ATTR_RW(vrm);
0613 
0614 static ssize_t alarms_in_show(struct device *dev,
0615                   struct device_attribute *attr, char *buf)
0616 {
0617     struct pc87360_data *data = pc87360_update_device(dev);
0618     return sprintf(buf, "%u\n", data->in_alarms);
0619 }
0620 static DEVICE_ATTR_RO(alarms_in);
0621 
0622 static struct attribute *pc8736x_vin_attr_array[] = {
0623     VIN_UNIT_ATTRS(0),
0624     VIN_UNIT_ATTRS(1),
0625     VIN_UNIT_ATTRS(2),
0626     VIN_UNIT_ATTRS(3),
0627     VIN_UNIT_ATTRS(4),
0628     VIN_UNIT_ATTRS(5),
0629     VIN_UNIT_ATTRS(6),
0630     VIN_UNIT_ATTRS(7),
0631     VIN_UNIT_ATTRS(8),
0632     VIN_UNIT_ATTRS(9),
0633     VIN_UNIT_ATTRS(10),
0634     &dev_attr_cpu0_vid.attr,
0635     &dev_attr_vrm.attr,
0636     &dev_attr_alarms_in.attr,
0637     NULL
0638 };
0639 static const struct attribute_group pc8736x_vin_group = {
0640     .attrs = pc8736x_vin_attr_array,
0641 };
0642 
0643 static ssize_t therm_input_show(struct device *dev,
0644                 struct device_attribute *devattr, char *buf)
0645 {
0646     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0647     struct pc87360_data *data = pc87360_update_device(dev);
0648     return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
0649                data->in_vref));
0650 }
0651 static ssize_t therm_min_show(struct device *dev,
0652                   struct device_attribute *devattr, char *buf)
0653 {
0654     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0655     struct pc87360_data *data = pc87360_update_device(dev);
0656     return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
0657                data->in_vref));
0658 }
0659 static ssize_t therm_max_show(struct device *dev,
0660                   struct device_attribute *devattr, char *buf)
0661 {
0662     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0663     struct pc87360_data *data = pc87360_update_device(dev);
0664     return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
0665                data->in_vref));
0666 }
0667 static ssize_t therm_crit_show(struct device *dev,
0668                    struct device_attribute *devattr, char *buf)
0669 {
0670     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0671     struct pc87360_data *data = pc87360_update_device(dev);
0672     return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
0673                data->in_vref));
0674 }
0675 static ssize_t therm_status_show(struct device *dev,
0676                  struct device_attribute *devattr, char *buf)
0677 {
0678     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0679     struct pc87360_data *data = pc87360_update_device(dev);
0680     return sprintf(buf, "%u\n", data->in_status[attr->index]);
0681 }
0682 
0683 static ssize_t therm_min_store(struct device *dev,
0684                    struct device_attribute *devattr,
0685                    const char *buf, size_t count)
0686 {
0687     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0688     struct pc87360_data *data = dev_get_drvdata(dev);
0689     long val;
0690     int err;
0691 
0692     err = kstrtol(buf, 10, &val);
0693     if (err)
0694         return err;
0695 
0696     mutex_lock(&data->update_lock);
0697     data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
0698     pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
0699                 data->in_min[attr->index]);
0700     mutex_unlock(&data->update_lock);
0701     return count;
0702 }
0703 
0704 static ssize_t therm_max_store(struct device *dev,
0705                    struct device_attribute *devattr,
0706                    const char *buf, size_t count)
0707 {
0708     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0709     struct pc87360_data *data = dev_get_drvdata(dev);
0710     long val;
0711     int err;
0712 
0713     err = kstrtol(buf, 10, &val);
0714     if (err)
0715         return err;
0716 
0717     mutex_lock(&data->update_lock);
0718     data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
0719     pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
0720                 data->in_max[attr->index]);
0721     mutex_unlock(&data->update_lock);
0722     return count;
0723 }
0724 static ssize_t therm_crit_store(struct device *dev,
0725                 struct device_attribute *devattr,
0726                 const char *buf, size_t count)
0727 {
0728     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0729     struct pc87360_data *data = dev_get_drvdata(dev);
0730     long val;
0731     int err;
0732 
0733     err = kstrtol(buf, 10, &val);
0734     if (err)
0735         return err;
0736 
0737     mutex_lock(&data->update_lock);
0738     data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
0739     pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
0740                 data->in_crit[attr->index-11]);
0741     mutex_unlock(&data->update_lock);
0742     return count;
0743 }
0744 
0745 /*
0746  * the +11 term below reflects the fact that VLM units 11,12,13 are
0747  * used in the chip to measure voltage across the thermistors
0748  */
0749 static struct sensor_device_attribute therm_input[] = {
0750     SENSOR_ATTR_RO(temp4_input, therm_input, 0 + 11),
0751     SENSOR_ATTR_RO(temp5_input, therm_input, 1 + 11),
0752     SENSOR_ATTR_RO(temp6_input, therm_input, 2 + 11),
0753 };
0754 static struct sensor_device_attribute therm_status[] = {
0755     SENSOR_ATTR_RO(temp4_status, therm_status, 0 + 11),
0756     SENSOR_ATTR_RO(temp5_status, therm_status, 1 + 11),
0757     SENSOR_ATTR_RO(temp6_status, therm_status, 2 + 11),
0758 };
0759 static struct sensor_device_attribute therm_min[] = {
0760     SENSOR_ATTR_RW(temp4_min, therm_min, 0 + 11),
0761     SENSOR_ATTR_RW(temp5_min, therm_min, 1 + 11),
0762     SENSOR_ATTR_RW(temp6_min, therm_min, 2 + 11),
0763 };
0764 static struct sensor_device_attribute therm_max[] = {
0765     SENSOR_ATTR_RW(temp4_max, therm_max, 0 + 11),
0766     SENSOR_ATTR_RW(temp5_max, therm_max, 1 + 11),
0767     SENSOR_ATTR_RW(temp6_max, therm_max, 2 + 11),
0768 };
0769 static struct sensor_device_attribute therm_crit[] = {
0770     SENSOR_ATTR_RW(temp4_crit, therm_crit, 0 + 11),
0771     SENSOR_ATTR_RW(temp5_crit, therm_crit, 1 + 11),
0772     SENSOR_ATTR_RW(temp6_crit, therm_crit, 2 + 11),
0773 };
0774 
0775 /*
0776  * show_therm_min/max_alarm() reads data from the per-channel voltage
0777  * status register (sec 11.5.12)
0778  */
0779 
0780 static ssize_t therm_min_alarm_show(struct device *dev,
0781                     struct device_attribute *devattr,
0782                     char *buf)
0783 {
0784     struct pc87360_data *data = pc87360_update_device(dev);
0785     unsigned nr = to_sensor_dev_attr(devattr)->index;
0786 
0787     return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
0788 }
0789 static ssize_t therm_max_alarm_show(struct device *dev,
0790                     struct device_attribute *devattr,
0791                     char *buf)
0792 {
0793     struct pc87360_data *data = pc87360_update_device(dev);
0794     unsigned nr = to_sensor_dev_attr(devattr)->index;
0795 
0796     return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
0797 }
0798 static ssize_t therm_crit_alarm_show(struct device *dev,
0799                      struct device_attribute *devattr,
0800                      char *buf)
0801 {
0802     struct pc87360_data *data = pc87360_update_device(dev);
0803     unsigned nr = to_sensor_dev_attr(devattr)->index;
0804 
0805     return sprintf(buf, "%u\n", !!(data->in_status[nr] & TEMP_ALM_CRIT));
0806 }
0807 
0808 static struct sensor_device_attribute therm_min_alarm[] = {
0809     SENSOR_ATTR_RO(temp4_min_alarm, therm_min_alarm, 0 + 11),
0810     SENSOR_ATTR_RO(temp5_min_alarm, therm_min_alarm, 1 + 11),
0811     SENSOR_ATTR_RO(temp6_min_alarm, therm_min_alarm, 2 + 11),
0812 };
0813 static struct sensor_device_attribute therm_max_alarm[] = {
0814     SENSOR_ATTR_RO(temp4_max_alarm, therm_max_alarm, 0 + 11),
0815     SENSOR_ATTR_RO(temp5_max_alarm, therm_max_alarm, 1 + 11),
0816     SENSOR_ATTR_RO(temp6_max_alarm, therm_max_alarm, 2 + 11),
0817 };
0818 static struct sensor_device_attribute therm_crit_alarm[] = {
0819     SENSOR_ATTR_RO(temp4_crit_alarm, therm_crit_alarm, 0 + 11),
0820     SENSOR_ATTR_RO(temp5_crit_alarm, therm_crit_alarm, 1 + 11),
0821     SENSOR_ATTR_RO(temp6_crit_alarm, therm_crit_alarm, 2 + 11),
0822 };
0823 
0824 #define THERM_UNIT_ATTRS(X) \
0825     &therm_input[X].dev_attr.attr,  \
0826     &therm_status[X].dev_attr.attr, \
0827     &therm_min[X].dev_attr.attr,    \
0828     &therm_max[X].dev_attr.attr,    \
0829     &therm_crit[X].dev_attr.attr,   \
0830     &therm_min_alarm[X].dev_attr.attr, \
0831     &therm_max_alarm[X].dev_attr.attr, \
0832     &therm_crit_alarm[X].dev_attr.attr
0833 
0834 static struct attribute *pc8736x_therm_attr_array[] = {
0835     THERM_UNIT_ATTRS(0),
0836     THERM_UNIT_ATTRS(1),
0837     THERM_UNIT_ATTRS(2),
0838     NULL
0839 };
0840 static const struct attribute_group pc8736x_therm_group = {
0841     .attrs = pc8736x_therm_attr_array,
0842 };
0843 
0844 static ssize_t temp_input_show(struct device *dev,
0845                    struct device_attribute *devattr, char *buf)
0846 {
0847     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0848     struct pc87360_data *data = pc87360_update_device(dev);
0849     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
0850 }
0851 
0852 static ssize_t temp_min_show(struct device *dev,
0853                  struct device_attribute *devattr, char *buf)
0854 {
0855     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0856     struct pc87360_data *data = pc87360_update_device(dev);
0857     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
0858 }
0859 
0860 static ssize_t temp_max_show(struct device *dev,
0861                  struct device_attribute *devattr, char *buf)
0862 {
0863     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0864     struct pc87360_data *data = pc87360_update_device(dev);
0865     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
0866 }
0867 
0868 static ssize_t temp_crit_show(struct device *dev,
0869                   struct device_attribute *devattr, char *buf)
0870 {
0871     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0872     struct pc87360_data *data = pc87360_update_device(dev);
0873     return sprintf(buf, "%d\n",
0874                TEMP_FROM_REG(data->temp_crit[attr->index]));
0875 }
0876 
0877 static ssize_t temp_status_show(struct device *dev,
0878                 struct device_attribute *devattr, char *buf)
0879 {
0880     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0881     struct pc87360_data *data = pc87360_update_device(dev);
0882     return sprintf(buf, "%d\n", data->temp_status[attr->index]);
0883 }
0884 
0885 static ssize_t temp_min_store(struct device *dev,
0886                   struct device_attribute *devattr,
0887                   const char *buf, size_t count)
0888 {
0889     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0890     struct pc87360_data *data = dev_get_drvdata(dev);
0891     long val;
0892     int err;
0893 
0894     err = kstrtol(buf, 10, &val);
0895     if (err)
0896         return err;
0897 
0898     mutex_lock(&data->update_lock);
0899     data->temp_min[attr->index] = TEMP_TO_REG(val);
0900     pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
0901                 data->temp_min[attr->index]);
0902     mutex_unlock(&data->update_lock);
0903     return count;
0904 }
0905 
0906 static ssize_t temp_max_store(struct device *dev,
0907                   struct device_attribute *devattr,
0908                   const char *buf, size_t count)
0909 {
0910     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0911     struct pc87360_data *data = dev_get_drvdata(dev);
0912     long val;
0913     int err;
0914 
0915     err = kstrtol(buf, 10, &val);
0916     if (err)
0917         return err;
0918 
0919     mutex_lock(&data->update_lock);
0920     data->temp_max[attr->index] = TEMP_TO_REG(val);
0921     pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
0922                 data->temp_max[attr->index]);
0923     mutex_unlock(&data->update_lock);
0924     return count;
0925 }
0926 
0927 static ssize_t temp_crit_store(struct device *dev,
0928                    struct device_attribute *devattr,
0929                    const char *buf, size_t count)
0930 {
0931     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0932     struct pc87360_data *data = dev_get_drvdata(dev);
0933     long val;
0934     int err;
0935 
0936     err = kstrtol(buf, 10, &val);
0937     if (err)
0938         return err;
0939 
0940     mutex_lock(&data->update_lock);
0941     data->temp_crit[attr->index] = TEMP_TO_REG(val);
0942     pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
0943                 data->temp_crit[attr->index]);
0944     mutex_unlock(&data->update_lock);
0945     return count;
0946 }
0947 
0948 static struct sensor_device_attribute temp_input[] = {
0949     SENSOR_ATTR_RO(temp1_input, temp_input, 0),
0950     SENSOR_ATTR_RO(temp2_input, temp_input, 1),
0951     SENSOR_ATTR_RO(temp3_input, temp_input, 2),
0952 };
0953 static struct sensor_device_attribute temp_status[] = {
0954     SENSOR_ATTR_RO(temp1_status, temp_status, 0),
0955     SENSOR_ATTR_RO(temp2_status, temp_status, 1),
0956     SENSOR_ATTR_RO(temp3_status, temp_status, 2),
0957 };
0958 static struct sensor_device_attribute temp_min[] = {
0959     SENSOR_ATTR_RW(temp1_min, temp_min, 0),
0960     SENSOR_ATTR_RW(temp2_min, temp_min, 1),
0961     SENSOR_ATTR_RW(temp3_min, temp_min, 2),
0962 };
0963 static struct sensor_device_attribute temp_max[] = {
0964     SENSOR_ATTR_RW(temp1_max, temp_max, 0),
0965     SENSOR_ATTR_RW(temp2_max, temp_max, 1),
0966     SENSOR_ATTR_RW(temp3_max, temp_max, 2),
0967 };
0968 static struct sensor_device_attribute temp_crit[] = {
0969     SENSOR_ATTR_RW(temp1_crit, temp_crit, 0),
0970     SENSOR_ATTR_RW(temp2_crit, temp_crit, 1),
0971     SENSOR_ATTR_RW(temp3_crit, temp_crit, 2),
0972 };
0973 
0974 static ssize_t alarms_temp_show(struct device *dev,
0975                 struct device_attribute *attr, char *buf)
0976 {
0977     struct pc87360_data *data = pc87360_update_device(dev);
0978     return sprintf(buf, "%u\n", data->temp_alarms);
0979 }
0980 
0981 static DEVICE_ATTR_RO(alarms_temp);
0982 
0983 /*
0984  * show_temp_min/max_alarm() reads data from the per-channel status
0985  * register (sec 12.3.7), not the temp event status registers (sec
0986  * 12.3.2) that show_temp_alarm() reads (via data->temp_alarms)
0987  */
0988 
0989 static ssize_t temp_min_alarm_show(struct device *dev,
0990                    struct device_attribute *devattr,
0991                    char *buf)
0992 {
0993     struct pc87360_data *data = pc87360_update_device(dev);
0994     unsigned nr = to_sensor_dev_attr(devattr)->index;
0995 
0996     return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MIN));
0997 }
0998 
0999 static ssize_t temp_max_alarm_show(struct device *dev,
1000                    struct device_attribute *devattr,
1001                    char *buf)
1002 {
1003     struct pc87360_data *data = pc87360_update_device(dev);
1004     unsigned nr = to_sensor_dev_attr(devattr)->index;
1005 
1006     return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MAX));
1007 }
1008 
1009 static ssize_t temp_crit_alarm_show(struct device *dev,
1010                     struct device_attribute *devattr,
1011                     char *buf)
1012 {
1013     struct pc87360_data *data = pc87360_update_device(dev);
1014     unsigned nr = to_sensor_dev_attr(devattr)->index;
1015 
1016     return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_ALM_CRIT));
1017 }
1018 
1019 static struct sensor_device_attribute temp_min_alarm[] = {
1020     SENSOR_ATTR_RO(temp1_min_alarm, temp_min_alarm, 0),
1021     SENSOR_ATTR_RO(temp2_min_alarm, temp_min_alarm, 1),
1022     SENSOR_ATTR_RO(temp3_min_alarm, temp_min_alarm, 2),
1023 };
1024 
1025 static struct sensor_device_attribute temp_max_alarm[] = {
1026     SENSOR_ATTR_RO(temp1_max_alarm, temp_max_alarm, 0),
1027     SENSOR_ATTR_RO(temp2_max_alarm, temp_max_alarm, 1),
1028     SENSOR_ATTR_RO(temp3_max_alarm, temp_max_alarm, 2),
1029 };
1030 
1031 static struct sensor_device_attribute temp_crit_alarm[] = {
1032     SENSOR_ATTR_RO(temp1_crit_alarm, temp_crit_alarm, 0),
1033     SENSOR_ATTR_RO(temp2_crit_alarm, temp_crit_alarm, 1),
1034     SENSOR_ATTR_RO(temp3_crit_alarm, temp_crit_alarm, 2),
1035 };
1036 
1037 #define TEMP_FAULT  0x40    /* open diode */
1038 static ssize_t temp_fault_show(struct device *dev,
1039                    struct device_attribute *devattr, char *buf)
1040 {
1041     struct pc87360_data *data = pc87360_update_device(dev);
1042     unsigned nr = to_sensor_dev_attr(devattr)->index;
1043 
1044     return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_FAULT));
1045 }
1046 static struct sensor_device_attribute temp_fault[] = {
1047     SENSOR_ATTR_RO(temp1_fault, temp_fault, 0),
1048     SENSOR_ATTR_RO(temp2_fault, temp_fault, 1),
1049     SENSOR_ATTR_RO(temp3_fault, temp_fault, 2),
1050 };
1051 
1052 #define TEMP_UNIT_ATTRS(X)          \
1053 {   &temp_input[X].dev_attr.attr,       \
1054     &temp_status[X].dev_attr.attr,      \
1055     &temp_min[X].dev_attr.attr,     \
1056     &temp_max[X].dev_attr.attr,     \
1057     &temp_crit[X].dev_attr.attr,        \
1058     &temp_min_alarm[X].dev_attr.attr,   \
1059     &temp_max_alarm[X].dev_attr.attr,   \
1060     &temp_crit_alarm[X].dev_attr.attr,  \
1061     &temp_fault[X].dev_attr.attr,       \
1062     NULL                    \
1063 }
1064 
1065 static struct attribute *pc8736x_temp_attr[][10] = {
1066     TEMP_UNIT_ATTRS(0),
1067     TEMP_UNIT_ATTRS(1),
1068     TEMP_UNIT_ATTRS(2)
1069 };
1070 
1071 static const struct attribute_group pc8736x_temp_attr_group[] = {
1072     { .attrs = pc8736x_temp_attr[0] },
1073     { .attrs = pc8736x_temp_attr[1] },
1074     { .attrs = pc8736x_temp_attr[2] }
1075 };
1076 
1077 static ssize_t name_show(struct device *dev,
1078             struct device_attribute *devattr, char *buf)
1079 {
1080     struct pc87360_data *data = dev_get_drvdata(dev);
1081     return sprintf(buf, "%s\n", data->name);
1082 }
1083 
1084 static DEVICE_ATTR_RO(name);
1085 
1086 /*
1087  * Device detection, registration and update
1088  */
1089 
1090 static int __init pc87360_find(int sioaddr, u8 *devid,
1091                    unsigned short *addresses)
1092 {
1093     u16 val;
1094     int i;
1095     int nrdev; /* logical device count */
1096 
1097     /* No superio_enter */
1098 
1099     /* Identify device */
1100     val = force_id ? force_id : superio_inb(sioaddr, DEVID);
1101     switch (val) {
1102     case 0xE1: /* PC87360 */
1103     case 0xE8: /* PC87363 */
1104     case 0xE4: /* PC87364 */
1105         nrdev = 1;
1106         break;
1107     case 0xE5: /* PC87365 */
1108     case 0xE9: /* PC87366 */
1109         nrdev = 3;
1110         break;
1111     default:
1112         superio_exit(sioaddr);
1113         return -ENODEV;
1114     }
1115     /* Remember the device id */
1116     *devid = val;
1117 
1118     for (i = 0; i < nrdev; i++) {
1119         /* select logical device */
1120         superio_outb(sioaddr, DEV, logdev[i]);
1121 
1122         val = superio_inb(sioaddr, ACT);
1123         if (!(val & 0x01)) {
1124             pr_info("Device 0x%02x not activated\n", logdev[i]);
1125             continue;
1126         }
1127 
1128         val = (superio_inb(sioaddr, BASE) << 8)
1129             | superio_inb(sioaddr, BASE + 1);
1130         if (!val) {
1131             pr_info("Base address not set for device 0x%02x\n",
1132                 logdev[i]);
1133             continue;
1134         }
1135 
1136         addresses[i] = val;
1137 
1138         if (i == 0) { /* Fans */
1139             confreg[0] = superio_inb(sioaddr, 0xF0);
1140             confreg[1] = superio_inb(sioaddr, 0xF1);
1141 
1142             pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 1,
1143                  (confreg[0] >> 2) & 1, (confreg[0] >> 3) & 1,
1144                  (confreg[0] >> 4) & 1);
1145             pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 2,
1146                  (confreg[0] >> 5) & 1, (confreg[0] >> 6) & 1,
1147                  (confreg[0] >> 7) & 1);
1148             pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 3,
1149                  confreg[1] & 1, (confreg[1] >> 1) & 1,
1150                  (confreg[1] >> 2) & 1);
1151         } else if (i == 1) { /* Voltages */
1152             /* Are we using thermistors? */
1153             if (*devid == 0xE9) { /* PC87366 */
1154                 /*
1155                  * These registers are not logical-device
1156                  * specific, just that we won't need them if
1157                  * we don't use the VLM device
1158                  */
1159                 confreg[2] = superio_inb(sioaddr, 0x2B);
1160                 confreg[3] = superio_inb(sioaddr, 0x25);
1161 
1162                 if (confreg[2] & 0x40) {
1163                     pr_info("Using thermistors for temperature monitoring\n");
1164                 }
1165                 if (confreg[3] & 0xE0) {
1166                     pr_info("VID inputs routed (mode %u)\n",
1167                         confreg[3] >> 5);
1168                 }
1169             }
1170         }
1171     }
1172 
1173     superio_exit(sioaddr);
1174     return 0;
1175 }
1176 
1177 static void pc87360_remove_files(struct device *dev)
1178 {
1179     int i;
1180 
1181     device_remove_file(dev, &dev_attr_name);
1182     device_remove_file(dev, &dev_attr_alarms_temp);
1183     for (i = 0; i < ARRAY_SIZE(pc8736x_temp_attr_group); i++)
1184         sysfs_remove_group(&dev->kobj, &pc8736x_temp_attr_group[i]);
1185     for (i = 0; i < ARRAY_SIZE(pc8736x_fan_attr_group); i++) {
1186         sysfs_remove_group(&pdev->dev.kobj, &pc8736x_fan_attr_group[i]);
1187         device_remove_file(dev, &pwm[i].dev_attr);
1188     }
1189     sysfs_remove_group(&dev->kobj, &pc8736x_therm_group);
1190     sysfs_remove_group(&dev->kobj, &pc8736x_vin_group);
1191 }
1192 
1193 static int pc87360_probe(struct platform_device *pdev)
1194 {
1195     int i;
1196     struct pc87360_data *data;
1197     int err = 0;
1198     const char *name;
1199     int use_thermistors = 0;
1200     struct device *dev = &pdev->dev;
1201 
1202     data = devm_kzalloc(dev, sizeof(struct pc87360_data), GFP_KERNEL);
1203     if (!data)
1204         return -ENOMEM;
1205 
1206     switch (devid) {
1207     default:
1208         name = "pc87360";
1209         data->fannr = 2;
1210         break;
1211     case 0xe8:
1212         name = "pc87363";
1213         data->fannr = 2;
1214         break;
1215     case 0xe4:
1216         name = "pc87364";
1217         data->fannr = 3;
1218         break;
1219     case 0xe5:
1220         name = "pc87365";
1221         data->fannr = extra_isa[0] ? 3 : 0;
1222         data->innr = extra_isa[1] ? 11 : 0;
1223         data->tempnr = extra_isa[2] ? 2 : 0;
1224         break;
1225     case 0xe9:
1226         name = "pc87366";
1227         data->fannr = extra_isa[0] ? 3 : 0;
1228         data->innr = extra_isa[1] ? 14 : 0;
1229         data->tempnr = extra_isa[2] ? 3 : 0;
1230         break;
1231     }
1232 
1233     data->name = name;
1234     mutex_init(&data->lock);
1235     mutex_init(&data->update_lock);
1236     platform_set_drvdata(pdev, data);
1237 
1238     for (i = 0; i < LDNI_MAX; i++) {
1239         data->address[i] = extra_isa[i];
1240         if (data->address[i]
1241          && !devm_request_region(dev, extra_isa[i], PC87360_EXTENT,
1242                      pc87360_driver.driver.name)) {
1243             dev_err(dev,
1244                 "Region 0x%x-0x%x already in use!\n",
1245                 extra_isa[i], extra_isa[i]+PC87360_EXTENT-1);
1246             return -EBUSY;
1247         }
1248     }
1249 
1250     /* Retrieve the fans configuration from Super-I/O space */
1251     if (data->fannr)
1252         data->fan_conf = confreg[0] | (confreg[1] << 8);
1253 
1254     /*
1255      * Use the correct reference voltage
1256      * Unless both the VLM and the TMS logical devices agree to
1257      * use an external Vref, the internal one is used.
1258      */
1259     if (data->innr) {
1260         i = pc87360_read_value(data, LD_IN, NO_BANK,
1261                        PC87365_REG_IN_CONFIG);
1262         if (data->tempnr) {
1263             i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
1264                         PC87365_REG_TEMP_CONFIG);
1265         }
1266         data->in_vref = (i&0x02) ? 3025 : 2966;
1267         dev_dbg(dev, "Using %s reference voltage\n",
1268             (i&0x02) ? "external" : "internal");
1269 
1270         data->vid_conf = confreg[3];
1271         data->vrm = vid_which_vrm();
1272     }
1273 
1274     /* Fan clock dividers may be needed before any data is read */
1275     for (i = 0; i < data->fannr; i++) {
1276         if (FAN_CONFIG_MONITOR(data->fan_conf, i))
1277             data->fan_status[i] = pc87360_read_value(data,
1278                           LD_FAN, NO_BANK,
1279                           PC87360_REG_FAN_STATUS(i));
1280     }
1281 
1282     if (init > 0) {
1283         if (devid == 0xe9 && data->address[1]) /* PC87366 */
1284             use_thermistors = confreg[2] & 0x40;
1285 
1286         pc87360_init_device(pdev, use_thermistors);
1287     }
1288 
1289     /* Register all-or-nothing sysfs groups */
1290 
1291     if (data->innr) {
1292         err = sysfs_create_group(&dev->kobj, &pc8736x_vin_group);
1293         if (err)
1294             goto error;
1295     }
1296 
1297     if (data->innr == 14) {
1298         err = sysfs_create_group(&dev->kobj, &pc8736x_therm_group);
1299         if (err)
1300             goto error;
1301     }
1302 
1303     /* create device attr-files for varying sysfs groups */
1304 
1305     if (data->tempnr) {
1306         for (i = 0; i < data->tempnr; i++) {
1307             err = sysfs_create_group(&dev->kobj,
1308                          &pc8736x_temp_attr_group[i]);
1309             if (err)
1310                 goto error;
1311         }
1312         err = device_create_file(dev, &dev_attr_alarms_temp);
1313         if (err)
1314             goto error;
1315     }
1316 
1317     for (i = 0; i < data->fannr; i++) {
1318         if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1319             err = sysfs_create_group(&dev->kobj,
1320                          &pc8736x_fan_attr_group[i]);
1321             if (err)
1322                 goto error;
1323         }
1324         if (FAN_CONFIG_CONTROL(data->fan_conf, i)) {
1325             err = device_create_file(dev, &pwm[i].dev_attr);
1326             if (err)
1327                 goto error;
1328         }
1329     }
1330 
1331     err = device_create_file(dev, &dev_attr_name);
1332     if (err)
1333         goto error;
1334 
1335     data->hwmon_dev = hwmon_device_register(dev);
1336     if (IS_ERR(data->hwmon_dev)) {
1337         err = PTR_ERR(data->hwmon_dev);
1338         goto error;
1339     }
1340     return 0;
1341 
1342 error:
1343     pc87360_remove_files(dev);
1344     return err;
1345 }
1346 
1347 static int pc87360_remove(struct platform_device *pdev)
1348 {
1349     struct pc87360_data *data = platform_get_drvdata(pdev);
1350 
1351     hwmon_device_unregister(data->hwmon_dev);
1352     pc87360_remove_files(&pdev->dev);
1353 
1354     return 0;
1355 }
1356 
1357 /*
1358  * ldi is the logical device index
1359  * bank is for voltages and temperatures only
1360  */
1361 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1362                   u8 reg)
1363 {
1364     int res;
1365 
1366     mutex_lock(&(data->lock));
1367     if (bank != NO_BANK)
1368         outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1369     res = inb_p(data->address[ldi] + reg);
1370     mutex_unlock(&(data->lock));
1371 
1372     return res;
1373 }
1374 
1375 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1376                 u8 reg, u8 value)
1377 {
1378     mutex_lock(&(data->lock));
1379     if (bank != NO_BANK)
1380         outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1381     outb_p(value, data->address[ldi] + reg);
1382     mutex_unlock(&(data->lock));
1383 }
1384 
1385 /* (temp & vin) channel conversion status register flags (pdf sec.11.5.12) */
1386 #define CHAN_CNVRTD 0x80    /* new data ready */
1387 #define CHAN_ENA    0x01    /* enabled channel (temp or vin) */
1388 #define CHAN_ALM_ENA    0x10    /* propagate to alarms-reg ?? (chk val!) */
1389 #define CHAN_READY  (CHAN_ENA|CHAN_CNVRTD) /* sample ready mask */
1390 
1391 #define TEMP_OTS_OE 0x20    /* OTS Output Enable */
1392 #define VIN_RW1C_MASK   (CHAN_READY|CHAN_ALM_MAX|CHAN_ALM_MIN)   /* 0x87 */
1393 #define TEMP_RW1C_MASK  (VIN_RW1C_MASK|TEMP_ALM_CRIT|TEMP_FAULT) /* 0xCF */
1394 
1395 static void pc87360_init_device(struct platform_device *pdev,
1396                 int use_thermistors)
1397 {
1398     struct pc87360_data *data = platform_get_drvdata(pdev);
1399     int i, nr;
1400     const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1401     const u8 init_temp[3] = { 2, 2, 1 };
1402     u8 reg;
1403 
1404     if (init >= 2 && data->innr) {
1405         reg = pc87360_read_value(data, LD_IN, NO_BANK,
1406                      PC87365_REG_IN_CONVRATE);
1407         dev_info(&pdev->dev,
1408              "VLM conversion set to 1s period, 160us delay\n");
1409         pc87360_write_value(data, LD_IN, NO_BANK,
1410                     PC87365_REG_IN_CONVRATE,
1411                     (reg & 0xC0) | 0x11);
1412     }
1413 
1414     nr = data->innr < 11 ? data->innr : 11;
1415     for (i = 0; i < nr; i++) {
1416         reg = pc87360_read_value(data, LD_IN, i,
1417                      PC87365_REG_IN_STATUS);
1418         dev_dbg(&pdev->dev, "bios in%d status:0x%02x\n", i, reg);
1419         if (init >= init_in[i]) {
1420             /* Forcibly enable voltage channel */
1421             if (!(reg & CHAN_ENA)) {
1422                 dev_dbg(&pdev->dev, "Forcibly enabling in%d\n",
1423                     i);
1424                 pc87360_write_value(data, LD_IN, i,
1425                             PC87365_REG_IN_STATUS,
1426                             (reg & 0x68) | 0x87);
1427             }
1428         }
1429     }
1430 
1431     /*
1432      * We can't blindly trust the Super-I/O space configuration bit,
1433      * most BIOS won't set it properly
1434      */
1435     dev_dbg(&pdev->dev, "bios thermistors:%d\n", use_thermistors);
1436     for (i = 11; i < data->innr; i++) {
1437         reg = pc87360_read_value(data, LD_IN, i,
1438                      PC87365_REG_TEMP_STATUS);
1439         use_thermistors = use_thermistors || (reg & CHAN_ENA);
1440         /* thermistors are temp[4-6], measured on vin[11-14] */
1441         dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i-7, reg);
1442     }
1443     dev_dbg(&pdev->dev, "using thermistors:%d\n", use_thermistors);
1444 
1445     i = use_thermistors ? 2 : 0;
1446     for (; i < data->tempnr; i++) {
1447         reg = pc87360_read_value(data, LD_TEMP, i,
1448                      PC87365_REG_TEMP_STATUS);
1449         dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i + 1, reg);
1450         if (init >= init_temp[i]) {
1451             /* Forcibly enable temperature channel */
1452             if (!(reg & CHAN_ENA)) {
1453                 dev_dbg(&pdev->dev,
1454                     "Forcibly enabling temp%d\n", i + 1);
1455                 pc87360_write_value(data, LD_TEMP, i,
1456                             PC87365_REG_TEMP_STATUS,
1457                             0xCF);
1458             }
1459         }
1460     }
1461 
1462     if (use_thermistors) {
1463         for (i = 11; i < data->innr; i++) {
1464             if (init >= init_in[i]) {
1465                 /*
1466                  * The pin may already be used by thermal
1467                  * diodes
1468                  */
1469                 reg = pc87360_read_value(data, LD_TEMP,
1470                       (i - 11) / 2, PC87365_REG_TEMP_STATUS);
1471                 if (reg & CHAN_ENA) {
1472                     dev_dbg(&pdev->dev,
1473             "Skipping temp%d, pin already in use by temp%d\n",
1474                         i - 7, (i - 11) / 2);
1475                     continue;
1476                 }
1477 
1478                 /* Forcibly enable thermistor channel */
1479                 reg = pc87360_read_value(data, LD_IN, i,
1480                              PC87365_REG_IN_STATUS);
1481                 if (!(reg & CHAN_ENA)) {
1482                     dev_dbg(&pdev->dev,
1483                         "Forcibly enabling temp%d\n",
1484                         i - 7);
1485                     pc87360_write_value(data, LD_IN, i,
1486                         PC87365_REG_TEMP_STATUS,
1487                         (reg & 0x60) | 0x8F);
1488                 }
1489             }
1490         }
1491     }
1492 
1493     if (data->innr) {
1494         reg = pc87360_read_value(data, LD_IN, NO_BANK,
1495                      PC87365_REG_IN_CONFIG);
1496         dev_dbg(&pdev->dev, "bios vin-cfg:0x%02x\n", reg);
1497         if (reg & CHAN_ENA) {
1498             dev_dbg(&pdev->dev,
1499                 "Forcibly enabling monitoring (VLM)\n");
1500             pc87360_write_value(data, LD_IN, NO_BANK,
1501                         PC87365_REG_IN_CONFIG,
1502                         reg & 0xFE);
1503         }
1504     }
1505 
1506     if (data->tempnr) {
1507         reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1508                      PC87365_REG_TEMP_CONFIG);
1509         dev_dbg(&pdev->dev, "bios temp-cfg:0x%02x\n", reg);
1510         if (reg & CHAN_ENA) {
1511             dev_dbg(&pdev->dev,
1512                 "Forcibly enabling monitoring (TMS)\n");
1513             pc87360_write_value(data, LD_TEMP, NO_BANK,
1514                         PC87365_REG_TEMP_CONFIG,
1515                         reg & 0xFE);
1516         }
1517 
1518         if (init >= 2) {
1519             /* Chip config as documented by National Semi. */
1520             pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1521             /*
1522              * We voluntarily omit the bank here, in case the
1523              * sequence itself matters. It shouldn't be a problem,
1524              * since nobody else is supposed to access the
1525              * device at that point.
1526              */
1527             pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1528             pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1529             pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1530             pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1531         }
1532     }
1533 }
1534 
1535 static void pc87360_autodiv(struct device *dev, int nr)
1536 {
1537     struct pc87360_data *data = dev_get_drvdata(dev);
1538     u8 old_min = data->fan_min[nr];
1539 
1540     /* Increase clock divider if needed and possible */
1541     if ((data->fan_status[nr] & 0x04) /* overflow flag */
1542      || (data->fan[nr] >= 224)) { /* next to overflow */
1543         if ((data->fan_status[nr] & 0x60) != 0x60) {
1544             data->fan_status[nr] += 0x20;
1545             data->fan_min[nr] >>= 1;
1546             data->fan[nr] >>= 1;
1547             dev_dbg(dev,
1548                 "Increasing clock divider to %d for fan %d\n",
1549                 FAN_DIV_FROM_REG(data->fan_status[nr]), nr + 1);
1550         }
1551     } else {
1552         /* Decrease clock divider if possible */
1553         while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1554          && data->fan[nr] < 85 /* bad accuracy */
1555          && (data->fan_status[nr] & 0x60) != 0x00) {
1556             data->fan_status[nr] -= 0x20;
1557             data->fan_min[nr] <<= 1;
1558             data->fan[nr] <<= 1;
1559             dev_dbg(dev,
1560                 "Decreasing clock divider to %d for fan %d\n",
1561                 FAN_DIV_FROM_REG(data->fan_status[nr]),
1562                 nr + 1);
1563         }
1564     }
1565 
1566     /* Write new fan min if it changed */
1567     if (old_min != data->fan_min[nr]) {
1568         pc87360_write_value(data, LD_FAN, NO_BANK,
1569                     PC87360_REG_FAN_MIN(nr),
1570                     data->fan_min[nr]);
1571     }
1572 }
1573 
1574 static struct pc87360_data *pc87360_update_device(struct device *dev)
1575 {
1576     struct pc87360_data *data = dev_get_drvdata(dev);
1577     u8 i;
1578 
1579     mutex_lock(&data->update_lock);
1580 
1581     if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
1582         dev_dbg(dev, "Data update\n");
1583 
1584         /* Fans */
1585         for (i = 0; i < data->fannr; i++) {
1586             if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1587                 data->fan_status[i] =
1588                     pc87360_read_value(data, LD_FAN,
1589                     NO_BANK, PC87360_REG_FAN_STATUS(i));
1590                 data->fan[i] = pc87360_read_value(data, LD_FAN,
1591                            NO_BANK, PC87360_REG_FAN(i));
1592                 data->fan_min[i] = pc87360_read_value(data,
1593                            LD_FAN, NO_BANK,
1594                            PC87360_REG_FAN_MIN(i));
1595                 /* Change clock divider if needed */
1596                 pc87360_autodiv(dev, i);
1597                 /* Clear bits and write new divider */
1598                 pc87360_write_value(data, LD_FAN, NO_BANK,
1599                             PC87360_REG_FAN_STATUS(i),
1600                             data->fan_status[i]);
1601             }
1602             if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1603                 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1604                            NO_BANK, PC87360_REG_PWM(i));
1605         }
1606 
1607         /* Voltages */
1608         for (i = 0; i < data->innr; i++) {
1609             data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1610                          PC87365_REG_IN_STATUS);
1611             /* Clear bits */
1612             pc87360_write_value(data, LD_IN, i,
1613                         PC87365_REG_IN_STATUS,
1614                         data->in_status[i]);
1615             if ((data->in_status[i] & CHAN_READY) == CHAN_READY) {
1616                 data->in[i] = pc87360_read_value(data, LD_IN,
1617                           i, PC87365_REG_IN);
1618             }
1619             if (data->in_status[i] & CHAN_ENA) {
1620                 data->in_min[i] = pc87360_read_value(data,
1621                           LD_IN, i,
1622                           PC87365_REG_IN_MIN);
1623                 data->in_max[i] = pc87360_read_value(data,
1624                           LD_IN, i,
1625                           PC87365_REG_IN_MAX);
1626                 if (i >= 11)
1627                     data->in_crit[i-11] =
1628                         pc87360_read_value(data, LD_IN,
1629                         i, PC87365_REG_TEMP_CRIT);
1630             }
1631         }
1632         if (data->innr) {
1633             data->in_alarms = pc87360_read_value(data, LD_IN,
1634                       NO_BANK, PC87365_REG_IN_ALARMS1)
1635                     | ((pc87360_read_value(data, LD_IN,
1636                         NO_BANK, PC87365_REG_IN_ALARMS2)
1637                         & 0x07) << 8);
1638             data->vid = (data->vid_conf & 0xE0) ?
1639                     pc87360_read_value(data, LD_IN,
1640                     NO_BANK, PC87365_REG_VID) : 0x1F;
1641         }
1642 
1643         /* Temperatures */
1644         for (i = 0; i < data->tempnr; i++) {
1645             data->temp_status[i] = pc87360_read_value(data,
1646                            LD_TEMP, i,
1647                            PC87365_REG_TEMP_STATUS);
1648             /* Clear bits */
1649             pc87360_write_value(data, LD_TEMP, i,
1650                         PC87365_REG_TEMP_STATUS,
1651                         data->temp_status[i]);
1652             if ((data->temp_status[i] & CHAN_READY) == CHAN_READY) {
1653                 data->temp[i] = pc87360_read_value(data,
1654                         LD_TEMP, i,
1655                         PC87365_REG_TEMP);
1656             }
1657             if (data->temp_status[i] & CHAN_ENA) {
1658                 data->temp_min[i] = pc87360_read_value(data,
1659                             LD_TEMP, i,
1660                             PC87365_REG_TEMP_MIN);
1661                 data->temp_max[i] = pc87360_read_value(data,
1662                             LD_TEMP, i,
1663                             PC87365_REG_TEMP_MAX);
1664                 data->temp_crit[i] = pc87360_read_value(data,
1665                              LD_TEMP, i,
1666                              PC87365_REG_TEMP_CRIT);
1667             }
1668         }
1669         if (data->tempnr) {
1670             data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1671                         NO_BANK, PC87365_REG_TEMP_ALARMS)
1672                         & 0x3F;
1673         }
1674 
1675         data->last_updated = jiffies;
1676         data->valid = true;
1677     }
1678 
1679     mutex_unlock(&data->update_lock);
1680 
1681     return data;
1682 }
1683 
1684 static int __init pc87360_device_add(unsigned short address)
1685 {
1686     struct resource res[3];
1687     int err, i, res_count;
1688 
1689     pdev = platform_device_alloc("pc87360", address);
1690     if (!pdev) {
1691         err = -ENOMEM;
1692         pr_err("Device allocation failed\n");
1693         goto exit;
1694     }
1695 
1696     memset(res, 0, 3 * sizeof(struct resource));
1697     res_count = 0;
1698     for (i = 0; i < 3; i++) {
1699         if (!extra_isa[i])
1700             continue;
1701         res[res_count].start = extra_isa[i];
1702         res[res_count].end = extra_isa[i] + PC87360_EXTENT - 1;
1703         res[res_count].name = "pc87360";
1704         res[res_count].flags = IORESOURCE_IO;
1705 
1706         err = acpi_check_resource_conflict(&res[res_count]);
1707         if (err)
1708             goto exit_device_put;
1709 
1710         res_count++;
1711     }
1712 
1713     err = platform_device_add_resources(pdev, res, res_count);
1714     if (err) {
1715         pr_err("Device resources addition failed (%d)\n", err);
1716         goto exit_device_put;
1717     }
1718 
1719     err = platform_device_add(pdev);
1720     if (err) {
1721         pr_err("Device addition failed (%d)\n", err);
1722         goto exit_device_put;
1723     }
1724 
1725     return 0;
1726 
1727 exit_device_put:
1728     platform_device_put(pdev);
1729 exit:
1730     return err;
1731 }
1732 
1733 static int __init pc87360_init(void)
1734 {
1735     int err, i;
1736     unsigned short address = 0;
1737 
1738     if (pc87360_find(0x2e, &devid, extra_isa)
1739      && pc87360_find(0x4e, &devid, extra_isa)) {
1740         pr_warn("PC8736x not detected, module not inserted\n");
1741         return -ENODEV;
1742     }
1743 
1744     /* Arbitrarily pick one of the addresses */
1745     for (i = 0; i < 3; i++) {
1746         if (extra_isa[i] != 0x0000) {
1747             address = extra_isa[i];
1748             break;
1749         }
1750     }
1751 
1752     if (address == 0x0000) {
1753         pr_warn("No active logical device, module not inserted\n");
1754         return -ENODEV;
1755     }
1756 
1757     err = platform_driver_register(&pc87360_driver);
1758     if (err)
1759         goto exit;
1760 
1761     /* Sets global pdev as a side effect */
1762     err = pc87360_device_add(address);
1763     if (err)
1764         goto exit_driver;
1765 
1766     return 0;
1767 
1768  exit_driver:
1769     platform_driver_unregister(&pc87360_driver);
1770  exit:
1771     return err;
1772 }
1773 
1774 static void __exit pc87360_exit(void)
1775 {
1776     platform_device_unregister(pdev);
1777     platform_driver_unregister(&pc87360_driver);
1778 }
1779 
1780 
1781 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
1782 MODULE_DESCRIPTION("PC8736x hardware monitor");
1783 MODULE_LICENSE("GPL");
1784 
1785 module_init(pc87360_init);
1786 module_exit(pc87360_exit);