Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * w83l785ts.c - Part of lm_sensors, Linux kernel modules for hardware
0004  *               monitoring
0005  * Copyright (C) 2003-2009  Jean Delvare <jdelvare@suse.de>
0006  *
0007  * Inspired from the lm83 driver. The W83L785TS-S is a sensor chip made
0008  * by Winbond. It reports a single external temperature with a 1 deg
0009  * resolution and a 3 deg accuracy. Datasheet can be obtained from
0010  * Winbond's website at:
0011  *   http://www.winbond-usa.com/products/winbond_products/pdfs/PCIC/W83L785TS-S.pdf
0012  *
0013  * Ported to Linux 2.6 by Wolfgang Ziegler <nuppla@gmx.at> and Jean Delvare
0014  * <jdelvare@suse.de>.
0015  *
0016  * Thanks to James Bolt <james@evilpenguin.com> for benchmarking the read
0017  * error handling mechanism.
0018  */
0019 
0020 #include <linux/module.h>
0021 #include <linux/delay.h>
0022 #include <linux/init.h>
0023 #include <linux/slab.h>
0024 #include <linux/jiffies.h>
0025 #include <linux/i2c.h>
0026 #include <linux/hwmon.h>
0027 #include <linux/hwmon-sysfs.h>
0028 #include <linux/err.h>
0029 #include <linux/mutex.h>
0030 
0031 /* How many retries on register read error */
0032 #define MAX_RETRIES 5
0033 
0034 /*
0035  * Address to scan
0036  * Address is fully defined internally and cannot be changed.
0037  */
0038 
0039 static const unsigned short normal_i2c[] = { 0x2e, I2C_CLIENT_END };
0040 
0041 /*
0042  * The W83L785TS-S registers
0043  * Manufacturer ID is 0x5CA3 for Winbond.
0044  */
0045 
0046 #define W83L785TS_REG_MAN_ID1       0x4D
0047 #define W83L785TS_REG_MAN_ID2       0x4C
0048 #define W83L785TS_REG_CHIP_ID       0x4E
0049 #define W83L785TS_REG_CONFIG        0x40
0050 #define W83L785TS_REG_TYPE      0x52
0051 #define W83L785TS_REG_TEMP      0x27
0052 #define W83L785TS_REG_TEMP_OVER     0x53 /* not sure about this one */
0053 
0054 /*
0055  * Conversions
0056  * The W83L785TS-S uses signed 8-bit values.
0057  */
0058 
0059 #define TEMP_FROM_REG(val)  ((val) * 1000)
0060 
0061 /*
0062  * Functions declaration
0063  */
0064 
0065 static int w83l785ts_probe(struct i2c_client *client);
0066 static int w83l785ts_detect(struct i2c_client *client,
0067                 struct i2c_board_info *info);
0068 static int w83l785ts_remove(struct i2c_client *client);
0069 static u8 w83l785ts_read_value(struct i2c_client *client, u8 reg, u8 defval);
0070 static struct w83l785ts_data *w83l785ts_update_device(struct device *dev);
0071 
0072 /*
0073  * Driver data (common to all clients)
0074  */
0075 
0076 static const struct i2c_device_id w83l785ts_id[] = {
0077     { "w83l785ts", 0 },
0078     { }
0079 };
0080 MODULE_DEVICE_TABLE(i2c, w83l785ts_id);
0081 
0082 static struct i2c_driver w83l785ts_driver = {
0083     .class      = I2C_CLASS_HWMON,
0084     .driver = {
0085         .name   = "w83l785ts",
0086     },
0087     .probe_new  = w83l785ts_probe,
0088     .remove     = w83l785ts_remove,
0089     .id_table   = w83l785ts_id,
0090     .detect     = w83l785ts_detect,
0091     .address_list   = normal_i2c,
0092 };
0093 
0094 /*
0095  * Client data (each client gets its own)
0096  */
0097 
0098 struct w83l785ts_data {
0099     struct device *hwmon_dev;
0100     struct mutex update_lock;
0101     bool valid; /* false until following fields are valid */
0102     unsigned long last_updated; /* in jiffies */
0103 
0104     /* registers values */
0105     s8 temp[2]; /* 0: input, 1: critical limit */
0106 };
0107 
0108 /*
0109  * Sysfs stuff
0110  */
0111 
0112 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
0113     char *buf)
0114 {
0115     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0116     struct w83l785ts_data *data = w83l785ts_update_device(dev);
0117     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
0118 }
0119 
0120 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
0121 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL, 1);
0122 
0123 /*
0124  * Real code
0125  */
0126 
0127 /* Return 0 if detection is successful, -ENODEV otherwise */
0128 static int w83l785ts_detect(struct i2c_client *client,
0129                 struct i2c_board_info *info)
0130 {
0131     struct i2c_adapter *adapter = client->adapter;
0132     u16 man_id;
0133     u8 chip_id;
0134 
0135     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0136         return -ENODEV;
0137 
0138     /* detection */
0139     if ((w83l785ts_read_value(client, W83L785TS_REG_CONFIG, 0) & 0x80)
0140      || (w83l785ts_read_value(client, W83L785TS_REG_TYPE, 0) & 0xFC)) {
0141         dev_dbg(&adapter->dev,
0142             "W83L785TS-S detection failed at 0x%02x\n",
0143             client->addr);
0144         return -ENODEV;
0145     }
0146 
0147     /* Identification */
0148     man_id = (w83l785ts_read_value(client, W83L785TS_REG_MAN_ID1, 0) << 8)
0149            + w83l785ts_read_value(client, W83L785TS_REG_MAN_ID2, 0);
0150     chip_id = w83l785ts_read_value(client, W83L785TS_REG_CHIP_ID, 0);
0151 
0152     if (man_id != 0x5CA3        /* Winbond */
0153      || chip_id != 0x70) {      /* W83L785TS-S */
0154         dev_dbg(&adapter->dev,
0155             "Unsupported chip (man_id=0x%04X, chip_id=0x%02X)\n",
0156             man_id, chip_id);
0157         return -ENODEV;
0158     }
0159 
0160     strlcpy(info->type, "w83l785ts", I2C_NAME_SIZE);
0161 
0162     return 0;
0163 }
0164 
0165 static int w83l785ts_probe(struct i2c_client *client)
0166 {
0167     struct w83l785ts_data *data;
0168     struct device *dev = &client->dev;
0169     int err;
0170 
0171     data = devm_kzalloc(dev, sizeof(struct w83l785ts_data), GFP_KERNEL);
0172     if (!data)
0173         return -ENOMEM;
0174 
0175     i2c_set_clientdata(client, data);
0176     mutex_init(&data->update_lock);
0177 
0178     /*
0179      * Initialize the W83L785TS chip
0180      * Nothing yet, assume it is already started.
0181      */
0182 
0183     err = device_create_file(dev, &sensor_dev_attr_temp1_input.dev_attr);
0184     if (err)
0185         return err;
0186 
0187     err = device_create_file(dev, &sensor_dev_attr_temp1_max.dev_attr);
0188     if (err)
0189         goto exit_remove;
0190 
0191     /* Register sysfs hooks */
0192     data->hwmon_dev = hwmon_device_register(dev);
0193     if (IS_ERR(data->hwmon_dev)) {
0194         err = PTR_ERR(data->hwmon_dev);
0195         goto exit_remove;
0196     }
0197 
0198     return 0;
0199 
0200 exit_remove:
0201     device_remove_file(dev, &sensor_dev_attr_temp1_input.dev_attr);
0202     device_remove_file(dev, &sensor_dev_attr_temp1_max.dev_attr);
0203     return err;
0204 }
0205 
0206 static int w83l785ts_remove(struct i2c_client *client)
0207 {
0208     struct w83l785ts_data *data = i2c_get_clientdata(client);
0209 
0210     hwmon_device_unregister(data->hwmon_dev);
0211     device_remove_file(&client->dev,
0212                &sensor_dev_attr_temp1_input.dev_attr);
0213     device_remove_file(&client->dev,
0214                &sensor_dev_attr_temp1_max.dev_attr);
0215 
0216     return 0;
0217 }
0218 
0219 static u8 w83l785ts_read_value(struct i2c_client *client, u8 reg, u8 defval)
0220 {
0221     int value, i;
0222     struct device *dev;
0223     const char *prefix;
0224 
0225     /*
0226      * We might be called during detection, at which point the client
0227      * isn't yet fully initialized, so we can't use dev_dbg on it
0228      */
0229     if (i2c_get_clientdata(client)) {
0230         dev = &client->dev;
0231         prefix = "";
0232     } else {
0233         dev = &client->adapter->dev;
0234         prefix = "w83l785ts: ";
0235     }
0236 
0237     /*
0238      * Frequent read errors have been reported on Asus boards, so we
0239      * retry on read errors. If it still fails (unlikely), return the
0240      * default value requested by the caller.
0241      */
0242     for (i = 1; i <= MAX_RETRIES; i++) {
0243         value = i2c_smbus_read_byte_data(client, reg);
0244         if (value >= 0) {
0245             dev_dbg(dev, "%sRead 0x%02x from register 0x%02x.\n",
0246                 prefix, value, reg);
0247             return value;
0248         }
0249         dev_dbg(dev, "%sRead failed, will retry in %d.\n", prefix, i);
0250         msleep(i);
0251     }
0252 
0253     dev_err(dev, "%sCouldn't read value from register 0x%02x.\n", prefix,
0254         reg);
0255     return defval;
0256 }
0257 
0258 static struct w83l785ts_data *w83l785ts_update_device(struct device *dev)
0259 {
0260     struct i2c_client *client = to_i2c_client(dev);
0261     struct w83l785ts_data *data = i2c_get_clientdata(client);
0262 
0263     mutex_lock(&data->update_lock);
0264 
0265     if (!data->valid || time_after(jiffies, data->last_updated + HZ * 2)) {
0266         dev_dbg(&client->dev, "Updating w83l785ts data.\n");
0267         data->temp[0] = w83l785ts_read_value(client,
0268                 W83L785TS_REG_TEMP, data->temp[0]);
0269         data->temp[1] = w83l785ts_read_value(client,
0270                 W83L785TS_REG_TEMP_OVER, data->temp[1]);
0271 
0272         data->last_updated = jiffies;
0273         data->valid = true;
0274     }
0275 
0276     mutex_unlock(&data->update_lock);
0277 
0278     return data;
0279 }
0280 
0281 module_i2c_driver(w83l785ts_driver);
0282 
0283 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
0284 MODULE_DESCRIPTION("W83L785TS-S driver");
0285 MODULE_LICENSE("GPL");