linux/drivers/hwmon/adm1021.c
<<
>>
Prefs
   1/*
   2    adm1021.c - Part of lm_sensors, Linux kernel modules for hardware
   3                monitoring
   4    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl> and
   5    Philip Edelbrock <phil@netroedge.com>
   6
   7    This program is free software; you can redistribute it and/or modify
   8    it under the terms of the GNU General Public License as published by
   9    the Free Software Foundation; either version 2 of the License, or
  10    (at your option) any later version.
  11
  12    This program is distributed in the hope that it will be useful,
  13    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15    GNU General Public License for more details.
  16
  17    You should have received a copy of the GNU General Public License
  18    along with this program; if not, write to the Free Software
  19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20*/
  21
  22#include <linux/module.h>
  23#include <linux/init.h>
  24#include <linux/slab.h>
  25#include <linux/jiffies.h>
  26#include <linux/i2c.h>
  27#include <linux/hwmon.h>
  28#include <linux/hwmon-sysfs.h>
  29#include <linux/err.h>
  30#include <linux/mutex.h>
  31
  32
  33/* Addresses to scan */
  34static const unsigned short normal_i2c[] = {
  35        0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
  36
  37/* Insmod parameters */
  38I2C_CLIENT_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm,
  39                        mc1066);
  40
  41/* adm1021 constants specified below */
  42
  43/* The adm1021 registers */
  44/* Read-only */
  45/* For nr in 0-1 */
  46#define ADM1021_REG_TEMP(nr)            (nr)
  47#define ADM1021_REG_STATUS              0x02
  48/* 0x41 = AD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi */
  49#define ADM1021_REG_MAN_ID              0xFE
  50/* ADM1021 = 0x0X, ADM1023 = 0x3X */
  51#define ADM1021_REG_DEV_ID              0xFF
  52/* These use different addresses for reading/writing */
  53#define ADM1021_REG_CONFIG_R            0x03
  54#define ADM1021_REG_CONFIG_W            0x09
  55#define ADM1021_REG_CONV_RATE_R         0x04
  56#define ADM1021_REG_CONV_RATE_W         0x0A
  57/* These are for the ADM1023's additional precision on the remote temp sensor */
  58#define ADM1023_REG_REM_TEMP_PREC       0x10
  59#define ADM1023_REG_REM_OFFSET          0x11
  60#define ADM1023_REG_REM_OFFSET_PREC     0x12
  61#define ADM1023_REG_REM_TOS_PREC        0x13
  62#define ADM1023_REG_REM_THYST_PREC      0x14
  63/* limits */
  64/* For nr in 0-1 */
  65#define ADM1021_REG_TOS_R(nr)           (0x05 + 2 * (nr))
  66#define ADM1021_REG_TOS_W(nr)           (0x0B + 2 * (nr))
  67#define ADM1021_REG_THYST_R(nr)         (0x06 + 2 * (nr))
  68#define ADM1021_REG_THYST_W(nr)         (0x0C + 2 * (nr))
  69/* write-only */
  70#define ADM1021_REG_ONESHOT             0x0F
  71
  72/* Initial values */
  73
  74/* Note: Even though I left the low and high limits named os and hyst,
  75they don't quite work like a thermostat the way the LM75 does.  I.e.,
  76a lower temp than THYST actually triggers an alarm instead of
  77clearing it.  Weird, ey?   --Phil  */
  78
  79/* Each client has this additional data */
  80struct adm1021_data {
  81        struct device *hwmon_dev;
  82        enum chips type;
  83
  84        struct mutex update_lock;
  85        char valid;             /* !=0 if following fields are valid */
  86        char low_power;         /* !=0 if device in low power mode */
  87        unsigned long last_updated;     /* In jiffies */
  88
  89        int temp_max[2];                /* Register values */
  90        int temp_min[2];
  91        int temp[2];
  92        u8 alarms;
  93        /* Special values for ADM1023 only */
  94        u8 remote_temp_offset;
  95        u8 remote_temp_offset_prec;
  96};
  97
  98static int adm1021_probe(struct i2c_client *client,
  99                         const struct i2c_device_id *id);
 100static int adm1021_detect(struct i2c_client *client, int kind,
 101                          struct i2c_board_info *info);
 102static void adm1021_init_client(struct i2c_client *client);
 103static int adm1021_remove(struct i2c_client *client);
 104static struct adm1021_data *adm1021_update_device(struct device *dev);
 105
 106/* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
 107static int read_only;
 108
 109
 110static const struct i2c_device_id adm1021_id[] = {
 111        { "adm1021", adm1021 },
 112        { "adm1023", adm1023 },
 113        { "max1617", max1617 },
 114        { "max1617a", max1617a },
 115        { "thmc10", thmc10 },
 116        { "lm84", lm84 },
 117        { "gl523sm", gl523sm },
 118        { "mc1066", mc1066 },
 119        { }
 120};
 121MODULE_DEVICE_TABLE(i2c, adm1021_id);
 122
 123/* This is the driver that will be inserted */
 124static struct i2c_driver adm1021_driver = {
 125        .class          = I2C_CLASS_HWMON,
 126        .driver = {
 127                .name   = "adm1021",
 128        },
 129        .probe          = adm1021_probe,
 130        .remove         = adm1021_remove,
 131        .id_table       = adm1021_id,
 132        .detect         = adm1021_detect,
 133        .address_data   = &addr_data,
 134};
 135
 136static ssize_t show_temp(struct device *dev,
 137                         struct device_attribute *devattr, char *buf)
 138{
 139        int index = to_sensor_dev_attr(devattr)->index;
 140        struct adm1021_data *data = adm1021_update_device(dev);
 141
 142        return sprintf(buf, "%d\n", data->temp[index]);
 143}
 144
 145static ssize_t show_temp_max(struct device *dev,
 146                             struct device_attribute *devattr, char *buf)
 147{
 148        int index = to_sensor_dev_attr(devattr)->index;
 149        struct adm1021_data *data = adm1021_update_device(dev);
 150
 151        return sprintf(buf, "%d\n", data->temp_max[index]);
 152}
 153
 154static ssize_t show_temp_min(struct device *dev,
 155                             struct device_attribute *devattr, char *buf)
 156{
 157        int index = to_sensor_dev_attr(devattr)->index;
 158        struct adm1021_data *data = adm1021_update_device(dev);
 159
 160        return sprintf(buf, "%d\n", data->temp_min[index]);
 161}
 162
 163static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
 164                          char *buf)
 165{
 166        int index = to_sensor_dev_attr(attr)->index;
 167        struct adm1021_data *data = adm1021_update_device(dev);
 168        return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
 169}
 170
 171static ssize_t show_alarms(struct device *dev,
 172                           struct device_attribute *attr,
 173                           char *buf)
 174{
 175        struct adm1021_data *data = adm1021_update_device(dev);
 176        return sprintf(buf, "%u\n", data->alarms);
 177}
 178
 179static ssize_t set_temp_max(struct device *dev,
 180                            struct device_attribute *devattr,
 181                            const char *buf, size_t count)
 182{
 183        int index = to_sensor_dev_attr(devattr)->index;
 184        struct i2c_client *client = to_i2c_client(dev);
 185        struct adm1021_data *data = i2c_get_clientdata(client);
 186        long temp = simple_strtol(buf, NULL, 10) / 1000;
 187
 188        mutex_lock(&data->update_lock);
 189        data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127);
 190        if (!read_only)
 191                i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
 192                                          data->temp_max[index]);
 193        mutex_unlock(&data->update_lock);
 194
 195        return count;
 196}
 197
 198static ssize_t set_temp_min(struct device *dev,
 199                            struct device_attribute *devattr,
 200                            const char *buf, size_t count)
 201{
 202        int index = to_sensor_dev_attr(devattr)->index;
 203        struct i2c_client *client = to_i2c_client(dev);
 204        struct adm1021_data *data = i2c_get_clientdata(client);
 205        long temp = simple_strtol(buf, NULL, 10) / 1000;
 206
 207        mutex_lock(&data->update_lock);
 208        data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127);
 209        if (!read_only)
 210                i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
 211                                          data->temp_min[index]);
 212        mutex_unlock(&data->update_lock);
 213
 214        return count;
 215}
 216
 217static ssize_t show_low_power(struct device *dev,
 218                              struct device_attribute *devattr, char *buf)
 219{
 220        struct adm1021_data *data = adm1021_update_device(dev);
 221        return sprintf(buf, "%d\n", data->low_power);
 222}
 223
 224static ssize_t set_low_power(struct device *dev,
 225                             struct device_attribute *devattr,
 226                             const char *buf, size_t count)
 227{
 228        struct i2c_client *client = to_i2c_client(dev);
 229        struct adm1021_data *data = i2c_get_clientdata(client);
 230        int low_power = simple_strtol(buf, NULL, 10) != 0;
 231
 232        mutex_lock(&data->update_lock);
 233        if (low_power != data->low_power) {
 234                int config = i2c_smbus_read_byte_data(
 235                        client, ADM1021_REG_CONFIG_R);
 236                data->low_power = low_power;
 237                i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
 238                        (config & 0xBF) | (low_power << 6));
 239        }
 240        mutex_unlock(&data->update_lock);
 241
 242        return count;
 243}
 244
 245
 246static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
 247static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
 248                          set_temp_max, 0);
 249static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
 250                          set_temp_min, 0);
 251static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
 252static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
 253                          set_temp_max, 1);
 254static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
 255                          set_temp_min, 1);
 256static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
 257static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
 258static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
 259static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
 260static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
 261
 262static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
 263static DEVICE_ATTR(low_power, S_IWUSR | S_IRUGO, show_low_power, set_low_power);
 264
 265static struct attribute *adm1021_attributes[] = {
 266        &sensor_dev_attr_temp1_max.dev_attr.attr,
 267        &sensor_dev_attr_temp1_min.dev_attr.attr,
 268        &sensor_dev_attr_temp1_input.dev_attr.attr,
 269        &sensor_dev_attr_temp2_max.dev_attr.attr,
 270        &sensor_dev_attr_temp2_min.dev_attr.attr,
 271        &sensor_dev_attr_temp2_input.dev_attr.attr,
 272        &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
 273        &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
 274        &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
 275        &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
 276        &sensor_dev_attr_temp2_fault.dev_attr.attr,
 277        &dev_attr_alarms.attr,
 278        &dev_attr_low_power.attr,
 279        NULL
 280};
 281
 282static const struct attribute_group adm1021_group = {
 283        .attrs = adm1021_attributes,
 284};
 285
 286/* Return 0 if detection is successful, -ENODEV otherwise */
 287static int adm1021_detect(struct i2c_client *client, int kind,
 288                          struct i2c_board_info *info)
 289{
 290        struct i2c_adapter *adapter = client->adapter;
 291        int i;
 292        const char *type_name = "";
 293        int conv_rate, status, config;
 294
 295        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
 296                pr_debug("adm1021: detect failed, "
 297                         "smbus byte data not supported!\n");
 298                return -ENODEV;
 299        }
 300
 301        status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
 302        conv_rate = i2c_smbus_read_byte_data(client,
 303                                             ADM1021_REG_CONV_RATE_R);
 304        config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
 305
 306        /* Now, we do the remaining detection. */
 307        if (kind < 0) {
 308                if ((status & 0x03) != 0x00 || (config & 0x3F) != 0x00
 309                    || (conv_rate & 0xF8) != 0x00) {
 310                        pr_debug("adm1021: detect failed, "
 311                                 "chip not detected!\n");
 312                        return -ENODEV;
 313                }
 314        }
 315
 316        /* Determine the chip type. */
 317        if (kind <= 0) {
 318                i = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
 319                if (i == 0x41)
 320                        if ((i2c_smbus_read_byte_data(client,
 321                                        ADM1021_REG_DEV_ID) & 0xF0) == 0x30)
 322                                kind = adm1023;
 323                        else
 324                                kind = adm1021;
 325                else if (i == 0x49)
 326                        kind = thmc10;
 327                else if (i == 0x23)
 328                        kind = gl523sm;
 329                else if ((i == 0x4d) &&
 330                         (i2c_smbus_read_byte_data(client,
 331                                                   ADM1021_REG_DEV_ID) == 0x01))
 332                        kind = max1617a;
 333                else if (i == 0x54)
 334                        kind = mc1066;
 335                /* LM84 Mfr ID in a different place, and it has more unused bits */
 336                else if (conv_rate == 0x00
 337                         && (kind == 0 /* skip extra detection */
 338                             || ((config & 0x7F) == 0x00
 339                                 && (status & 0xAB) == 0x00)))
 340                        kind = lm84;
 341                else
 342                        kind = max1617;
 343        }
 344
 345        if (kind == max1617) {
 346                type_name = "max1617";
 347        } else if (kind == max1617a) {
 348                type_name = "max1617a";
 349        } else if (kind == adm1021) {
 350                type_name = "adm1021";
 351        } else if (kind == adm1023) {
 352                type_name = "adm1023";
 353        } else if (kind == thmc10) {
 354                type_name = "thmc10";
 355        } else if (kind == lm84) {
 356                type_name = "lm84";
 357        } else if (kind == gl523sm) {
 358                type_name = "gl523sm";
 359        } else if (kind == mc1066) {
 360                type_name = "mc1066";
 361        }
 362        pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n",
 363                 type_name, i2c_adapter_id(adapter), client->addr);
 364        strlcpy(info->type, type_name, I2C_NAME_SIZE);
 365
 366        return 0;
 367}
 368
 369static int adm1021_probe(struct i2c_client *client,
 370                         const struct i2c_device_id *id)
 371{
 372        struct adm1021_data *data;
 373        int err;
 374
 375        data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL);
 376        if (!data) {
 377                pr_debug("adm1021: detect failed, kzalloc failed!\n");
 378                err = -ENOMEM;
 379                goto error0;
 380        }
 381
 382        i2c_set_clientdata(client, data);
 383        data->type = id->driver_data;
 384        mutex_init(&data->update_lock);
 385
 386        /* Initialize the ADM1021 chip */
 387        if (data->type != lm84 && !read_only)
 388                adm1021_init_client(client);
 389
 390        /* Register sysfs hooks */
 391        if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group)))
 392                goto error1;
 393
 394        data->hwmon_dev = hwmon_device_register(&client->dev);
 395        if (IS_ERR(data->hwmon_dev)) {
 396                err = PTR_ERR(data->hwmon_dev);
 397                goto error3;
 398        }
 399
 400        return 0;
 401
 402error3:
 403        sysfs_remove_group(&client->dev.kobj, &adm1021_group);
 404error1:
 405        kfree(data);
 406error0:
 407        return err;
 408}
 409
 410static void adm1021_init_client(struct i2c_client *client)
 411{
 412        /* Enable ADC and disable suspend mode */
 413        i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
 414                i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
 415        /* Set Conversion rate to 1/sec (this can be tinkered with) */
 416        i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
 417}
 418
 419static int adm1021_remove(struct i2c_client *client)
 420{
 421        struct adm1021_data *data = i2c_get_clientdata(client);
 422
 423        hwmon_device_unregister(data->hwmon_dev);
 424        sysfs_remove_group(&client->dev.kobj, &adm1021_group);
 425
 426        kfree(data);
 427        return 0;
 428}
 429
 430static struct adm1021_data *adm1021_update_device(struct device *dev)
 431{
 432        struct i2c_client *client = to_i2c_client(dev);
 433        struct adm1021_data *data = i2c_get_clientdata(client);
 434
 435        mutex_lock(&data->update_lock);
 436
 437        if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
 438            || !data->valid) {
 439                int i;
 440
 441                dev_dbg(&client->dev, "Starting adm1021 update\n");
 442
 443                for (i = 0; i < 2; i++) {
 444                        data->temp[i] = 1000 *
 445                                (s8) i2c_smbus_read_byte_data(
 446                                        client, ADM1021_REG_TEMP(i));
 447                        data->temp_max[i] = 1000 *
 448                                (s8) i2c_smbus_read_byte_data(
 449                                        client, ADM1021_REG_TOS_R(i));
 450                        data->temp_min[i] = 1000 *
 451                                (s8) i2c_smbus_read_byte_data(
 452                                        client, ADM1021_REG_THYST_R(i));
 453                }
 454                data->alarms = i2c_smbus_read_byte_data(client,
 455                                                ADM1021_REG_STATUS) & 0x7c;
 456                if (data->type == adm1023) {
 457                        /* The ADM1023 provides 3 extra bits of precision for
 458                         * the remote sensor in extra registers. */
 459                        data->temp[1] += 125 * (i2c_smbus_read_byte_data(
 460                                client, ADM1023_REG_REM_TEMP_PREC) >> 5);
 461                        data->temp_max[1] += 125 * (i2c_smbus_read_byte_data(
 462                                client, ADM1023_REG_REM_TOS_PREC) >> 5);
 463                        data->temp_min[1] += 125 * (i2c_smbus_read_byte_data(
 464                                client, ADM1023_REG_REM_THYST_PREC) >> 5);
 465                        data->remote_temp_offset =
 466                                i2c_smbus_read_byte_data(client,
 467                                                ADM1023_REG_REM_OFFSET);
 468                        data->remote_temp_offset_prec =
 469                                i2c_smbus_read_byte_data(client,
 470                                                ADM1023_REG_REM_OFFSET_PREC);
 471                }
 472                data->last_updated = jiffies;
 473                data->valid = 1;
 474        }
 475
 476        mutex_unlock(&data->update_lock);
 477
 478        return data;
 479}
 480
 481static int __init sensors_adm1021_init(void)
 482{
 483        return i2c_add_driver(&adm1021_driver);
 484}
 485
 486static void __exit sensors_adm1021_exit(void)
 487{
 488        i2c_del_driver(&adm1021_driver);
 489}
 490
 491MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
 492                "Philip Edelbrock <phil@netroedge.com>");
 493MODULE_DESCRIPTION("adm1021 driver");
 494MODULE_LICENSE("GPL");
 495
 496module_param(read_only, bool, 0);
 497MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
 498
 499module_init(sensors_adm1021_init)
 500module_exit(sensors_adm1021_exit)
 501