linux/drivers/hwmon/smsc47b397.c
<<
>>
Prefs
   1/*
   2    smsc47b397.c - Part of lm_sensors, Linux kernel modules
   3                        for hardware monitoring
   4
   5    Supports the SMSC LPC47B397-NC Super-I/O chip.
   6
   7    Author/Maintainer: Mark M. Hoffman <mhoffman@lightlink.com>
   8        Copyright (C) 2004 Utilitek Systems, Inc.
   9
  10    derived in part from smsc47m1.c:
  11        Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
  12        Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
  13
  14    This program is free software; you can redistribute it and/or modify
  15    it under the terms of the GNU General Public License as published by
  16    the Free Software Foundation; either version 2 of the License, or
  17    (at your option) any later version.
  18
  19    This program is distributed in the hope that it will be useful,
  20    but WITHOUT ANY WARRANTY; without even the implied warranty of
  21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22    GNU General Public License for more details.
  23
  24    You should have received a copy of the GNU General Public License
  25    along with this program; if not, write to the Free Software
  26    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27*/
  28
  29#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30
  31#include <linux/module.h>
  32#include <linux/slab.h>
  33#include <linux/ioport.h>
  34#include <linux/jiffies.h>
  35#include <linux/platform_device.h>
  36#include <linux/hwmon.h>
  37#include <linux/hwmon-sysfs.h>
  38#include <linux/err.h>
  39#include <linux/init.h>
  40#include <linux/mutex.h>
  41#include <linux/acpi.h>
  42#include <linux/io.h>
  43
  44static unsigned short force_id;
  45module_param(force_id, ushort, 0);
  46MODULE_PARM_DESC(force_id, "Override the detected device ID");
  47
  48static struct platform_device *pdev;
  49
  50#define DRVNAME "smsc47b397"
  51
  52/* Super-I/0 registers and commands */
  53
  54#define REG     0x2e    /* The register to read/write */
  55#define VAL     0x2f    /* The value to read/write */
  56
  57static inline void superio_outb(int reg, int val)
  58{
  59        outb(reg, REG);
  60        outb(val, VAL);
  61}
  62
  63static inline int superio_inb(int reg)
  64{
  65        outb(reg, REG);
  66        return inb(VAL);
  67}
  68
  69/* select superio logical device */
  70static inline void superio_select(int ld)
  71{
  72        superio_outb(0x07, ld);
  73}
  74
  75static inline void superio_enter(void)
  76{
  77        outb(0x55, REG);
  78}
  79
  80static inline void superio_exit(void)
  81{
  82        outb(0xAA, REG);
  83}
  84
  85#define SUPERIO_REG_DEVID       0x20
  86#define SUPERIO_REG_DEVREV      0x21
  87#define SUPERIO_REG_BASE_MSB    0x60
  88#define SUPERIO_REG_BASE_LSB    0x61
  89#define SUPERIO_REG_LD8         0x08
  90
  91#define SMSC_EXTENT             0x02
  92
  93/* 0 <= nr <= 3 */
  94static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
  95#define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
  96
  97/* 0 <= nr <= 3 */
  98#define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
  99#define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
 100
 101struct smsc47b397_data {
 102        unsigned short addr;
 103        const char *name;
 104        struct device *hwmon_dev;
 105        struct mutex lock;
 106
 107        struct mutex update_lock;
 108        unsigned long last_updated; /* in jiffies */
 109        int valid;
 110
 111        /* register values */
 112        u16 fan[4];
 113        u8 temp[4];
 114};
 115
 116static int smsc47b397_read_value(struct smsc47b397_data* data, u8 reg)
 117{
 118        int res;
 119
 120        mutex_lock(&data->lock);
 121        outb(reg, data->addr);
 122        res = inb_p(data->addr + 1);
 123        mutex_unlock(&data->lock);
 124        return res;
 125}
 126
 127static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
 128{
 129        struct smsc47b397_data *data = dev_get_drvdata(dev);
 130        int i;
 131
 132        mutex_lock(&data->update_lock);
 133
 134        if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
 135                dev_dbg(dev, "starting device update...\n");
 136
 137                /* 4 temperature inputs, 4 fan inputs */
 138                for (i = 0; i < 4; i++) {
 139                        data->temp[i] = smsc47b397_read_value(data,
 140                                        SMSC47B397_REG_TEMP(i));
 141
 142                        /* must read LSB first */
 143                        data->fan[i]  = smsc47b397_read_value(data,
 144                                        SMSC47B397_REG_FAN_LSB(i));
 145                        data->fan[i] |= smsc47b397_read_value(data,
 146                                        SMSC47B397_REG_FAN_MSB(i)) << 8;
 147                }
 148
 149                data->last_updated = jiffies;
 150                data->valid = 1;
 151
 152                dev_dbg(dev, "... device update complete\n");
 153        }
 154
 155        mutex_unlock(&data->update_lock);
 156
 157        return data;
 158}
 159
 160/* TEMP: 0.001C/bit (-128C to +127C)
 161   REG: 1C/bit, two's complement */
 162static int temp_from_reg(u8 reg)
 163{
 164        return (s8)reg * 1000;
 165}
 166
 167static ssize_t show_temp(struct device *dev, struct device_attribute
 168                         *devattr, char *buf)
 169{
 170        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 171        struct smsc47b397_data *data = smsc47b397_update_device(dev);
 172        return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index]));
 173}
 174
 175static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
 176static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
 177static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
 178static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
 179
 180/* FAN: 1 RPM/bit
 181   REG: count of 90kHz pulses / revolution */
 182static int fan_from_reg(u16 reg)
 183{
 184        if (reg == 0 || reg == 0xffff)
 185                return 0;
 186        return 90000 * 60 / reg;
 187}
 188
 189static ssize_t show_fan(struct device *dev, struct device_attribute
 190                        *devattr, char *buf)
 191{
 192        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 193        struct smsc47b397_data *data = smsc47b397_update_device(dev);
 194        return sprintf(buf, "%d\n", fan_from_reg(data->fan[attr->index]));
 195}
 196static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
 197static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
 198static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
 199static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
 200
 201static ssize_t show_name(struct device *dev, struct device_attribute
 202                         *devattr, char *buf)
 203{
 204        struct smsc47b397_data *data = dev_get_drvdata(dev);
 205        return sprintf(buf, "%s\n", data->name);
 206}
 207static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
 208
 209static struct attribute *smsc47b397_attributes[] = {
 210        &sensor_dev_attr_temp1_input.dev_attr.attr,
 211        &sensor_dev_attr_temp2_input.dev_attr.attr,
 212        &sensor_dev_attr_temp3_input.dev_attr.attr,
 213        &sensor_dev_attr_temp4_input.dev_attr.attr,
 214        &sensor_dev_attr_fan1_input.dev_attr.attr,
 215        &sensor_dev_attr_fan2_input.dev_attr.attr,
 216        &sensor_dev_attr_fan3_input.dev_attr.attr,
 217        &sensor_dev_attr_fan4_input.dev_attr.attr,
 218
 219        &dev_attr_name.attr,
 220        NULL
 221};
 222
 223static const struct attribute_group smsc47b397_group = {
 224        .attrs = smsc47b397_attributes,
 225};
 226
 227static int __devexit smsc47b397_remove(struct platform_device *pdev)
 228{
 229        struct smsc47b397_data *data = platform_get_drvdata(pdev);
 230        struct resource *res;
 231
 232        hwmon_device_unregister(data->hwmon_dev);
 233        sysfs_remove_group(&pdev->dev.kobj, &smsc47b397_group);
 234        res = platform_get_resource(pdev, IORESOURCE_IO, 0);
 235        release_region(res->start, SMSC_EXTENT);
 236        kfree(data);
 237
 238        return 0;
 239}
 240
 241static int smsc47b397_probe(struct platform_device *pdev);
 242
 243static struct platform_driver smsc47b397_driver = {
 244        .driver = {
 245                .owner  = THIS_MODULE,
 246                .name   = DRVNAME,
 247        },
 248        .probe          = smsc47b397_probe,
 249        .remove         = __devexit_p(smsc47b397_remove),
 250};
 251
 252static int __devinit smsc47b397_probe(struct platform_device *pdev)
 253{
 254        struct device *dev = &pdev->dev;
 255        struct smsc47b397_data *data;
 256        struct resource *res;
 257        int err = 0;
 258
 259        res = platform_get_resource(pdev, IORESOURCE_IO, 0);
 260        if (!request_region(res->start, SMSC_EXTENT,
 261                            smsc47b397_driver.driver.name)) {
 262                dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
 263                        (unsigned long)res->start,
 264                        (unsigned long)res->start + SMSC_EXTENT - 1);
 265                return -EBUSY;
 266        }
 267
 268        if (!(data = kzalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) {
 269                err = -ENOMEM;
 270                goto error_release;
 271        }
 272
 273        data->addr = res->start;
 274        data->name = "smsc47b397";
 275        mutex_init(&data->lock);
 276        mutex_init(&data->update_lock);
 277        platform_set_drvdata(pdev, data);
 278
 279        if ((err = sysfs_create_group(&dev->kobj, &smsc47b397_group)))
 280                goto error_free;
 281
 282        data->hwmon_dev = hwmon_device_register(dev);
 283        if (IS_ERR(data->hwmon_dev)) {
 284                err = PTR_ERR(data->hwmon_dev);
 285                goto error_remove;
 286        }
 287
 288        return 0;
 289
 290error_remove:
 291        sysfs_remove_group(&dev->kobj, &smsc47b397_group);
 292error_free:
 293        kfree(data);
 294error_release:
 295        release_region(res->start, SMSC_EXTENT);
 296        return err;
 297}
 298
 299static int __init smsc47b397_device_add(unsigned short address)
 300{
 301        struct resource res = {
 302                .start  = address,
 303                .end    = address + SMSC_EXTENT - 1,
 304                .name   = DRVNAME,
 305                .flags  = IORESOURCE_IO,
 306        };
 307        int err;
 308
 309        err = acpi_check_resource_conflict(&res);
 310        if (err)
 311                goto exit;
 312
 313        pdev = platform_device_alloc(DRVNAME, address);
 314        if (!pdev) {
 315                err = -ENOMEM;
 316                pr_err("Device allocation failed\n");
 317                goto exit;
 318        }
 319
 320        err = platform_device_add_resources(pdev, &res, 1);
 321        if (err) {
 322                pr_err("Device resource addition failed (%d)\n", err);
 323                goto exit_device_put;
 324        }
 325
 326        err = platform_device_add(pdev);
 327        if (err) {
 328                pr_err("Device addition failed (%d)\n", err);
 329                goto exit_device_put;
 330        }
 331
 332        return 0;
 333
 334exit_device_put:
 335        platform_device_put(pdev);
 336exit:
 337        return err;
 338}
 339
 340static int __init smsc47b397_find(unsigned short *addr)
 341{
 342        u8 id, rev;
 343        char *name;
 344
 345        superio_enter();
 346        id = force_id ? force_id : superio_inb(SUPERIO_REG_DEVID);
 347
 348        switch(id) {
 349        case 0x81:
 350                name = "SCH5307-NS";
 351                break;
 352        case 0x6f:
 353                name = "LPC47B397-NC";
 354                break;
 355        case 0x85:
 356        case 0x8c:
 357                name = "SCH5317";
 358                break;
 359        default:
 360                superio_exit();
 361                return -ENODEV;
 362        }
 363
 364        rev = superio_inb(SUPERIO_REG_DEVREV);
 365
 366        superio_select(SUPERIO_REG_LD8);
 367        *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
 368                 |  superio_inb(SUPERIO_REG_BASE_LSB);
 369
 370        pr_info("found SMSC %s (base address 0x%04x, revision %u)\n",
 371                name, *addr, rev);
 372
 373        superio_exit();
 374        return 0;
 375}
 376
 377static int __init smsc47b397_init(void)
 378{
 379        unsigned short address;
 380        int ret;
 381
 382        if ((ret = smsc47b397_find(&address)))
 383                return ret;
 384
 385        ret = platform_driver_register(&smsc47b397_driver);
 386        if (ret)
 387                goto exit;
 388
 389        /* Sets global pdev as a side effect */
 390        ret = smsc47b397_device_add(address);
 391        if (ret)
 392                goto exit_driver;
 393
 394        return 0;
 395
 396exit_driver:
 397        platform_driver_unregister(&smsc47b397_driver);
 398exit:
 399        return ret;
 400}
 401
 402static void __exit smsc47b397_exit(void)
 403{
 404        platform_device_unregister(pdev);
 405        platform_driver_unregister(&smsc47b397_driver);
 406}
 407
 408MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
 409MODULE_DESCRIPTION("SMSC LPC47B397 driver");
 410MODULE_LICENSE("GPL");
 411
 412module_init(smsc47b397_init);
 413module_exit(smsc47b397_exit);
 414