Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Driver for TI ADC128D818 System Monitor with Temperature Sensor
0004  *
0005  * Copyright (c) 2014 Guenter Roeck
0006  *
0007  * Derived from lm80.c
0008  * Copyright (C) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
0009  *               and Philip Edelbrock <phil@netroedge.com>
0010  */
0011 
0012 #include <linux/module.h>
0013 #include <linux/slab.h>
0014 #include <linux/jiffies.h>
0015 #include <linux/i2c.h>
0016 #include <linux/hwmon.h>
0017 #include <linux/hwmon-sysfs.h>
0018 #include <linux/err.h>
0019 #include <linux/regulator/consumer.h>
0020 #include <linux/mutex.h>
0021 #include <linux/bitops.h>
0022 #include <linux/of.h>
0023 
0024 /* Addresses to scan
0025  * The chip also supports addresses 0x35..0x37. Don't scan those addresses
0026  * since they are also used by some EEPROMs, which may result in false
0027  * positives.
0028  */
0029 static const unsigned short normal_i2c[] = {
0030     0x1d, 0x1e, 0x1f, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
0031 
0032 /* registers */
0033 #define ADC128_REG_IN_MAX(nr)       (0x2a + (nr) * 2)
0034 #define ADC128_REG_IN_MIN(nr)       (0x2b + (nr) * 2)
0035 #define ADC128_REG_IN(nr)       (0x20 + (nr))
0036 
0037 #define ADC128_REG_TEMP         0x27
0038 #define ADC128_REG_TEMP_MAX     0x38
0039 #define ADC128_REG_TEMP_HYST        0x39
0040 
0041 #define ADC128_REG_CONFIG       0x00
0042 #define ADC128_REG_ALARM        0x01
0043 #define ADC128_REG_MASK         0x03
0044 #define ADC128_REG_CONV_RATE        0x07
0045 #define ADC128_REG_ONESHOT      0x09
0046 #define ADC128_REG_SHUTDOWN     0x0a
0047 #define ADC128_REG_CONFIG_ADV       0x0b
0048 #define ADC128_REG_BUSY_STATUS      0x0c
0049 
0050 #define ADC128_REG_MAN_ID       0x3e
0051 #define ADC128_REG_DEV_ID       0x3f
0052 
0053 /* No. of voltage entries in adc128_attrs */
0054 #define ADC128_ATTR_NUM_VOLT        (8 * 4)
0055 
0056 /* Voltage inputs visible per operation mode */
0057 static const u8 num_inputs[] = { 7, 8, 4, 6 };
0058 
0059 struct adc128_data {
0060     struct i2c_client *client;
0061     struct regulator *regulator;
0062     int vref;       /* Reference voltage in mV */
0063     struct mutex update_lock;
0064     u8 mode;        /* Operation mode */
0065     bool valid;     /* true if following fields are valid */
0066     unsigned long last_updated; /* In jiffies */
0067 
0068     u16 in[3][8];       /* Register value, normalized to 12 bit
0069                  * 0: input voltage
0070                  * 1: min limit
0071                  * 2: max limit
0072                  */
0073     s16 temp[3];        /* Register value, normalized to 9 bit
0074                  * 0: sensor 1: limit 2: hyst
0075                  */
0076     u8 alarms;      /* alarm register value */
0077 };
0078 
0079 static struct adc128_data *adc128_update_device(struct device *dev)
0080 {
0081     struct adc128_data *data = dev_get_drvdata(dev);
0082     struct i2c_client *client = data->client;
0083     struct adc128_data *ret = data;
0084     int i, rv;
0085 
0086     mutex_lock(&data->update_lock);
0087 
0088     if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
0089         for (i = 0; i < num_inputs[data->mode]; i++) {
0090             rv = i2c_smbus_read_word_swapped(client,
0091                              ADC128_REG_IN(i));
0092             if (rv < 0)
0093                 goto abort;
0094             data->in[0][i] = rv >> 4;
0095 
0096             rv = i2c_smbus_read_byte_data(client,
0097                               ADC128_REG_IN_MIN(i));
0098             if (rv < 0)
0099                 goto abort;
0100             data->in[1][i] = rv << 4;
0101 
0102             rv = i2c_smbus_read_byte_data(client,
0103                               ADC128_REG_IN_MAX(i));
0104             if (rv < 0)
0105                 goto abort;
0106             data->in[2][i] = rv << 4;
0107         }
0108 
0109         if (data->mode != 1) {
0110             rv = i2c_smbus_read_word_swapped(client,
0111                              ADC128_REG_TEMP);
0112             if (rv < 0)
0113                 goto abort;
0114             data->temp[0] = rv >> 7;
0115 
0116             rv = i2c_smbus_read_byte_data(client,
0117                               ADC128_REG_TEMP_MAX);
0118             if (rv < 0)
0119                 goto abort;
0120             data->temp[1] = rv << 1;
0121 
0122             rv = i2c_smbus_read_byte_data(client,
0123                               ADC128_REG_TEMP_HYST);
0124             if (rv < 0)
0125                 goto abort;
0126             data->temp[2] = rv << 1;
0127         }
0128 
0129         rv = i2c_smbus_read_byte_data(client, ADC128_REG_ALARM);
0130         if (rv < 0)
0131             goto abort;
0132         data->alarms |= rv;
0133 
0134         data->last_updated = jiffies;
0135         data->valid = true;
0136     }
0137     goto done;
0138 
0139 abort:
0140     ret = ERR_PTR(rv);
0141     data->valid = false;
0142 done:
0143     mutex_unlock(&data->update_lock);
0144     return ret;
0145 }
0146 
0147 static ssize_t adc128_in_show(struct device *dev,
0148                   struct device_attribute *attr, char *buf)
0149 {
0150     struct adc128_data *data = adc128_update_device(dev);
0151     int index = to_sensor_dev_attr_2(attr)->index;
0152     int nr = to_sensor_dev_attr_2(attr)->nr;
0153     int val;
0154 
0155     if (IS_ERR(data))
0156         return PTR_ERR(data);
0157 
0158     val = DIV_ROUND_CLOSEST(data->in[index][nr] * data->vref, 4095);
0159     return sprintf(buf, "%d\n", val);
0160 }
0161 
0162 static ssize_t adc128_in_store(struct device *dev,
0163                    struct device_attribute *attr, const char *buf,
0164                    size_t count)
0165 {
0166     struct adc128_data *data = dev_get_drvdata(dev);
0167     int index = to_sensor_dev_attr_2(attr)->index;
0168     int nr = to_sensor_dev_attr_2(attr)->nr;
0169     u8 reg, regval;
0170     long val;
0171     int err;
0172 
0173     err = kstrtol(buf, 10, &val);
0174     if (err < 0)
0175         return err;
0176 
0177     mutex_lock(&data->update_lock);
0178     /* 10 mV LSB on limit registers */
0179     regval = clamp_val(DIV_ROUND_CLOSEST(val, 10), 0, 255);
0180     data->in[index][nr] = regval << 4;
0181     reg = index == 1 ? ADC128_REG_IN_MIN(nr) : ADC128_REG_IN_MAX(nr);
0182     i2c_smbus_write_byte_data(data->client, reg, regval);
0183     mutex_unlock(&data->update_lock);
0184 
0185     return count;
0186 }
0187 
0188 static ssize_t adc128_temp_show(struct device *dev,
0189                 struct device_attribute *attr, char *buf)
0190 {
0191     struct adc128_data *data = adc128_update_device(dev);
0192     int index = to_sensor_dev_attr(attr)->index;
0193     int temp;
0194 
0195     if (IS_ERR(data))
0196         return PTR_ERR(data);
0197 
0198     temp = sign_extend32(data->temp[index], 8);
0199     return sprintf(buf, "%d\n", temp * 500);/* 0.5 degrees C resolution */
0200 }
0201 
0202 static ssize_t adc128_temp_store(struct device *dev,
0203                  struct device_attribute *attr,
0204                  const char *buf, size_t count)
0205 {
0206     struct adc128_data *data = dev_get_drvdata(dev);
0207     int index = to_sensor_dev_attr(attr)->index;
0208     long val;
0209     int err;
0210     s8 regval;
0211 
0212     err = kstrtol(buf, 10, &val);
0213     if (err < 0)
0214         return err;
0215 
0216     mutex_lock(&data->update_lock);
0217     regval = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
0218     data->temp[index] = regval << 1;
0219     i2c_smbus_write_byte_data(data->client,
0220                   index == 1 ? ADC128_REG_TEMP_MAX
0221                          : ADC128_REG_TEMP_HYST,
0222                   regval);
0223     mutex_unlock(&data->update_lock);
0224 
0225     return count;
0226 }
0227 
0228 static ssize_t adc128_alarm_show(struct device *dev,
0229                  struct device_attribute *attr, char *buf)
0230 {
0231     struct adc128_data *data = adc128_update_device(dev);
0232     int mask = 1 << to_sensor_dev_attr(attr)->index;
0233     u8 alarms;
0234 
0235     if (IS_ERR(data))
0236         return PTR_ERR(data);
0237 
0238     /*
0239      * Clear an alarm after reporting it to user space. If it is still
0240      * active, the next update sequence will set the alarm bit again.
0241      */
0242     alarms = data->alarms;
0243     data->alarms &= ~mask;
0244 
0245     return sprintf(buf, "%u\n", !!(alarms & mask));
0246 }
0247 
0248 static umode_t adc128_is_visible(struct kobject *kobj,
0249                  struct attribute *attr, int index)
0250 {
0251     struct device *dev = kobj_to_dev(kobj);
0252     struct adc128_data *data = dev_get_drvdata(dev);
0253 
0254     if (index < ADC128_ATTR_NUM_VOLT) {
0255         /* Voltage, visible according to num_inputs[] */
0256         if (index >= num_inputs[data->mode] * 4)
0257             return 0;
0258     } else {
0259         /* Temperature, visible if not in mode 1 */
0260         if (data->mode == 1)
0261             return 0;
0262     }
0263 
0264     return attr->mode;
0265 }
0266 
0267 static SENSOR_DEVICE_ATTR_2_RO(in0_input, adc128_in, 0, 0);
0268 static SENSOR_DEVICE_ATTR_2_RW(in0_min, adc128_in, 0, 1);
0269 static SENSOR_DEVICE_ATTR_2_RW(in0_max, adc128_in, 0, 2);
0270 
0271 static SENSOR_DEVICE_ATTR_2_RO(in1_input, adc128_in, 1, 0);
0272 static SENSOR_DEVICE_ATTR_2_RW(in1_min, adc128_in, 1, 1);
0273 static SENSOR_DEVICE_ATTR_2_RW(in1_max, adc128_in, 1, 2);
0274 
0275 static SENSOR_DEVICE_ATTR_2_RO(in2_input, adc128_in, 2, 0);
0276 static SENSOR_DEVICE_ATTR_2_RW(in2_min, adc128_in, 2, 1);
0277 static SENSOR_DEVICE_ATTR_2_RW(in2_max, adc128_in, 2, 2);
0278 
0279 static SENSOR_DEVICE_ATTR_2_RO(in3_input, adc128_in, 3, 0);
0280 static SENSOR_DEVICE_ATTR_2_RW(in3_min, adc128_in, 3, 1);
0281 static SENSOR_DEVICE_ATTR_2_RW(in3_max, adc128_in, 3, 2);
0282 
0283 static SENSOR_DEVICE_ATTR_2_RO(in4_input, adc128_in, 4, 0);
0284 static SENSOR_DEVICE_ATTR_2_RW(in4_min, adc128_in, 4, 1);
0285 static SENSOR_DEVICE_ATTR_2_RW(in4_max, adc128_in, 4, 2);
0286 
0287 static SENSOR_DEVICE_ATTR_2_RO(in5_input, adc128_in, 5, 0);
0288 static SENSOR_DEVICE_ATTR_2_RW(in5_min, adc128_in, 5, 1);
0289 static SENSOR_DEVICE_ATTR_2_RW(in5_max, adc128_in, 5, 2);
0290 
0291 static SENSOR_DEVICE_ATTR_2_RO(in6_input, adc128_in, 6, 0);
0292 static SENSOR_DEVICE_ATTR_2_RW(in6_min, adc128_in, 6, 1);
0293 static SENSOR_DEVICE_ATTR_2_RW(in6_max, adc128_in, 6, 2);
0294 
0295 static SENSOR_DEVICE_ATTR_2_RO(in7_input, adc128_in, 7, 0);
0296 static SENSOR_DEVICE_ATTR_2_RW(in7_min, adc128_in, 7, 1);
0297 static SENSOR_DEVICE_ATTR_2_RW(in7_max, adc128_in, 7, 2);
0298 
0299 static SENSOR_DEVICE_ATTR_RO(temp1_input, adc128_temp, 0);
0300 static SENSOR_DEVICE_ATTR_RW(temp1_max, adc128_temp, 1);
0301 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, adc128_temp, 2);
0302 
0303 static SENSOR_DEVICE_ATTR_RO(in0_alarm, adc128_alarm, 0);
0304 static SENSOR_DEVICE_ATTR_RO(in1_alarm, adc128_alarm, 1);
0305 static SENSOR_DEVICE_ATTR_RO(in2_alarm, adc128_alarm, 2);
0306 static SENSOR_DEVICE_ATTR_RO(in3_alarm, adc128_alarm, 3);
0307 static SENSOR_DEVICE_ATTR_RO(in4_alarm, adc128_alarm, 4);
0308 static SENSOR_DEVICE_ATTR_RO(in5_alarm, adc128_alarm, 5);
0309 static SENSOR_DEVICE_ATTR_RO(in6_alarm, adc128_alarm, 6);
0310 static SENSOR_DEVICE_ATTR_RO(in7_alarm, adc128_alarm, 7);
0311 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, adc128_alarm, 7);
0312 
0313 static struct attribute *adc128_attrs[] = {
0314     &sensor_dev_attr_in0_alarm.dev_attr.attr,
0315     &sensor_dev_attr_in0_input.dev_attr.attr,
0316     &sensor_dev_attr_in0_max.dev_attr.attr,
0317     &sensor_dev_attr_in0_min.dev_attr.attr,
0318     &sensor_dev_attr_in1_alarm.dev_attr.attr,
0319     &sensor_dev_attr_in1_input.dev_attr.attr,
0320     &sensor_dev_attr_in1_max.dev_attr.attr,
0321     &sensor_dev_attr_in1_min.dev_attr.attr,
0322     &sensor_dev_attr_in2_alarm.dev_attr.attr,
0323     &sensor_dev_attr_in2_input.dev_attr.attr,
0324     &sensor_dev_attr_in2_max.dev_attr.attr,
0325     &sensor_dev_attr_in2_min.dev_attr.attr,
0326     &sensor_dev_attr_in3_alarm.dev_attr.attr,
0327     &sensor_dev_attr_in3_input.dev_attr.attr,
0328     &sensor_dev_attr_in3_max.dev_attr.attr,
0329     &sensor_dev_attr_in3_min.dev_attr.attr,
0330     &sensor_dev_attr_in4_alarm.dev_attr.attr,
0331     &sensor_dev_attr_in4_input.dev_attr.attr,
0332     &sensor_dev_attr_in4_max.dev_attr.attr,
0333     &sensor_dev_attr_in4_min.dev_attr.attr,
0334     &sensor_dev_attr_in5_alarm.dev_attr.attr,
0335     &sensor_dev_attr_in5_input.dev_attr.attr,
0336     &sensor_dev_attr_in5_max.dev_attr.attr,
0337     &sensor_dev_attr_in5_min.dev_attr.attr,
0338     &sensor_dev_attr_in6_alarm.dev_attr.attr,
0339     &sensor_dev_attr_in6_input.dev_attr.attr,
0340     &sensor_dev_attr_in6_max.dev_attr.attr,
0341     &sensor_dev_attr_in6_min.dev_attr.attr,
0342     &sensor_dev_attr_in7_alarm.dev_attr.attr,
0343     &sensor_dev_attr_in7_input.dev_attr.attr,
0344     &sensor_dev_attr_in7_max.dev_attr.attr,
0345     &sensor_dev_attr_in7_min.dev_attr.attr,
0346     &sensor_dev_attr_temp1_input.dev_attr.attr,
0347     &sensor_dev_attr_temp1_max.dev_attr.attr,
0348     &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
0349     &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
0350     NULL
0351 };
0352 
0353 static const struct attribute_group adc128_group = {
0354     .attrs = adc128_attrs,
0355     .is_visible = adc128_is_visible,
0356 };
0357 __ATTRIBUTE_GROUPS(adc128);
0358 
0359 static int adc128_detect(struct i2c_client *client, struct i2c_board_info *info)
0360 {
0361     int man_id, dev_id;
0362 
0363     if (!i2c_check_functionality(client->adapter,
0364                      I2C_FUNC_SMBUS_BYTE_DATA |
0365                      I2C_FUNC_SMBUS_WORD_DATA))
0366         return -ENODEV;
0367 
0368     man_id = i2c_smbus_read_byte_data(client, ADC128_REG_MAN_ID);
0369     dev_id = i2c_smbus_read_byte_data(client, ADC128_REG_DEV_ID);
0370     if (man_id != 0x01 || dev_id != 0x09)
0371         return -ENODEV;
0372 
0373     /* Check unused bits for confirmation */
0374     if (i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG) & 0xf4)
0375         return -ENODEV;
0376     if (i2c_smbus_read_byte_data(client, ADC128_REG_CONV_RATE) & 0xfe)
0377         return -ENODEV;
0378     if (i2c_smbus_read_byte_data(client, ADC128_REG_ONESHOT) & 0xfe)
0379         return -ENODEV;
0380     if (i2c_smbus_read_byte_data(client, ADC128_REG_SHUTDOWN) & 0xfe)
0381         return -ENODEV;
0382     if (i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG_ADV) & 0xf8)
0383         return -ENODEV;
0384     if (i2c_smbus_read_byte_data(client, ADC128_REG_BUSY_STATUS) & 0xfc)
0385         return -ENODEV;
0386 
0387     strlcpy(info->type, "adc128d818", I2C_NAME_SIZE);
0388 
0389     return 0;
0390 }
0391 
0392 static int adc128_init_client(struct adc128_data *data)
0393 {
0394     struct i2c_client *client = data->client;
0395     int err;
0396     u8 regval = 0x0;
0397 
0398     /*
0399      * Reset chip to defaults.
0400      * This makes most other initializations unnecessary.
0401      */
0402     err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x80);
0403     if (err)
0404         return err;
0405 
0406     /* Set operation mode, if non-default */
0407     if (data->mode != 0)
0408         regval |= data->mode << 1;
0409 
0410     /* If external vref is selected, configure the chip to use it */
0411     if (data->regulator)
0412         regval |= 0x01;
0413 
0414     /* Write advanced configuration register */
0415     if (regval != 0x0) {
0416         err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG_ADV,
0417                         regval);
0418         if (err)
0419             return err;
0420     }
0421 
0422     /* Start monitoring */
0423     err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x01);
0424     if (err)
0425         return err;
0426 
0427     return 0;
0428 }
0429 
0430 static int adc128_probe(struct i2c_client *client)
0431 {
0432     struct device *dev = &client->dev;
0433     struct regulator *regulator;
0434     struct device *hwmon_dev;
0435     struct adc128_data *data;
0436     int err, vref;
0437 
0438     data = devm_kzalloc(dev, sizeof(struct adc128_data), GFP_KERNEL);
0439     if (!data)
0440         return -ENOMEM;
0441 
0442     /* vref is optional. If specified, is used as chip reference voltage */
0443     regulator = devm_regulator_get_optional(dev, "vref");
0444     if (!IS_ERR(regulator)) {
0445         data->regulator = regulator;
0446         err = regulator_enable(regulator);
0447         if (err < 0)
0448             return err;
0449         vref = regulator_get_voltage(regulator);
0450         if (vref < 0) {
0451             err = vref;
0452             goto error;
0453         }
0454         data->vref = DIV_ROUND_CLOSEST(vref, 1000);
0455     } else {
0456         data->vref = 2560;  /* 2.56V, in mV */
0457     }
0458 
0459     /* Operation mode is optional. If unspecified, keep current mode */
0460     if (of_property_read_u8(dev->of_node, "ti,mode", &data->mode) == 0) {
0461         if (data->mode > 3) {
0462             dev_err(dev, "invalid operation mode %d\n",
0463                 data->mode);
0464             err = -EINVAL;
0465             goto error;
0466         }
0467     } else {
0468         err = i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG_ADV);
0469         if (err < 0)
0470             goto error;
0471         data->mode = (err >> 1) & ADC128_REG_MASK;
0472     }
0473 
0474     data->client = client;
0475     i2c_set_clientdata(client, data);
0476     mutex_init(&data->update_lock);
0477 
0478     /* Initialize the chip */
0479     err = adc128_init_client(data);
0480     if (err < 0)
0481         goto error;
0482 
0483     hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
0484                                data, adc128_groups);
0485     if (IS_ERR(hwmon_dev)) {
0486         err = PTR_ERR(hwmon_dev);
0487         goto error;
0488     }
0489 
0490     return 0;
0491 
0492 error:
0493     if (data->regulator)
0494         regulator_disable(data->regulator);
0495     return err;
0496 }
0497 
0498 static int adc128_remove(struct i2c_client *client)
0499 {
0500     struct adc128_data *data = i2c_get_clientdata(client);
0501 
0502     if (data->regulator)
0503         regulator_disable(data->regulator);
0504 
0505     return 0;
0506 }
0507 
0508 static const struct i2c_device_id adc128_id[] = {
0509     { "adc128d818", 0 },
0510     { }
0511 };
0512 MODULE_DEVICE_TABLE(i2c, adc128_id);
0513 
0514 static const struct of_device_id __maybe_unused adc128_of_match[] = {
0515     { .compatible = "ti,adc128d818" },
0516     { },
0517 };
0518 MODULE_DEVICE_TABLE(of, adc128_of_match);
0519 
0520 static struct i2c_driver adc128_driver = {
0521     .class      = I2C_CLASS_HWMON,
0522     .driver = {
0523         .name   = "adc128d818",
0524         .of_match_table = of_match_ptr(adc128_of_match),
0525     },
0526     .probe_new  = adc128_probe,
0527     .remove     = adc128_remove,
0528     .id_table   = adc128_id,
0529     .detect     = adc128_detect,
0530     .address_list   = normal_i2c,
0531 };
0532 
0533 module_i2c_driver(adc128_driver);
0534 
0535 MODULE_AUTHOR("Guenter Roeck");
0536 MODULE_DESCRIPTION("Driver for ADC128D818");
0537 MODULE_LICENSE("GPL");