Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * ds1621.c - Part of lm_sensors, Linux kernel modules for hardware
0004  *        monitoring
0005  * Christian W. Zuckschwerdt  <zany@triq.net>  2000-11-23
0006  * based on lm75.c by Frodo Looijaard <frodol@dds.nl>
0007  * Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
0008  * the help of Jean Delvare <jdelvare@suse.de>
0009  *
0010  * The DS1621 device is a digital temperature/thermometer with 9-bit
0011  * resolution, a thermal alarm output (Tout), and user-defined minimum
0012  * and maximum temperature thresholds (TH and TL).
0013  *
0014  * The DS1625, DS1631, DS1721, and DS1731 are pin compatible with the DS1621
0015  * and similar in operation, with slight variations as noted in the device
0016  * datasheets (please refer to www.maximintegrated.com for specific
0017  * device information).
0018  *
0019  * Since the DS1621 was the first chipset supported by this driver,
0020  * most comments will refer to this chipset, but are actually general
0021  * and concern all supported chipsets, unless mentioned otherwise.
0022  */
0023 
0024 #include <linux/module.h>
0025 #include <linux/init.h>
0026 #include <linux/slab.h>
0027 #include <linux/jiffies.h>
0028 #include <linux/i2c.h>
0029 #include <linux/hwmon.h>
0030 #include <linux/hwmon-sysfs.h>
0031 #include <linux/err.h>
0032 #include <linux/mutex.h>
0033 #include <linux/sysfs.h>
0034 #include <linux/kernel.h>
0035 
0036 /* Supported devices */
0037 enum chips { ds1621, ds1625, ds1631, ds1721, ds1731 };
0038 
0039 /* Insmod parameters */
0040 static int polarity = -1;
0041 module_param(polarity, int, 0);
0042 MODULE_PARM_DESC(polarity, "Output's polarity: 0 = active high, 1 = active low");
0043 
0044 /*
0045  * The Configuration/Status register
0046  *
0047  * - DS1621:
0048  *   7    6    5    4    3    2    1    0
0049  * |Done|THF |TLF |NVB | X  | X  |POL |1SHOT|
0050  *
0051  * - DS1625:
0052  *   7    6    5    4    3    2    1    0
0053  * |Done|THF |TLF |NVB | 1  | 0  |POL |1SHOT|
0054  *
0055  * - DS1631, DS1731:
0056  *   7    6    5    4    3    2    1    0
0057  * |Done|THF |TLF |NVB | R1 | R0 |POL |1SHOT|
0058  *
0059  * - DS1721:
0060  *   7    6    5    4    3    2    1    0
0061  * |Done| X  | X  | U  | R1 | R0 |POL |1SHOT|
0062  *
0063  * Where:
0064  * - 'X' is Reserved
0065  * - 'U' is Undefined
0066  */
0067 #define DS1621_REG_CONFIG_NVB       0x10
0068 #define DS1621_REG_CONFIG_RESOL     0x0C
0069 #define DS1621_REG_CONFIG_POLARITY  0x02
0070 #define DS1621_REG_CONFIG_1SHOT     0x01
0071 #define DS1621_REG_CONFIG_DONE      0x80
0072 
0073 #define DS1621_REG_CONFIG_RESOL_SHIFT   2
0074 
0075 /* ds1721 conversion rates: {C/LSB, time(ms), resolution bit setting} */
0076 static const unsigned short ds1721_convrates[] = {
0077     94, /*  9-bits (0.5,  93.75, RES[0..1] = 0 */
0078     188,    /* 10-bits (0.25, 187.5, RES[0..1] = 1 */
0079     375,    /* 11-bits (0.125,  375, RES[0..1] = 2 */
0080     750,    /* 12-bits (0.0625, 750, RES[0..1] = 3 */
0081 };
0082 
0083 #define DS1621_CONVERSION_MAX   750
0084 #define DS1625_CONVERSION_MAX   500
0085 
0086 #define DS1621_TEMP_MAX 125000
0087 #define DS1621_TEMP_MIN (-55000)
0088 
0089 /* The DS1621 temperature registers */
0090 static const u8 DS1621_REG_TEMP[3] = {
0091     0xAA,       /* input, word, RO */
0092     0xA2,       /* min, word, RW */
0093     0xA1,       /* max, word, RW */
0094 };
0095 #define DS1621_REG_CONF         0xAC /* byte, RW */
0096 #define DS1621_COM_START        0xEE /* no data */
0097 #define DS1721_COM_START        0x51 /* no data */
0098 #define DS1621_COM_STOP         0x22 /* no data */
0099 
0100 /* The DS1621 configuration register */
0101 #define DS1621_ALARM_TEMP_HIGH      0x40
0102 #define DS1621_ALARM_TEMP_LOW       0x20
0103 
0104 /* Conversions */
0105 #define ALARMS_FROM_REG(val) ((val) & \
0106             (DS1621_ALARM_TEMP_HIGH | DS1621_ALARM_TEMP_LOW))
0107 
0108 /* Each client has this additional data */
0109 struct ds1621_data {
0110     struct i2c_client *client;
0111     struct mutex update_lock;
0112     bool valid;         /* true if following fields are valid */
0113     unsigned long last_updated; /* In jiffies */
0114     enum chips kind;        /* device type */
0115 
0116     u16 temp[3];            /* Register values, word */
0117     u8 conf;            /* Register encoding, combined */
0118     u8 zbits;           /* Resolution encoded as number of
0119                      * zero bits */
0120     u16 update_interval;        /* Conversion rate in milliseconds */
0121 };
0122 
0123 static inline int DS1621_TEMP_FROM_REG(u16 reg)
0124 {
0125     return DIV_ROUND_CLOSEST(((s16)reg / 16) * 625, 10);
0126 }
0127 
0128 /*
0129  * TEMP: 0.001C/bit (-55C to +125C)
0130  * REG:
0131  *  - 1621, 1625: 0.5C/bit, 7 zero-bits
0132  *  - 1631, 1721, 1731: 0.0625C/bit, 4 zero-bits
0133  */
0134 static inline u16 DS1621_TEMP_TO_REG(long temp, u8 zbits)
0135 {
0136     temp = clamp_val(temp, DS1621_TEMP_MIN, DS1621_TEMP_MAX);
0137     temp = DIV_ROUND_CLOSEST(temp * (1 << (8 - zbits)), 1000) << zbits;
0138     return temp;
0139 }
0140 
0141 static void ds1621_init_client(struct ds1621_data *data,
0142                    struct i2c_client *client)
0143 {
0144     u8 conf, new_conf, sreg, resol;
0145 
0146     new_conf = conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
0147     /* switch to continuous conversion mode */
0148     new_conf &= ~DS1621_REG_CONFIG_1SHOT;
0149 
0150     /* setup output polarity */
0151     if (polarity == 0)
0152         new_conf &= ~DS1621_REG_CONFIG_POLARITY;
0153     else if (polarity == 1)
0154         new_conf |= DS1621_REG_CONFIG_POLARITY;
0155 
0156     if (conf != new_conf)
0157         i2c_smbus_write_byte_data(client, DS1621_REG_CONF, new_conf);
0158 
0159     switch (data->kind) {
0160     case ds1625:
0161         data->update_interval = DS1625_CONVERSION_MAX;
0162         data->zbits = 7;
0163         sreg = DS1621_COM_START;
0164         break;
0165     case ds1631:
0166     case ds1721:
0167     case ds1731:
0168         resol = (new_conf & DS1621_REG_CONFIG_RESOL) >>
0169              DS1621_REG_CONFIG_RESOL_SHIFT;
0170         data->update_interval = ds1721_convrates[resol];
0171         data->zbits = 7 - resol;
0172         sreg = DS1721_COM_START;
0173         break;
0174     default:
0175         data->update_interval = DS1621_CONVERSION_MAX;
0176         data->zbits = 7;
0177         sreg = DS1621_COM_START;
0178         break;
0179     }
0180 
0181     /* start conversion */
0182     i2c_smbus_write_byte(client, sreg);
0183 }
0184 
0185 static struct ds1621_data *ds1621_update_client(struct device *dev)
0186 {
0187     struct ds1621_data *data = dev_get_drvdata(dev);
0188     struct i2c_client *client = data->client;
0189     u8 new_conf;
0190 
0191     mutex_lock(&data->update_lock);
0192 
0193     if (time_after(jiffies, data->last_updated + data->update_interval) ||
0194         !data->valid) {
0195         int i;
0196 
0197         dev_dbg(&client->dev, "Starting ds1621 update\n");
0198 
0199         data->conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
0200 
0201         for (i = 0; i < ARRAY_SIZE(data->temp); i++)
0202             data->temp[i] = i2c_smbus_read_word_swapped(client,
0203                              DS1621_REG_TEMP[i]);
0204 
0205         /* reset alarms if necessary */
0206         new_conf = data->conf;
0207         if (data->temp[0] > data->temp[1])  /* input > min */
0208             new_conf &= ~DS1621_ALARM_TEMP_LOW;
0209         if (data->temp[0] < data->temp[2])  /* input < max */
0210             new_conf &= ~DS1621_ALARM_TEMP_HIGH;
0211         if (data->conf != new_conf)
0212             i2c_smbus_write_byte_data(client, DS1621_REG_CONF,
0213                           new_conf);
0214 
0215         data->last_updated = jiffies;
0216         data->valid = true;
0217     }
0218 
0219     mutex_unlock(&data->update_lock);
0220 
0221     return data;
0222 }
0223 
0224 static ssize_t temp_show(struct device *dev, struct device_attribute *da,
0225              char *buf)
0226 {
0227     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0228     struct ds1621_data *data = ds1621_update_client(dev);
0229     return sprintf(buf, "%d\n",
0230                DS1621_TEMP_FROM_REG(data->temp[attr->index]));
0231 }
0232 
0233 static ssize_t temp_store(struct device *dev, struct device_attribute *da,
0234               const char *buf, size_t count)
0235 {
0236     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0237     struct ds1621_data *data = dev_get_drvdata(dev);
0238     long val;
0239     int err;
0240 
0241     err = kstrtol(buf, 10, &val);
0242     if (err)
0243         return err;
0244 
0245     mutex_lock(&data->update_lock);
0246     data->temp[attr->index] = DS1621_TEMP_TO_REG(val, data->zbits);
0247     i2c_smbus_write_word_swapped(data->client, DS1621_REG_TEMP[attr->index],
0248                      data->temp[attr->index]);
0249     mutex_unlock(&data->update_lock);
0250     return count;
0251 }
0252 
0253 static ssize_t alarms_show(struct device *dev, struct device_attribute *da,
0254                char *buf)
0255 {
0256     struct ds1621_data *data = ds1621_update_client(dev);
0257     return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->conf));
0258 }
0259 
0260 static ssize_t alarm_show(struct device *dev, struct device_attribute *da,
0261               char *buf)
0262 {
0263     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0264     struct ds1621_data *data = ds1621_update_client(dev);
0265     return sprintf(buf, "%d\n", !!(data->conf & attr->index));
0266 }
0267 
0268 static ssize_t update_interval_show(struct device *dev,
0269                     struct device_attribute *da, char *buf)
0270 {
0271     struct ds1621_data *data = dev_get_drvdata(dev);
0272     return scnprintf(buf, PAGE_SIZE, "%hu\n", data->update_interval);
0273 }
0274 
0275 static ssize_t update_interval_store(struct device *dev,
0276                      struct device_attribute *da,
0277                      const char *buf, size_t count)
0278 {
0279     struct ds1621_data *data = dev_get_drvdata(dev);
0280     struct i2c_client *client = data->client;
0281     unsigned long convrate;
0282     s32 err;
0283     int resol = 0;
0284 
0285     err = kstrtoul(buf, 10, &convrate);
0286     if (err)
0287         return err;
0288 
0289     /* Convert rate into resolution bits */
0290     while (resol < (ARRAY_SIZE(ds1721_convrates) - 1) &&
0291            convrate > ds1721_convrates[resol])
0292         resol++;
0293 
0294     mutex_lock(&data->update_lock);
0295     data->conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
0296     data->conf &= ~DS1621_REG_CONFIG_RESOL;
0297     data->conf |= (resol << DS1621_REG_CONFIG_RESOL_SHIFT);
0298     i2c_smbus_write_byte_data(client, DS1621_REG_CONF, data->conf);
0299     data->update_interval = ds1721_convrates[resol];
0300     data->zbits = 7 - resol;
0301     mutex_unlock(&data->update_lock);
0302 
0303     return count;
0304 }
0305 
0306 static DEVICE_ATTR_RO(alarms);
0307 static DEVICE_ATTR_RW(update_interval);
0308 
0309 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
0310 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, 1);
0311 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 2);
0312 static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, DS1621_ALARM_TEMP_LOW);
0313 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, DS1621_ALARM_TEMP_HIGH);
0314 
0315 static struct attribute *ds1621_attributes[] = {
0316     &sensor_dev_attr_temp1_input.dev_attr.attr,
0317     &sensor_dev_attr_temp1_min.dev_attr.attr,
0318     &sensor_dev_attr_temp1_max.dev_attr.attr,
0319     &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
0320     &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
0321     &dev_attr_alarms.attr,
0322     &dev_attr_update_interval.attr,
0323     NULL
0324 };
0325 
0326 static umode_t ds1621_attribute_visible(struct kobject *kobj,
0327                     struct attribute *attr, int index)
0328 {
0329     struct device *dev = kobj_to_dev(kobj);
0330     struct ds1621_data *data = dev_get_drvdata(dev);
0331 
0332     if (attr == &dev_attr_update_interval.attr)
0333         if (data->kind == ds1621 || data->kind == ds1625)
0334             /* shhh, we're hiding update_interval */
0335             return 0;
0336     return attr->mode;
0337 }
0338 
0339 static const struct attribute_group ds1621_group = {
0340     .attrs = ds1621_attributes,
0341     .is_visible = ds1621_attribute_visible
0342 };
0343 __ATTRIBUTE_GROUPS(ds1621);
0344 
0345 static const struct i2c_device_id ds1621_id[];
0346 
0347 static int ds1621_probe(struct i2c_client *client)
0348 {
0349     struct ds1621_data *data;
0350     struct device *hwmon_dev;
0351 
0352     data = devm_kzalloc(&client->dev, sizeof(struct ds1621_data),
0353                 GFP_KERNEL);
0354     if (!data)
0355         return -ENOMEM;
0356 
0357     mutex_init(&data->update_lock);
0358 
0359     data->kind = i2c_match_id(ds1621_id, client)->driver_data;
0360     data->client = client;
0361 
0362     /* Initialize the DS1621 chip */
0363     ds1621_init_client(data, client);
0364 
0365     hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
0366                                client->name, data,
0367                                ds1621_groups);
0368     return PTR_ERR_OR_ZERO(hwmon_dev);
0369 }
0370 
0371 static const struct i2c_device_id ds1621_id[] = {
0372     { "ds1621", ds1621 },
0373     { "ds1625", ds1625 },
0374     { "ds1631", ds1631 },
0375     { "ds1721", ds1721 },
0376     { "ds1731", ds1731 },
0377     { }
0378 };
0379 MODULE_DEVICE_TABLE(i2c, ds1621_id);
0380 
0381 /* This is the driver that will be inserted */
0382 static struct i2c_driver ds1621_driver = {
0383     .class      = I2C_CLASS_HWMON,
0384     .driver = {
0385         .name   = "ds1621",
0386     },
0387     .probe_new  = ds1621_probe,
0388     .id_table   = ds1621_id,
0389 };
0390 
0391 module_i2c_driver(ds1621_driver);
0392 
0393 MODULE_AUTHOR("Christian W. Zuckschwerdt <zany@triq.net>");
0394 MODULE_DESCRIPTION("DS1621 driver");
0395 MODULE_LICENSE("GPL");