]> bbs.cooldavid.org Git - net-next-2.6.git/blobdiff - drivers/hwmon/pc87427.c
hwmon: (pc87427) Minor style cleanups
[net-next-2.6.git] / drivers / hwmon / pc87427.c
index 3170b26d2443b76bfbd6b518153c7329d622280c..0ac55ba6c6b847d8c8d1c2f72c343a05a631766f 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  pc87427.c - hardware monitoring driver for the
  *              National Semiconductor PC87427 Super-I/O chip
- *  Copyright (C) 2006 Jean Delvare <khali@linux-fr.org>
+ *  Copyright (C) 2006, 2008  Jean Delvare <khali@linux-fr.org>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 as
@@ -59,12 +59,21 @@ struct pc87427_data {
        u8 fan_status[8];               /* register values */
 };
 
+struct pc87427_sio_data {
+       u8 has_fanin;
+};
+
 /*
  * Super-I/O registers and operations
  */
 
 #define SIOREG_LDSEL   0x07    /* Logical device select */
 #define SIOREG_DEVID   0x20    /* Device ID */
+#define SIOREG_CF2     0x22    /* Configuration 2 */
+#define SIOREG_CF3     0x23    /* Configuration 3 */
+#define SIOREG_CF4     0x24    /* Configuration 4 */
+#define SIOREG_CFB     0x2B    /* Configuration B */
+#define SIOREG_CFD     0x2D    /* Configuration D */
 #define SIOREG_ACT     0x30    /* Device activation */
 #define SIOREG_MAP     0x50    /* I/O or memory mapping */
 #define SIOREG_IOBASE  0x60    /* I/O base address */
@@ -208,9 +217,8 @@ done:
 static ssize_t show_fan_input(struct device *dev, struct device_attribute
                              *devattr, char *buf)
 {
-       struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
        struct pc87427_data *data = pc87427_update_device(dev);
-       int nr = attr->index;
+       int nr = to_sensor_dev_attr(devattr)->index;
 
        return sprintf(buf, "%lu\n", fan_from_reg(data->fan[nr]));
 }
@@ -218,9 +226,8 @@ static ssize_t show_fan_input(struct device *dev, struct device_attribute
 static ssize_t show_fan_min(struct device *dev, struct device_attribute
                            *devattr, char *buf)
 {
-       struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
        struct pc87427_data *data = pc87427_update_device(dev);
-       int nr = attr->index;
+       int nr = to_sensor_dev_attr(devattr)->index;
 
        return sprintf(buf, "%lu\n", fan_from_reg(data->fan_min[nr]));
 }
@@ -228,9 +235,8 @@ static ssize_t show_fan_min(struct device *dev, struct device_attribute
 static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
                              *devattr, char *buf)
 {
-       struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
        struct pc87427_data *data = pc87427_update_device(dev);
-       int nr = attr->index;
+       int nr = to_sensor_dev_attr(devattr)->index;
 
        return sprintf(buf, "%d\n", !!(data->fan_status[nr]
                                       & FAN_STATUS_LOSPD));
@@ -239,9 +245,8 @@ static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
 static ssize_t show_fan_fault(struct device *dev, struct device_attribute
                              *devattr, char *buf)
 {
-       struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
        struct pc87427_data *data = pc87427_update_device(dev);
-       int nr = attr->index;
+       int nr = to_sensor_dev_attr(devattr)->index;
 
        return sprintf(buf, "%d\n", !!(data->fan_status[nr]
                                       & FAN_STATUS_STALL));
@@ -251,11 +256,13 @@ static ssize_t set_fan_min(struct device *dev, struct device_attribute
                           *devattr, const char *buf, size_t count)
 {
        struct pc87427_data *data = dev_get_drvdata(dev);
-       struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
-       int nr = attr->index;
-       unsigned long val = simple_strtoul(buf, NULL, 10);
+       int nr = to_sensor_dev_attr(devattr)->index;
+       unsigned long val;
        int iobase = data->address[LD_FAN];
 
+       if (strict_strtoul(buf, 10, &val) < 0)
+               return -EINVAL;
+
        mutex_lock(&data->lock);
        outb(BANK_FM(nr), iobase + PC87427_REG_BANK);
        /* The low speed limit registers are read-only while monitoring
@@ -393,6 +400,7 @@ static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
 
 static void __devinit pc87427_init_device(struct device *dev)
 {
+       struct pc87427_sio_data *sio_data = dev->platform_data;
        struct pc87427_data *data = dev_get_drvdata(dev);
        int i;
        u8 reg;
@@ -404,6 +412,8 @@ static void __devinit pc87427_init_device(struct device *dev)
 
        /* Check which fans are enabled */
        for (i = 0; i < 8; i++) {
+               if (!(sio_data->has_fanin & (1 << i)))  /* Not wired */
+                       continue;
                reg = pc87427_read8_bank(data, LD_FAN, BANK_FM(i),
                                         PC87427_REG_FAN_STATUS);
                if (reg & FAN_STATUS_MONEN)
@@ -411,12 +421,15 @@ static void __devinit pc87427_init_device(struct device *dev)
        }
 
        if (!data->fan_enabled) {
-               dev_dbg(dev, "Enabling all fan inputs\n");
-               for (i = 0; i < 8; i++)
+               dev_dbg(dev, "Enabling monitoring of all fans\n");
+               for (i = 0; i < 8; i++) {
+                       if (!(sio_data->has_fanin & (1 << i)))  /* Not wired */
+                               continue;
                        pc87427_write8_bank(data, LD_FAN, BANK_FM(i),
                                            PC87427_REG_FAN_STATUS,
                                            FAN_STATUS_MONEN);
-               data->fan_enabled = 0xff;
+               }
+               data->fan_enabled = sio_data->has_fanin;
        }
 }
 
@@ -426,7 +439,8 @@ static int __devinit pc87427_probe(struct platform_device *pdev)
        struct resource *res;
        int i, err;
 
-       if (!(data = kzalloc(sizeof(struct pc87427_data), GFP_KERNEL))) {
+       data = kzalloc(sizeof(struct pc87427_data), GFP_KERNEL);
+       if (!data) {
                err = -ENOMEM;
                printk(KERN_ERR DRVNAME ": Out of memory\n");
                goto exit;
@@ -449,13 +463,15 @@ static int __devinit pc87427_probe(struct platform_device *pdev)
        pc87427_init_device(&pdev->dev);
 
        /* Register sysfs hooks */
-       if ((err = device_create_file(&pdev->dev, &dev_attr_name)))
+       err = device_create_file(&pdev->dev, &dev_attr_name);
+       if (err)
                goto exit_release_region;
        for (i = 0; i < 8; i++) {
                if (!(data->fan_enabled & (1 << i)))
                        continue;
-               if ((err = sysfs_create_group(&pdev->dev.kobj,
-                                             &pc87427_group_fan[i])))
+               err = sysfs_create_group(&pdev->dev.kobj,
+                                        &pc87427_group_fan[i]);
+               if (err)
                        goto exit_remove_files;
        }
 
@@ -515,7 +531,8 @@ static struct platform_driver pc87427_driver = {
        .remove         = __devexit_p(pc87427_remove),
 };
 
-static int __init pc87427_device_add(unsigned short address)
+static int __init pc87427_device_add(unsigned short address,
+                                    const struct pc87427_sio_data *sio_data)
 {
        struct resource res = {
                .start  = address,
@@ -543,6 +560,13 @@ static int __init pc87427_device_add(unsigned short address)
                goto exit_device_put;
        }
 
+       err = platform_device_add_data(pdev, sio_data,
+                                      sizeof(struct pc87427_sio_data));
+       if (err) {
+               printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
+               goto exit_device_put;
+       }
+
        err = platform_device_add(pdev);
        if (err) {
                printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
@@ -558,9 +582,11 @@ exit:
        return err;
 }
 
-static int __init pc87427_find(int sioaddr, unsigned short *address)
+static int __init pc87427_find(int sioaddr, unsigned short *address,
+                              struct pc87427_sio_data *sio_data)
 {
        u16 val;
+       u8 cfg, cfg_b;
        int i, err = 0;
 
        /* Identify device */
@@ -599,6 +625,29 @@ static int __init pc87427_find(int sioaddr, unsigned short *address)
                address[i] = val;
        }
 
+       /* Check which fan inputs are wired */
+       sio_data->has_fanin = (1 << 2) | (1 << 3);      /* FANIN2, FANIN3 */
+
+       cfg = superio_inb(sioaddr, SIOREG_CF2);
+       if (!(cfg & (1 << 3)))
+               sio_data->has_fanin |= (1 << 0);        /* FANIN0 */
+       if (!(cfg & (1 << 2)))
+               sio_data->has_fanin |= (1 << 4);        /* FANIN4 */
+
+       cfg = superio_inb(sioaddr, SIOREG_CFD);
+       if (!(cfg & (1 << 0)))
+               sio_data->has_fanin |= (1 << 1);        /* FANIN1 */
+
+       cfg = superio_inb(sioaddr, SIOREG_CF4);
+       if (!(cfg & (1 << 0)))
+               sio_data->has_fanin |= (1 << 7);        /* FANIN7 */
+       cfg_b = superio_inb(sioaddr, SIOREG_CFB);
+       if (!(cfg & (1 << 1)) && (cfg_b & (1 << 3)))
+               sio_data->has_fanin |= (1 << 5);        /* FANIN5 */
+       cfg = superio_inb(sioaddr, SIOREG_CF3);
+       if ((cfg & (1 << 3)) && !(cfg_b & (1 << 5)))
+               sio_data->has_fanin |= (1 << 6);        /* FANIN6 */
+
 exit:
        superio_exit(sioaddr);
        return err;
@@ -608,9 +657,10 @@ static int __init pc87427_init(void)
 {
        int err;
        unsigned short address[2];
+       struct pc87427_sio_data sio_data;
 
-       if (pc87427_find(0x2e, address)
-        && pc87427_find(0x4e, address))
+       if (pc87427_find(0x2e, address, &sio_data)
+        && pc87427_find(0x4e, address, &sio_data))
                return -ENODEV;
 
        /* For now the driver only handles fans so we only care about the
@@ -623,7 +673,7 @@ static int __init pc87427_init(void)
                goto exit;
 
        /* Sets global pdev as a side effect */
-       err = pc87427_device_add(address[0]);
+       err = pc87427_device_add(address[0], &sio_data);
        if (err)
                goto exit_driver;