Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * LM73 Sensor driver
0004  * Based on LM75
0005  *
0006  * Copyright (C) 2007, CenoSYS (www.cenosys.com).
0007  * Copyright (C) 2009, Bollore telecom (www.bolloretelecom.eu).
0008  *
0009  * Guillaume Ligneul <guillaume.ligneul@gmail.com>
0010  * Adrien Demarez <adrien.demarez@bolloretelecom.eu>
0011  * Jeremy Laine <jeremy.laine@bolloretelecom.eu>
0012  * Chris Verges <kg4ysn@gmail.com>
0013  */
0014 
0015 #include <linux/module.h>
0016 #include <linux/init.h>
0017 #include <linux/i2c.h>
0018 #include <linux/hwmon.h>
0019 #include <linux/hwmon-sysfs.h>
0020 #include <linux/err.h>
0021 
0022 
0023 /* Addresses scanned */
0024 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c,
0025                     0x4d, 0x4e, I2C_CLIENT_END };
0026 
0027 /* LM73 registers */
0028 #define LM73_REG_INPUT      0x00
0029 #define LM73_REG_CONF       0x01
0030 #define LM73_REG_MAX        0x02
0031 #define LM73_REG_MIN        0x03
0032 #define LM73_REG_CTRL       0x04
0033 #define LM73_REG_ID     0x07
0034 
0035 #define LM73_ID         0x9001  /* 0x0190, byte-swapped */
0036 #define DRVNAME         "lm73"
0037 #define LM73_TEMP_MIN       (-256000 / 250)
0038 #define LM73_TEMP_MAX       (255750 / 250)
0039 
0040 #define LM73_CTRL_RES_SHIFT 5
0041 #define LM73_CTRL_RES_MASK  (BIT(5) | BIT(6))
0042 #define LM73_CTRL_TO_MASK   BIT(7)
0043 
0044 #define LM73_CTRL_HI_SHIFT  2
0045 #define LM73_CTRL_LO_SHIFT  1
0046 
0047 static const unsigned short lm73_convrates[] = {
0048     14, /* 11-bits (0.25000 C/LSB): RES1 Bit = 0, RES0 Bit = 0 */
0049     28, /* 12-bits (0.12500 C/LSB): RES1 Bit = 0, RES0 Bit = 1 */
0050     56, /* 13-bits (0.06250 C/LSB): RES1 Bit = 1, RES0 Bit = 0 */
0051     112,    /* 14-bits (0.03125 C/LSB): RES1 Bit = 1, RES0 Bit = 1 */
0052 };
0053 
0054 struct lm73_data {
0055     struct i2c_client *client;
0056     struct mutex lock;
0057     u8 ctrl;            /* control register value */
0058 };
0059 
0060 /*-----------------------------------------------------------------------*/
0061 
0062 static ssize_t temp_store(struct device *dev, struct device_attribute *da,
0063               const char *buf, size_t count)
0064 {
0065     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0066     struct lm73_data *data = dev_get_drvdata(dev);
0067     long temp;
0068     short value;
0069     s32 err;
0070 
0071     int status = kstrtol(buf, 10, &temp);
0072     if (status < 0)
0073         return status;
0074 
0075     /* Write value */
0076     value = clamp_val(temp / 250, LM73_TEMP_MIN, LM73_TEMP_MAX) << 5;
0077     err = i2c_smbus_write_word_swapped(data->client, attr->index, value);
0078     return (err < 0) ? err : count;
0079 }
0080 
0081 static ssize_t temp_show(struct device *dev, struct device_attribute *da,
0082              char *buf)
0083 {
0084     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0085     struct lm73_data *data = dev_get_drvdata(dev);
0086     int temp;
0087 
0088     s32 err = i2c_smbus_read_word_swapped(data->client, attr->index);
0089     if (err < 0)
0090         return err;
0091 
0092     /* use integer division instead of equivalent right shift to
0093        guarantee arithmetic shift and preserve the sign */
0094     temp = (((s16) err) * 250) / 32;
0095     return scnprintf(buf, PAGE_SIZE, "%d\n", temp);
0096 }
0097 
0098 static ssize_t convrate_store(struct device *dev, struct device_attribute *da,
0099                   const char *buf, size_t count)
0100 {
0101     struct lm73_data *data = dev_get_drvdata(dev);
0102     unsigned long convrate;
0103     s32 err;
0104     int res = 0;
0105 
0106     err = kstrtoul(buf, 10, &convrate);
0107     if (err < 0)
0108         return err;
0109 
0110     /*
0111      * Convert the desired conversion rate into register bits.
0112      * res is already initialized, and everything past the second-to-last
0113      * value in the array is treated as belonging to the last value
0114      * in the array.
0115      */
0116     while (res < (ARRAY_SIZE(lm73_convrates) - 1) &&
0117             convrate > lm73_convrates[res])
0118         res++;
0119 
0120     mutex_lock(&data->lock);
0121     data->ctrl &= LM73_CTRL_TO_MASK;
0122     data->ctrl |= res << LM73_CTRL_RES_SHIFT;
0123     err = i2c_smbus_write_byte_data(data->client, LM73_REG_CTRL,
0124                     data->ctrl);
0125     mutex_unlock(&data->lock);
0126 
0127     if (err < 0)
0128         return err;
0129 
0130     return count;
0131 }
0132 
0133 static ssize_t convrate_show(struct device *dev, struct device_attribute *da,
0134                  char *buf)
0135 {
0136     struct lm73_data *data = dev_get_drvdata(dev);
0137     int res;
0138 
0139     res = (data->ctrl & LM73_CTRL_RES_MASK) >> LM73_CTRL_RES_SHIFT;
0140     return scnprintf(buf, PAGE_SIZE, "%hu\n", lm73_convrates[res]);
0141 }
0142 
0143 static ssize_t maxmin_alarm_show(struct device *dev,
0144                  struct device_attribute *da, char *buf)
0145 {
0146     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0147     struct lm73_data *data = dev_get_drvdata(dev);
0148     s32 ctrl;
0149 
0150     mutex_lock(&data->lock);
0151     ctrl = i2c_smbus_read_byte_data(data->client, LM73_REG_CTRL);
0152     if (ctrl < 0)
0153         goto abort;
0154     data->ctrl = ctrl;
0155     mutex_unlock(&data->lock);
0156 
0157     return scnprintf(buf, PAGE_SIZE, "%d\n", (ctrl >> attr->index) & 1);
0158 
0159 abort:
0160     mutex_unlock(&data->lock);
0161     return ctrl;
0162 }
0163 
0164 /*-----------------------------------------------------------------------*/
0165 
0166 /* sysfs attributes for hwmon */
0167 
0168 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, LM73_REG_MAX);
0169 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, LM73_REG_MIN);
0170 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, LM73_REG_INPUT);
0171 static SENSOR_DEVICE_ATTR_RW(update_interval, convrate, 0);
0172 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, maxmin_alarm,
0173                  LM73_CTRL_HI_SHIFT);
0174 static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, maxmin_alarm,
0175                  LM73_CTRL_LO_SHIFT);
0176 
0177 static struct attribute *lm73_attrs[] = {
0178     &sensor_dev_attr_temp1_input.dev_attr.attr,
0179     &sensor_dev_attr_temp1_max.dev_attr.attr,
0180     &sensor_dev_attr_temp1_min.dev_attr.attr,
0181     &sensor_dev_attr_update_interval.dev_attr.attr,
0182     &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
0183     &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
0184     NULL
0185 };
0186 ATTRIBUTE_GROUPS(lm73);
0187 
0188 /*-----------------------------------------------------------------------*/
0189 
0190 /* device probe and removal */
0191 
0192 static int
0193 lm73_probe(struct i2c_client *client)
0194 {
0195     struct device *dev = &client->dev;
0196     struct device *hwmon_dev;
0197     struct lm73_data *data;
0198     int ctrl;
0199 
0200     data = devm_kzalloc(dev, sizeof(struct lm73_data), GFP_KERNEL);
0201     if (!data)
0202         return -ENOMEM;
0203 
0204     data->client = client;
0205     mutex_init(&data->lock);
0206 
0207     ctrl = i2c_smbus_read_byte_data(client, LM73_REG_CTRL);
0208     if (ctrl < 0)
0209         return ctrl;
0210     data->ctrl = ctrl;
0211 
0212     hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
0213                                data, lm73_groups);
0214     if (IS_ERR(hwmon_dev))
0215         return PTR_ERR(hwmon_dev);
0216 
0217     dev_info(dev, "sensor '%s'\n", client->name);
0218 
0219     return 0;
0220 }
0221 
0222 static const struct i2c_device_id lm73_ids[] = {
0223     { "lm73", 0 },
0224     { /* LIST END */ }
0225 };
0226 MODULE_DEVICE_TABLE(i2c, lm73_ids);
0227 
0228 /* Return 0 if detection is successful, -ENODEV otherwise */
0229 static int lm73_detect(struct i2c_client *new_client,
0230             struct i2c_board_info *info)
0231 {
0232     struct i2c_adapter *adapter = new_client->adapter;
0233     int id, ctrl, conf;
0234 
0235     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
0236                     I2C_FUNC_SMBUS_WORD_DATA))
0237         return -ENODEV;
0238 
0239     /*
0240      * Do as much detection as possible with byte reads first, as word
0241      * reads can confuse other devices.
0242      */
0243     ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL);
0244     if (ctrl < 0 || (ctrl & 0x10))
0245         return -ENODEV;
0246 
0247     conf = i2c_smbus_read_byte_data(new_client, LM73_REG_CONF);
0248     if (conf < 0 || (conf & 0x0c))
0249         return -ENODEV;
0250 
0251     id = i2c_smbus_read_byte_data(new_client, LM73_REG_ID);
0252     if (id < 0 || id != (LM73_ID & 0xff))
0253         return -ENODEV;
0254 
0255     /* Check device ID */
0256     id = i2c_smbus_read_word_data(new_client, LM73_REG_ID);
0257     if (id < 0 || id != LM73_ID)
0258         return -ENODEV;
0259 
0260     strlcpy(info->type, "lm73", I2C_NAME_SIZE);
0261 
0262     return 0;
0263 }
0264 
0265 static const struct of_device_id lm73_of_match[] = {
0266     {
0267         .compatible = "ti,lm73",
0268     },
0269     { },
0270 };
0271 
0272 MODULE_DEVICE_TABLE(of, lm73_of_match);
0273 
0274 static struct i2c_driver lm73_driver = {
0275     .class      = I2C_CLASS_HWMON,
0276     .driver = {
0277         .name   = "lm73",
0278         .of_match_table = lm73_of_match,
0279     },
0280     .probe_new  = lm73_probe,
0281     .id_table   = lm73_ids,
0282     .detect     = lm73_detect,
0283     .address_list   = normal_i2c,
0284 };
0285 
0286 module_i2c_driver(lm73_driver);
0287 
0288 MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>");
0289 MODULE_DESCRIPTION("LM73 driver");
0290 MODULE_LICENSE("GPL");