Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * lm92 - Hardware monitoring driver
0004  * Copyright (C) 2005-2008  Jean Delvare <jdelvare@suse.de>
0005  *
0006  * Based on the lm90 driver, with some ideas taken from the lm_sensors
0007  * lm92 driver as well.
0008  *
0009  * The LM92 is a sensor chip made by National Semiconductor. It reports
0010  * its own temperature with a 0.0625 deg resolution and a 0.33 deg
0011  * accuracy. Complete datasheet can be obtained from National's website
0012  * at:
0013  *   http://www.national.com/pf/LM/LM92.html
0014  *
0015  * This driver also supports the MAX6635 sensor chip made by Maxim.
0016  * This chip is compatible with the LM92, but has a lesser accuracy
0017  * (1.0 deg). Complete datasheet can be obtained from Maxim's website
0018  * at:
0019  *   http://www.maxim-ic.com/quick_view2.cfm/qv_pk/3074
0020  *
0021  * Since the LM92 was the first chipset supported by this driver, most
0022  * comments will refer to this chipset, but are actually general and
0023  * concern all supported chipsets, unless mentioned otherwise.
0024  *
0025  * Support could easily be added for the National Semiconductor LM76
0026  * and Maxim MAX6633 and MAX6634 chips, which are mostly compatible
0027  * with the LM92.
0028  */
0029 
0030 #include <linux/module.h>
0031 #include <linux/init.h>
0032 #include <linux/slab.h>
0033 #include <linux/i2c.h>
0034 #include <linux/hwmon.h>
0035 #include <linux/hwmon-sysfs.h>
0036 #include <linux/err.h>
0037 #include <linux/mutex.h>
0038 #include <linux/jiffies.h>
0039 
0040 /*
0041  * The LM92 and MAX6635 have 2 two-state pins for address selection,
0042  * resulting in 4 possible addresses.
0043  */
0044 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
0045                         I2C_CLIENT_END };
0046 enum chips { lm92, max6635 };
0047 
0048 /* The LM92 registers */
0049 #define LM92_REG_CONFIG         0x01 /* 8-bit, RW */
0050 #define LM92_REG_TEMP           0x00 /* 16-bit, RO */
0051 #define LM92_REG_TEMP_HYST      0x02 /* 16-bit, RW */
0052 #define LM92_REG_TEMP_CRIT      0x03 /* 16-bit, RW */
0053 #define LM92_REG_TEMP_LOW       0x04 /* 16-bit, RW */
0054 #define LM92_REG_TEMP_HIGH      0x05 /* 16-bit, RW */
0055 #define LM92_REG_MAN_ID         0x07 /* 16-bit, RO, LM92 only */
0056 
0057 /*
0058  * The LM92 uses signed 13-bit values with LSB = 0.0625 degree Celsius,
0059  * left-justified in 16-bit registers. No rounding is done, with such
0060  * a resolution it's just not worth it. Note that the MAX6635 doesn't
0061  * make use of the 4 lower bits for limits (i.e. effective resolution
0062  * for limits is 1 degree Celsius).
0063  */
0064 static inline int TEMP_FROM_REG(s16 reg)
0065 {
0066     return reg / 8 * 625 / 10;
0067 }
0068 
0069 static inline s16 TEMP_TO_REG(long val)
0070 {
0071     val = clamp_val(val, -60000, 160000);
0072     return val * 10 / 625 * 8;
0073 }
0074 
0075 /* Alarm flags are stored in the 3 LSB of the temperature register */
0076 static inline u8 ALARMS_FROM_REG(s16 reg)
0077 {
0078     return reg & 0x0007;
0079 }
0080 
0081 enum temp_index {
0082     t_input,
0083     t_crit,
0084     t_min,
0085     t_max,
0086     t_hyst,
0087     t_num_regs
0088 };
0089 
0090 static const u8 regs[t_num_regs] = {
0091     [t_input] = LM92_REG_TEMP,
0092     [t_crit] = LM92_REG_TEMP_CRIT,
0093     [t_min] = LM92_REG_TEMP_LOW,
0094     [t_max] = LM92_REG_TEMP_HIGH,
0095     [t_hyst] = LM92_REG_TEMP_HYST,
0096 };
0097 
0098 /* Client data (each client gets its own) */
0099 struct lm92_data {
0100     struct i2c_client *client;
0101     struct mutex update_lock;
0102     bool valid; /* false until following fields are valid */
0103     unsigned long last_updated; /* in jiffies */
0104 
0105     /* registers values */
0106     s16 temp[t_num_regs];   /* index with enum temp_index */
0107 };
0108 
0109 /*
0110  * Sysfs attributes and callback functions
0111  */
0112 
0113 static struct lm92_data *lm92_update_device(struct device *dev)
0114 {
0115     struct lm92_data *data = dev_get_drvdata(dev);
0116     struct i2c_client *client = data->client;
0117     int i;
0118 
0119     mutex_lock(&data->update_lock);
0120 
0121     if (time_after(jiffies, data->last_updated + HZ) ||
0122         !data->valid) {
0123         dev_dbg(&client->dev, "Updating lm92 data\n");
0124         for (i = 0; i < t_num_regs; i++) {
0125             data->temp[i] =
0126                 i2c_smbus_read_word_swapped(client, regs[i]);
0127         }
0128         data->last_updated = jiffies;
0129         data->valid = true;
0130     }
0131 
0132     mutex_unlock(&data->update_lock);
0133 
0134     return data;
0135 }
0136 
0137 static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
0138              char *buf)
0139 {
0140     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0141     struct lm92_data *data = lm92_update_device(dev);
0142 
0143     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
0144 }
0145 
0146 static ssize_t temp_store(struct device *dev,
0147               struct device_attribute *devattr, const char *buf,
0148               size_t count)
0149 {
0150     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0151     struct lm92_data *data = dev_get_drvdata(dev);
0152     struct i2c_client *client = data->client;
0153     int nr = attr->index;
0154     long val;
0155     int err;
0156 
0157     err = kstrtol(buf, 10, &val);
0158     if (err)
0159         return err;
0160 
0161     mutex_lock(&data->update_lock);
0162     data->temp[nr] = TEMP_TO_REG(val);
0163     i2c_smbus_write_word_swapped(client, regs[nr], data->temp[nr]);
0164     mutex_unlock(&data->update_lock);
0165     return count;
0166 }
0167 
0168 static ssize_t temp_hyst_show(struct device *dev,
0169                   struct device_attribute *devattr, char *buf)
0170 {
0171     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0172     struct lm92_data *data = lm92_update_device(dev);
0173 
0174     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index])
0175                - TEMP_FROM_REG(data->temp[t_hyst]));
0176 }
0177 
0178 static ssize_t temp1_min_hyst_show(struct device *dev,
0179                    struct device_attribute *attr, char *buf)
0180 {
0181     struct lm92_data *data = lm92_update_device(dev);
0182 
0183     return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[t_min])
0184                + TEMP_FROM_REG(data->temp[t_hyst]));
0185 }
0186 
0187 static ssize_t temp_hyst_store(struct device *dev,
0188                    struct device_attribute *devattr,
0189                    const char *buf, size_t count)
0190 {
0191     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0192     struct lm92_data *data = dev_get_drvdata(dev);
0193     struct i2c_client *client = data->client;
0194     long val;
0195     int err;
0196 
0197     err = kstrtol(buf, 10, &val);
0198     if (err)
0199         return err;
0200 
0201     val = clamp_val(val, -120000, 220000);
0202     mutex_lock(&data->update_lock);
0203     data->temp[t_hyst] =
0204         TEMP_TO_REG(TEMP_FROM_REG(data->temp[attr->index]) - val);
0205     i2c_smbus_write_word_swapped(client, LM92_REG_TEMP_HYST,
0206                      data->temp[t_hyst]);
0207     mutex_unlock(&data->update_lock);
0208     return count;
0209 }
0210 
0211 static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
0212                char *buf)
0213 {
0214     struct lm92_data *data = lm92_update_device(dev);
0215 
0216     return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->temp[t_input]));
0217 }
0218 
0219 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
0220               char *buf)
0221 {
0222     int bitnr = to_sensor_dev_attr(attr)->index;
0223     struct lm92_data *data = lm92_update_device(dev);
0224     return sprintf(buf, "%d\n", (data->temp[t_input] >> bitnr) & 1);
0225 }
0226 
0227 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, t_input);
0228 static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, t_crit);
0229 static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, temp_hyst, t_crit);
0230 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, t_min);
0231 static DEVICE_ATTR_RO(temp1_min_hyst);
0232 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, t_max);
0233 static SENSOR_DEVICE_ATTR_RO(temp1_max_hyst, temp_hyst, t_max);
0234 static DEVICE_ATTR_RO(alarms);
0235 static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 2);
0236 static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, 0);
0237 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 1);
0238 
0239 /*
0240  * Detection and registration
0241  */
0242 
0243 static void lm92_init_client(struct i2c_client *client)
0244 {
0245     u8 config;
0246 
0247     /* Start the conversions if needed */
0248     config = i2c_smbus_read_byte_data(client, LM92_REG_CONFIG);
0249     if (config & 0x01)
0250         i2c_smbus_write_byte_data(client, LM92_REG_CONFIG,
0251                       config & 0xFE);
0252 }
0253 
0254 static struct attribute *lm92_attrs[] = {
0255     &sensor_dev_attr_temp1_input.dev_attr.attr,
0256     &sensor_dev_attr_temp1_crit.dev_attr.attr,
0257     &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
0258     &sensor_dev_attr_temp1_min.dev_attr.attr,
0259     &dev_attr_temp1_min_hyst.attr,
0260     &sensor_dev_attr_temp1_max.dev_attr.attr,
0261     &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
0262     &dev_attr_alarms.attr,
0263     &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
0264     &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
0265     &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
0266     NULL
0267 };
0268 ATTRIBUTE_GROUPS(lm92);
0269 
0270 /* Return 0 if detection is successful, -ENODEV otherwise */
0271 static int lm92_detect(struct i2c_client *new_client,
0272                struct i2c_board_info *info)
0273 {
0274     struct i2c_adapter *adapter = new_client->adapter;
0275     u8 config;
0276     u16 man_id;
0277 
0278     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
0279                         | I2C_FUNC_SMBUS_WORD_DATA))
0280         return -ENODEV;
0281 
0282     config = i2c_smbus_read_byte_data(new_client, LM92_REG_CONFIG);
0283     man_id = i2c_smbus_read_word_data(new_client, LM92_REG_MAN_ID);
0284 
0285     if ((config & 0xe0) == 0x00 && man_id == 0x0180)
0286         pr_info("lm92: Found National Semiconductor LM92 chip\n");
0287     else
0288         return -ENODEV;
0289 
0290     strlcpy(info->type, "lm92", I2C_NAME_SIZE);
0291 
0292     return 0;
0293 }
0294 
0295 static int lm92_probe(struct i2c_client *new_client)
0296 {
0297     struct device *hwmon_dev;
0298     struct lm92_data *data;
0299 
0300     data = devm_kzalloc(&new_client->dev, sizeof(struct lm92_data),
0301                 GFP_KERNEL);
0302     if (!data)
0303         return -ENOMEM;
0304 
0305     data->client = new_client;
0306     mutex_init(&data->update_lock);
0307 
0308     /* Initialize the chipset */
0309     lm92_init_client(new_client);
0310 
0311     hwmon_dev = devm_hwmon_device_register_with_groups(&new_client->dev,
0312                                new_client->name,
0313                                data, lm92_groups);
0314     return PTR_ERR_OR_ZERO(hwmon_dev);
0315 }
0316 
0317 /*
0318  * Module and driver stuff
0319  */
0320 
0321 static const struct i2c_device_id lm92_id[] = {
0322     { "lm92", lm92 },
0323     { "max6635", max6635 },
0324     { }
0325 };
0326 MODULE_DEVICE_TABLE(i2c, lm92_id);
0327 
0328 static struct i2c_driver lm92_driver = {
0329     .class      = I2C_CLASS_HWMON,
0330     .driver = {
0331         .name   = "lm92",
0332     },
0333     .probe_new  = lm92_probe,
0334     .id_table   = lm92_id,
0335     .detect     = lm92_detect,
0336     .address_list   = normal_i2c,
0337 };
0338 
0339 module_i2c_driver(lm92_driver);
0340 
0341 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
0342 MODULE_DESCRIPTION("LM92/MAX6635 driver");
0343 MODULE_LICENSE("GPL");