Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * smsc47m1.c - Part of lm_sensors, Linux kernel modules
0004  *      for hardware monitoring
0005  *
0006  * Supports the SMSC LPC47B27x, LPC47M10x, LPC47M112, LPC47M13x,
0007  * LPC47M14x, LPC47M15x, LPC47M192, LPC47M292 and LPC47M997
0008  * Super-I/O chips.
0009  *
0010  * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
0011  * Copyright (C) 2004-2007 Jean Delvare <jdelvare@suse.de>
0012  * Ported to Linux 2.6 by Gabriele Gorla <gorlik@yahoo.com>
0013  *          and Jean Delvare
0014  */
0015 
0016 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0017 
0018 #include <linux/module.h>
0019 #include <linux/slab.h>
0020 #include <linux/ioport.h>
0021 #include <linux/jiffies.h>
0022 #include <linux/platform_device.h>
0023 #include <linux/hwmon.h>
0024 #include <linux/hwmon-sysfs.h>
0025 #include <linux/err.h>
0026 #include <linux/init.h>
0027 #include <linux/mutex.h>
0028 #include <linux/sysfs.h>
0029 #include <linux/acpi.h>
0030 #include <linux/io.h>
0031 
0032 static unsigned short force_id;
0033 module_param(force_id, ushort, 0);
0034 MODULE_PARM_DESC(force_id, "Override the detected device ID");
0035 
0036 static struct platform_device *pdev;
0037 
0038 #define DRVNAME "smsc47m1"
0039 enum chips { smsc47m1, smsc47m2 };
0040 
0041 /* Super-I/0 registers and commands */
0042 
0043 #define REG 0x2e    /* The register to read/write */
0044 #define VAL 0x2f    /* The value to read/write */
0045 
0046 static inline void
0047 superio_outb(int reg, int val)
0048 {
0049     outb(reg, REG);
0050     outb(val, VAL);
0051 }
0052 
0053 static inline int
0054 superio_inb(int reg)
0055 {
0056     outb(reg, REG);
0057     return inb(VAL);
0058 }
0059 
0060 /* logical device for fans is 0x0A */
0061 #define superio_select() superio_outb(0x07, 0x0A)
0062 
0063 static inline int
0064 superio_enter(void)
0065 {
0066     if (!request_muxed_region(REG, 2, DRVNAME))
0067         return -EBUSY;
0068 
0069     outb(0x55, REG);
0070     return 0;
0071 }
0072 
0073 static inline void
0074 superio_exit(void)
0075 {
0076     outb(0xAA, REG);
0077     release_region(REG, 2);
0078 }
0079 
0080 #define SUPERIO_REG_ACT     0x30
0081 #define SUPERIO_REG_BASE    0x60
0082 #define SUPERIO_REG_DEVID   0x20
0083 #define SUPERIO_REG_DEVREV  0x21
0084 
0085 /* Logical device registers */
0086 
0087 #define SMSC_EXTENT     0x80
0088 
0089 /* nr is 0 or 1 in the macros below */
0090 #define SMSC47M1_REG_ALARM      0x04
0091 #define SMSC47M1_REG_TPIN(nr)       (0x34 - (nr))
0092 #define SMSC47M1_REG_PPIN(nr)       (0x36 - (nr))
0093 #define SMSC47M1_REG_FANDIV     0x58
0094 
0095 static const u8 SMSC47M1_REG_FAN[3]     = { 0x59, 0x5a, 0x6b };
0096 static const u8 SMSC47M1_REG_FAN_PRELOAD[3] = { 0x5b, 0x5c, 0x6c };
0097 static const u8 SMSC47M1_REG_PWM[3]     = { 0x56, 0x57, 0x69 };
0098 
0099 #define SMSC47M2_REG_ALARM6     0x09
0100 #define SMSC47M2_REG_TPIN1      0x38
0101 #define SMSC47M2_REG_TPIN2      0x37
0102 #define SMSC47M2_REG_TPIN3      0x2d
0103 #define SMSC47M2_REG_PPIN3      0x2c
0104 #define SMSC47M2_REG_FANDIV3        0x6a
0105 
0106 #define MIN_FROM_REG(reg, div)      ((reg) >= 192 ? 0 : \
0107                      983040 / ((192 - (reg)) * (div)))
0108 #define FAN_FROM_REG(reg, div, preload) ((reg) <= (preload) || (reg) == 255 ? \
0109                      0 : \
0110                      983040 / (((reg) - (preload)) * (div)))
0111 #define DIV_FROM_REG(reg)       (1 << (reg))
0112 #define PWM_FROM_REG(reg)       (((reg) & 0x7E) << 1)
0113 #define PWM_EN_FROM_REG(reg)        ((~(reg)) & 0x01)
0114 #define PWM_TO_REG(reg)         (((reg) >> 1) & 0x7E)
0115 
0116 struct smsc47m1_data {
0117     unsigned short addr;
0118     const char *name;
0119     enum chips type;
0120     struct device *hwmon_dev;
0121 
0122     struct mutex update_lock;
0123     unsigned long last_updated; /* In jiffies */
0124 
0125     u8 fan[3];      /* Register value */
0126     u8 fan_preload[3];  /* Register value */
0127     u8 fan_div[3];      /* Register encoding, shifted right */
0128     u8 alarms;      /* Register encoding */
0129     u8 pwm[3];      /* Register value (bit 0 is disable) */
0130 };
0131 
0132 struct smsc47m1_sio_data {
0133     enum chips type;
0134     u8 activate;        /* Remember initial device state */
0135 };
0136 
0137 static inline int smsc47m1_read_value(struct smsc47m1_data *data, u8 reg)
0138 {
0139     return inb_p(data->addr + reg);
0140 }
0141 
0142 static inline void smsc47m1_write_value(struct smsc47m1_data *data, u8 reg,
0143         u8 value)
0144 {
0145     outb_p(value, data->addr + reg);
0146 }
0147 
0148 static struct smsc47m1_data *smsc47m1_update_device(struct device *dev,
0149         int init)
0150 {
0151     struct smsc47m1_data *data = dev_get_drvdata(dev);
0152 
0153     mutex_lock(&data->update_lock);
0154 
0155     if (time_after(jiffies, data->last_updated + HZ + HZ / 2) || init) {
0156         int i, fan_nr;
0157         fan_nr = data->type == smsc47m2 ? 3 : 2;
0158 
0159         for (i = 0; i < fan_nr; i++) {
0160             data->fan[i] = smsc47m1_read_value(data,
0161                        SMSC47M1_REG_FAN[i]);
0162             data->fan_preload[i] = smsc47m1_read_value(data,
0163                            SMSC47M1_REG_FAN_PRELOAD[i]);
0164             data->pwm[i] = smsc47m1_read_value(data,
0165                        SMSC47M1_REG_PWM[i]);
0166         }
0167 
0168         i = smsc47m1_read_value(data, SMSC47M1_REG_FANDIV);
0169         data->fan_div[0] = (i >> 4) & 0x03;
0170         data->fan_div[1] = i >> 6;
0171 
0172         data->alarms = smsc47m1_read_value(data,
0173                    SMSC47M1_REG_ALARM) >> 6;
0174         /* Clear alarms if needed */
0175         if (data->alarms)
0176             smsc47m1_write_value(data, SMSC47M1_REG_ALARM, 0xC0);
0177 
0178         if (fan_nr >= 3) {
0179             data->fan_div[2] = (smsc47m1_read_value(data,
0180                         SMSC47M2_REG_FANDIV3) >> 4) & 0x03;
0181             data->alarms |= (smsc47m1_read_value(data,
0182                      SMSC47M2_REG_ALARM6) & 0x40) >> 4;
0183             /* Clear alarm if needed */
0184             if (data->alarms & 0x04)
0185                 smsc47m1_write_value(data,
0186                              SMSC47M2_REG_ALARM6,
0187                              0x40);
0188         }
0189 
0190         data->last_updated = jiffies;
0191     }
0192 
0193     mutex_unlock(&data->update_lock);
0194     return data;
0195 }
0196 
0197 static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
0198             char *buf)
0199 {
0200     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0201     struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
0202     int nr = attr->index;
0203     /*
0204      * This chip (stupidly) stops monitoring fan speed if PWM is
0205      * enabled and duty cycle is 0%. This is fine if the monitoring
0206      * and control concern the same fan, but troublesome if they are
0207      * not (which could as well happen).
0208      */
0209     int rpm = (data->pwm[nr] & 0x7F) == 0x00 ? 0 :
0210           FAN_FROM_REG(data->fan[nr],
0211                    DIV_FROM_REG(data->fan_div[nr]),
0212                    data->fan_preload[nr]);
0213     return sprintf(buf, "%d\n", rpm);
0214 }
0215 
0216 static ssize_t fan_min_show(struct device *dev,
0217                 struct device_attribute *devattr, char *buf)
0218 {
0219     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0220     struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
0221     int nr = attr->index;
0222     int rpm = MIN_FROM_REG(data->fan_preload[nr],
0223                    DIV_FROM_REG(data->fan_div[nr]));
0224     return sprintf(buf, "%d\n", rpm);
0225 }
0226 
0227 static ssize_t fan_div_show(struct device *dev,
0228                 struct device_attribute *devattr, char *buf)
0229 {
0230     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0231     struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
0232     return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
0233 }
0234 
0235 static ssize_t fan_alarm_show(struct device *dev,
0236                   struct device_attribute *devattr, char *buf)
0237 {
0238     int bitnr = to_sensor_dev_attr(devattr)->index;
0239     struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
0240     return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
0241 }
0242 
0243 static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
0244             char *buf)
0245 {
0246     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0247     struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
0248     return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[attr->index]));
0249 }
0250 
0251 static ssize_t pwm_en_show(struct device *dev,
0252                struct device_attribute *devattr, char *buf)
0253 {
0254     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0255     struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
0256     return sprintf(buf, "%d\n", PWM_EN_FROM_REG(data->pwm[attr->index]));
0257 }
0258 
0259 static ssize_t alarms_show(struct device *dev,
0260                struct device_attribute *devattr, char *buf)
0261 {
0262     struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
0263     return sprintf(buf, "%d\n", data->alarms);
0264 }
0265 
0266 static ssize_t fan_min_store(struct device *dev,
0267                  struct device_attribute *devattr,
0268                  const char *buf, size_t count)
0269 {
0270     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0271     struct smsc47m1_data *data = dev_get_drvdata(dev);
0272     int nr = attr->index;
0273     long rpmdiv;
0274     long val;
0275     int err;
0276 
0277     err = kstrtol(buf, 10, &val);
0278     if (err)
0279         return err;
0280 
0281     mutex_lock(&data->update_lock);
0282     rpmdiv = val * DIV_FROM_REG(data->fan_div[nr]);
0283 
0284     if (983040 > 192 * rpmdiv || 2 * rpmdiv > 983040) {
0285         mutex_unlock(&data->update_lock);
0286         return -EINVAL;
0287     }
0288 
0289     data->fan_preload[nr] = 192 - ((983040 + rpmdiv / 2) / rpmdiv);
0290     smsc47m1_write_value(data, SMSC47M1_REG_FAN_PRELOAD[nr],
0291                  data->fan_preload[nr]);
0292     mutex_unlock(&data->update_lock);
0293 
0294     return count;
0295 }
0296 
0297 /*
0298  * Note: we save and restore the fan minimum here, because its value is
0299  * determined in part by the fan clock divider.  This follows the principle
0300  * of least surprise; the user doesn't expect the fan minimum to change just
0301  * because the divider changed.
0302  */
0303 static ssize_t fan_div_store(struct device *dev,
0304                  struct device_attribute *devattr,
0305                  const char *buf, size_t count)
0306 {
0307     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0308     struct smsc47m1_data *data = dev_get_drvdata(dev);
0309     int nr = attr->index;
0310     long new_div;
0311     int err;
0312     long tmp;
0313     u8 old_div = DIV_FROM_REG(data->fan_div[nr]);
0314 
0315     err = kstrtol(buf, 10, &new_div);
0316     if (err)
0317         return err;
0318 
0319     if (new_div == old_div) /* No change */
0320         return count;
0321 
0322     mutex_lock(&data->update_lock);
0323     switch (new_div) {
0324     case 1:
0325         data->fan_div[nr] = 0;
0326         break;
0327     case 2:
0328         data->fan_div[nr] = 1;
0329         break;
0330     case 4:
0331         data->fan_div[nr] = 2;
0332         break;
0333     case 8:
0334         data->fan_div[nr] = 3;
0335         break;
0336     default:
0337         mutex_unlock(&data->update_lock);
0338         return -EINVAL;
0339     }
0340 
0341     switch (nr) {
0342     case 0:
0343     case 1:
0344         tmp = smsc47m1_read_value(data, SMSC47M1_REG_FANDIV)
0345               & ~(0x03 << (4 + 2 * nr));
0346         tmp |= data->fan_div[nr] << (4 + 2 * nr);
0347         smsc47m1_write_value(data, SMSC47M1_REG_FANDIV, tmp);
0348         break;
0349     case 2:
0350         tmp = smsc47m1_read_value(data, SMSC47M2_REG_FANDIV3) & 0xCF;
0351         tmp |= data->fan_div[2] << 4;
0352         smsc47m1_write_value(data, SMSC47M2_REG_FANDIV3, tmp);
0353         break;
0354     default:
0355         BUG();
0356     }
0357 
0358     /* Preserve fan min */
0359     tmp = 192 - (old_div * (192 - data->fan_preload[nr])
0360              + new_div / 2) / new_div;
0361     data->fan_preload[nr] = clamp_val(tmp, 0, 191);
0362     smsc47m1_write_value(data, SMSC47M1_REG_FAN_PRELOAD[nr],
0363                  data->fan_preload[nr]);
0364     mutex_unlock(&data->update_lock);
0365 
0366     return count;
0367 }
0368 
0369 static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
0370              const char *buf, size_t count)
0371 {
0372     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0373     struct smsc47m1_data *data = dev_get_drvdata(dev);
0374     int nr = attr->index;
0375     long val;
0376     int err;
0377 
0378     err = kstrtol(buf, 10, &val);
0379     if (err)
0380         return err;
0381 
0382     if (val < 0 || val > 255)
0383         return -EINVAL;
0384 
0385     mutex_lock(&data->update_lock);
0386     data->pwm[nr] &= 0x81; /* Preserve additional bits */
0387     data->pwm[nr] |= PWM_TO_REG(val);
0388     smsc47m1_write_value(data, SMSC47M1_REG_PWM[nr],
0389                  data->pwm[nr]);
0390     mutex_unlock(&data->update_lock);
0391 
0392     return count;
0393 }
0394 
0395 static ssize_t pwm_en_store(struct device *dev,
0396                 struct device_attribute *devattr, const char *buf,
0397                 size_t count)
0398 {
0399     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0400     struct smsc47m1_data *data = dev_get_drvdata(dev);
0401     int nr = attr->index;
0402     unsigned long val;
0403     int err;
0404 
0405     err = kstrtoul(buf, 10, &val);
0406     if (err)
0407         return err;
0408 
0409     if (val > 1)
0410         return -EINVAL;
0411 
0412     mutex_lock(&data->update_lock);
0413     data->pwm[nr] &= 0xFE; /* preserve the other bits */
0414     data->pwm[nr] |= !val;
0415     smsc47m1_write_value(data, SMSC47M1_REG_PWM[nr],
0416                  data->pwm[nr]);
0417     mutex_unlock(&data->update_lock);
0418 
0419     return count;
0420 }
0421 
0422 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
0423 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
0424 static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
0425 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, fan_alarm, 0);
0426 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
0427 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_en, 0);
0428 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
0429 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
0430 static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
0431 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, fan_alarm, 1);
0432 static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
0433 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_en, 1);
0434 static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2);
0435 static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
0436 static SENSOR_DEVICE_ATTR_RW(fan3_div, fan_div, 2);
0437 static SENSOR_DEVICE_ATTR_RO(fan3_alarm, fan_alarm, 2);
0438 static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, 2);
0439 static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_en, 2);
0440 
0441 static DEVICE_ATTR_RO(alarms);
0442 
0443 static ssize_t name_show(struct device *dev, struct device_attribute
0444              *devattr, char *buf)
0445 {
0446     struct smsc47m1_data *data = dev_get_drvdata(dev);
0447 
0448     return sprintf(buf, "%s\n", data->name);
0449 }
0450 static DEVICE_ATTR_RO(name);
0451 
0452 static struct attribute *smsc47m1_attributes_fan1[] = {
0453     &sensor_dev_attr_fan1_input.dev_attr.attr,
0454     &sensor_dev_attr_fan1_min.dev_attr.attr,
0455     &sensor_dev_attr_fan1_div.dev_attr.attr,
0456     &sensor_dev_attr_fan1_alarm.dev_attr.attr,
0457     NULL
0458 };
0459 
0460 static const struct attribute_group smsc47m1_group_fan1 = {
0461     .attrs = smsc47m1_attributes_fan1,
0462 };
0463 
0464 static struct attribute *smsc47m1_attributes_fan2[] = {
0465     &sensor_dev_attr_fan2_input.dev_attr.attr,
0466     &sensor_dev_attr_fan2_min.dev_attr.attr,
0467     &sensor_dev_attr_fan2_div.dev_attr.attr,
0468     &sensor_dev_attr_fan2_alarm.dev_attr.attr,
0469     NULL
0470 };
0471 
0472 static const struct attribute_group smsc47m1_group_fan2 = {
0473     .attrs = smsc47m1_attributes_fan2,
0474 };
0475 
0476 static struct attribute *smsc47m1_attributes_fan3[] = {
0477     &sensor_dev_attr_fan3_input.dev_attr.attr,
0478     &sensor_dev_attr_fan3_min.dev_attr.attr,
0479     &sensor_dev_attr_fan3_div.dev_attr.attr,
0480     &sensor_dev_attr_fan3_alarm.dev_attr.attr,
0481     NULL
0482 };
0483 
0484 static const struct attribute_group smsc47m1_group_fan3 = {
0485     .attrs = smsc47m1_attributes_fan3,
0486 };
0487 
0488 static struct attribute *smsc47m1_attributes_pwm1[] = {
0489     &sensor_dev_attr_pwm1.dev_attr.attr,
0490     &sensor_dev_attr_pwm1_enable.dev_attr.attr,
0491     NULL
0492 };
0493 
0494 static const struct attribute_group smsc47m1_group_pwm1 = {
0495     .attrs = smsc47m1_attributes_pwm1,
0496 };
0497 
0498 static struct attribute *smsc47m1_attributes_pwm2[] = {
0499     &sensor_dev_attr_pwm2.dev_attr.attr,
0500     &sensor_dev_attr_pwm2_enable.dev_attr.attr,
0501     NULL
0502 };
0503 
0504 static const struct attribute_group smsc47m1_group_pwm2 = {
0505     .attrs = smsc47m1_attributes_pwm2,
0506 };
0507 
0508 static struct attribute *smsc47m1_attributes_pwm3[] = {
0509     &sensor_dev_attr_pwm3.dev_attr.attr,
0510     &sensor_dev_attr_pwm3_enable.dev_attr.attr,
0511     NULL
0512 };
0513 
0514 static const struct attribute_group smsc47m1_group_pwm3 = {
0515     .attrs = smsc47m1_attributes_pwm3,
0516 };
0517 
0518 static struct attribute *smsc47m1_attributes[] = {
0519     &dev_attr_alarms.attr,
0520     &dev_attr_name.attr,
0521     NULL
0522 };
0523 
0524 static const struct attribute_group smsc47m1_group = {
0525     .attrs = smsc47m1_attributes,
0526 };
0527 
0528 static int __init smsc47m1_find(struct smsc47m1_sio_data *sio_data)
0529 {
0530     u8 val;
0531     unsigned short addr;
0532     int err;
0533 
0534     err = superio_enter();
0535     if (err)
0536         return err;
0537 
0538     val = force_id ? force_id : superio_inb(SUPERIO_REG_DEVID);
0539 
0540     /*
0541      * SMSC LPC47M10x/LPC47M112/LPC47M13x (device id 0x59), LPC47M14x
0542      * (device id 0x5F) and LPC47B27x (device id 0x51) have fan control.
0543      * The LPC47M15x and LPC47M192 chips "with hardware monitoring block"
0544      * can do much more besides (device id 0x60).
0545      * The LPC47M997 is undocumented, but seems to be compatible with
0546      * the LPC47M192, and has the same device id.
0547      * The LPC47M292 (device id 0x6B) is somewhat compatible, but it
0548      * supports a 3rd fan, and the pin configuration registers are
0549      * unfortunately different.
0550      * The LPC47M233 has the same device id (0x6B) but is not compatible.
0551      * We check the high bit of the device revision register to
0552      * differentiate them.
0553      */
0554     switch (val) {
0555     case 0x51:
0556         pr_info("Found SMSC LPC47B27x\n");
0557         sio_data->type = smsc47m1;
0558         break;
0559     case 0x59:
0560         pr_info("Found SMSC LPC47M10x/LPC47M112/LPC47M13x\n");
0561         sio_data->type = smsc47m1;
0562         break;
0563     case 0x5F:
0564         pr_info("Found SMSC LPC47M14x\n");
0565         sio_data->type = smsc47m1;
0566         break;
0567     case 0x60:
0568         pr_info("Found SMSC LPC47M15x/LPC47M192/LPC47M997\n");
0569         sio_data->type = smsc47m1;
0570         break;
0571     case 0x6B:
0572         if (superio_inb(SUPERIO_REG_DEVREV) & 0x80) {
0573             pr_debug("Found SMSC LPC47M233, unsupported\n");
0574             superio_exit();
0575             return -ENODEV;
0576         }
0577 
0578         pr_info("Found SMSC LPC47M292\n");
0579         sio_data->type = smsc47m2;
0580         break;
0581     default:
0582         superio_exit();
0583         return -ENODEV;
0584     }
0585 
0586     superio_select();
0587     addr = (superio_inb(SUPERIO_REG_BASE) << 8)
0588           |  superio_inb(SUPERIO_REG_BASE + 1);
0589     if (addr == 0) {
0590         pr_info("Device address not set, will not use\n");
0591         superio_exit();
0592         return -ENODEV;
0593     }
0594 
0595     /*
0596      * Enable only if address is set (needed at least on the
0597      * Compaq Presario S4000NX)
0598      */
0599     sio_data->activate = superio_inb(SUPERIO_REG_ACT);
0600     if ((sio_data->activate & 0x01) == 0) {
0601         pr_info("Enabling device\n");
0602         superio_outb(SUPERIO_REG_ACT, sio_data->activate | 0x01);
0603     }
0604 
0605     superio_exit();
0606     return addr;
0607 }
0608 
0609 /* Restore device to its initial state */
0610 static void smsc47m1_restore(const struct smsc47m1_sio_data *sio_data)
0611 {
0612     if ((sio_data->activate & 0x01) == 0) {
0613         if (!superio_enter()) {
0614             superio_select();
0615             pr_info("Disabling device\n");
0616             superio_outb(SUPERIO_REG_ACT, sio_data->activate);
0617             superio_exit();
0618         } else {
0619             pr_warn("Failed to disable device\n");
0620         }
0621     }
0622 }
0623 
0624 #define CHECK       1
0625 #define REQUEST     2
0626 
0627 /*
0628  * This function can be used to:
0629  *  - test for resource conflicts with ACPI
0630  *  - request the resources
0631  * We only allocate the I/O ports we really need, to minimize the risk of
0632  * conflicts with ACPI or with other drivers.
0633  */
0634 static int __init smsc47m1_handle_resources(unsigned short address,
0635                         enum chips type, int action,
0636                         struct device *dev)
0637 {
0638     static const u8 ports_m1[] = {
0639         /* register, region length */
0640         0x04, 1,
0641         0x33, 4,
0642         0x56, 7,
0643     };
0644 
0645     static const u8 ports_m2[] = {
0646         /* register, region length */
0647         0x04, 1,
0648         0x09, 1,
0649         0x2c, 2,
0650         0x35, 4,
0651         0x56, 7,
0652         0x69, 4,
0653     };
0654 
0655     int i, ports_size, err;
0656     const u8 *ports;
0657 
0658     switch (type) {
0659     case smsc47m1:
0660     default:
0661         ports = ports_m1;
0662         ports_size = ARRAY_SIZE(ports_m1);
0663         break;
0664     case smsc47m2:
0665         ports = ports_m2;
0666         ports_size = ARRAY_SIZE(ports_m2);
0667         break;
0668     }
0669 
0670     for (i = 0; i + 1 < ports_size; i += 2) {
0671         unsigned short start = address + ports[i];
0672         unsigned short len = ports[i + 1];
0673 
0674         switch (action) {
0675         case CHECK:
0676             /* Only check for conflicts */
0677             err = acpi_check_region(start, len, DRVNAME);
0678             if (err)
0679                 return err;
0680             break;
0681         case REQUEST:
0682             /* Request the resources */
0683             if (!devm_request_region(dev, start, len, DRVNAME)) {
0684                 dev_err(dev,
0685                     "Region 0x%x-0x%x already in use!\n",
0686                     start, start + len);
0687                 return -EBUSY;
0688             }
0689             break;
0690         }
0691     }
0692 
0693     return 0;
0694 }
0695 
0696 static void smsc47m1_remove_files(struct device *dev)
0697 {
0698     sysfs_remove_group(&dev->kobj, &smsc47m1_group);
0699     sysfs_remove_group(&dev->kobj, &smsc47m1_group_fan1);
0700     sysfs_remove_group(&dev->kobj, &smsc47m1_group_fan2);
0701     sysfs_remove_group(&dev->kobj, &smsc47m1_group_fan3);
0702     sysfs_remove_group(&dev->kobj, &smsc47m1_group_pwm1);
0703     sysfs_remove_group(&dev->kobj, &smsc47m1_group_pwm2);
0704     sysfs_remove_group(&dev->kobj, &smsc47m1_group_pwm3);
0705 }
0706 
0707 static int __init smsc47m1_probe(struct platform_device *pdev)
0708 {
0709     struct device *dev = &pdev->dev;
0710     struct smsc47m1_sio_data *sio_data = dev_get_platdata(dev);
0711     struct smsc47m1_data *data;
0712     struct resource *res;
0713     int err;
0714     int fan1, fan2, fan3, pwm1, pwm2, pwm3;
0715 
0716     static const char * const names[] = {
0717         "smsc47m1",
0718         "smsc47m2",
0719     };
0720 
0721     res = platform_get_resource(pdev, IORESOURCE_IO, 0);
0722     err = smsc47m1_handle_resources(res->start, sio_data->type,
0723                     REQUEST, dev);
0724     if (err < 0)
0725         return err;
0726 
0727     data = devm_kzalloc(dev, sizeof(struct smsc47m1_data), GFP_KERNEL);
0728     if (!data)
0729         return -ENOMEM;
0730 
0731     data->addr = res->start;
0732     data->type = sio_data->type;
0733     data->name = names[sio_data->type];
0734     mutex_init(&data->update_lock);
0735     platform_set_drvdata(pdev, data);
0736 
0737     /*
0738      * If no function is properly configured, there's no point in
0739      * actually registering the chip.
0740      */
0741     pwm1 = (smsc47m1_read_value(data, SMSC47M1_REG_PPIN(0)) & 0x05)
0742            == 0x04;
0743     pwm2 = (smsc47m1_read_value(data, SMSC47M1_REG_PPIN(1)) & 0x05)
0744            == 0x04;
0745     if (data->type == smsc47m2) {
0746         fan1 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN1)
0747             & 0x0d) == 0x09;
0748         fan2 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN2)
0749             & 0x0d) == 0x09;
0750         fan3 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN3)
0751             & 0x0d) == 0x0d;
0752         pwm3 = (smsc47m1_read_value(data, SMSC47M2_REG_PPIN3)
0753             & 0x0d) == 0x08;
0754     } else {
0755         fan1 = (smsc47m1_read_value(data, SMSC47M1_REG_TPIN(0))
0756             & 0x05) == 0x05;
0757         fan2 = (smsc47m1_read_value(data, SMSC47M1_REG_TPIN(1))
0758             & 0x05) == 0x05;
0759         fan3 = 0;
0760         pwm3 = 0;
0761     }
0762     if (!(fan1 || fan2 || fan3 || pwm1 || pwm2 || pwm3)) {
0763         dev_warn(dev, "Device not configured, will not use\n");
0764         return -ENODEV;
0765     }
0766 
0767     /*
0768      * Some values (fan min, clock dividers, pwm registers) may be
0769      * needed before any update is triggered, so we better read them
0770      * at least once here. We don't usually do it that way, but in
0771      * this particular case, manually reading 5 registers out of 8
0772      * doesn't make much sense and we're better using the existing
0773      * function.
0774      */
0775     smsc47m1_update_device(dev, 1);
0776 
0777     /* Register sysfs hooks */
0778     if (fan1) {
0779         err = sysfs_create_group(&dev->kobj,
0780                      &smsc47m1_group_fan1);
0781         if (err)
0782             goto error_remove_files;
0783     } else
0784         dev_dbg(dev, "Fan 1 not enabled by hardware, skipping\n");
0785 
0786     if (fan2) {
0787         err = sysfs_create_group(&dev->kobj,
0788                      &smsc47m1_group_fan2);
0789         if (err)
0790             goto error_remove_files;
0791     } else
0792         dev_dbg(dev, "Fan 2 not enabled by hardware, skipping\n");
0793 
0794     if (fan3) {
0795         err = sysfs_create_group(&dev->kobj,
0796                      &smsc47m1_group_fan3);
0797         if (err)
0798             goto error_remove_files;
0799     } else if (data->type == smsc47m2)
0800         dev_dbg(dev, "Fan 3 not enabled by hardware, skipping\n");
0801 
0802     if (pwm1) {
0803         err = sysfs_create_group(&dev->kobj,
0804                      &smsc47m1_group_pwm1);
0805         if (err)
0806             goto error_remove_files;
0807     } else
0808         dev_dbg(dev, "PWM 1 not enabled by hardware, skipping\n");
0809 
0810     if (pwm2) {
0811         err = sysfs_create_group(&dev->kobj,
0812                      &smsc47m1_group_pwm2);
0813         if (err)
0814             goto error_remove_files;
0815     } else
0816         dev_dbg(dev, "PWM 2 not enabled by hardware, skipping\n");
0817 
0818     if (pwm3) {
0819         err = sysfs_create_group(&dev->kobj,
0820                      &smsc47m1_group_pwm3);
0821         if (err)
0822             goto error_remove_files;
0823     } else if (data->type == smsc47m2)
0824         dev_dbg(dev, "PWM 3 not enabled by hardware, skipping\n");
0825 
0826     err = sysfs_create_group(&dev->kobj, &smsc47m1_group);
0827     if (err)
0828         goto error_remove_files;
0829 
0830     data->hwmon_dev = hwmon_device_register(dev);
0831     if (IS_ERR(data->hwmon_dev)) {
0832         err = PTR_ERR(data->hwmon_dev);
0833         goto error_remove_files;
0834     }
0835 
0836     return 0;
0837 
0838 error_remove_files:
0839     smsc47m1_remove_files(dev);
0840     return err;
0841 }
0842 
0843 static int __exit smsc47m1_remove(struct platform_device *pdev)
0844 {
0845     struct smsc47m1_data *data = platform_get_drvdata(pdev);
0846 
0847     hwmon_device_unregister(data->hwmon_dev);
0848     smsc47m1_remove_files(&pdev->dev);
0849 
0850     return 0;
0851 }
0852 
0853 static struct platform_driver smsc47m1_driver = {
0854     .driver = {
0855         .name   = DRVNAME,
0856     },
0857     .remove     = __exit_p(smsc47m1_remove),
0858 };
0859 
0860 static int __init smsc47m1_device_add(unsigned short address,
0861                       const struct smsc47m1_sio_data *sio_data)
0862 {
0863     struct resource res = {
0864         .start  = address,
0865         .end    = address + SMSC_EXTENT - 1,
0866         .name   = DRVNAME,
0867         .flags  = IORESOURCE_IO,
0868     };
0869     int err;
0870 
0871     err = smsc47m1_handle_resources(address, sio_data->type, CHECK, NULL);
0872     if (err)
0873         goto exit;
0874 
0875     pdev = platform_device_alloc(DRVNAME, address);
0876     if (!pdev) {
0877         err = -ENOMEM;
0878         pr_err("Device allocation failed\n");
0879         goto exit;
0880     }
0881 
0882     err = platform_device_add_resources(pdev, &res, 1);
0883     if (err) {
0884         pr_err("Device resource addition failed (%d)\n", err);
0885         goto exit_device_put;
0886     }
0887 
0888     err = platform_device_add_data(pdev, sio_data,
0889                        sizeof(struct smsc47m1_sio_data));
0890     if (err) {
0891         pr_err("Platform data allocation failed\n");
0892         goto exit_device_put;
0893     }
0894 
0895     err = platform_device_add(pdev);
0896     if (err) {
0897         pr_err("Device addition failed (%d)\n", err);
0898         goto exit_device_put;
0899     }
0900 
0901     return 0;
0902 
0903 exit_device_put:
0904     platform_device_put(pdev);
0905 exit:
0906     return err;
0907 }
0908 
0909 static int __init sm_smsc47m1_init(void)
0910 {
0911     int err;
0912     unsigned short address;
0913     struct smsc47m1_sio_data sio_data;
0914 
0915     err = smsc47m1_find(&sio_data);
0916     if (err < 0)
0917         return err;
0918     address = err;
0919 
0920     /* Sets global pdev as a side effect */
0921     err = smsc47m1_device_add(address, &sio_data);
0922     if (err)
0923         return err;
0924 
0925     err = platform_driver_probe(&smsc47m1_driver, smsc47m1_probe);
0926     if (err)
0927         goto exit_device;
0928 
0929     return 0;
0930 
0931 exit_device:
0932     platform_device_unregister(pdev);
0933     smsc47m1_restore(&sio_data);
0934     return err;
0935 }
0936 
0937 static void __exit sm_smsc47m1_exit(void)
0938 {
0939     platform_driver_unregister(&smsc47m1_driver);
0940     smsc47m1_restore(dev_get_platdata(&pdev->dev));
0941     platform_device_unregister(pdev);
0942 }
0943 
0944 MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>");
0945 MODULE_DESCRIPTION("SMSC LPC47M1xx fan sensors driver");
0946 MODULE_LICENSE("GPL");
0947 
0948 module_init(sm_smsc47m1_init);
0949 module_exit(sm_smsc47m1_exit);