Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* tmp421.c
0003  *
0004  * Copyright (C) 2009 Andre Prendel <andre.prendel@gmx.de>
0005  * Preliminary support by:
0006  * Melvin Rook, Raymond Ng
0007  */
0008 
0009 /*
0010  * Driver for the Texas Instruments TMP421 SMBus temperature sensor IC.
0011  * Supported models: TMP421, TMP422, TMP423, TMP441, TMP442
0012  */
0013 
0014 #include <linux/module.h>
0015 #include <linux/init.h>
0016 #include <linux/slab.h>
0017 #include <linux/jiffies.h>
0018 #include <linux/i2c.h>
0019 #include <linux/hwmon.h>
0020 #include <linux/hwmon-sysfs.h>
0021 #include <linux/err.h>
0022 #include <linux/mutex.h>
0023 #include <linux/of_device.h>
0024 #include <linux/sysfs.h>
0025 
0026 /* Addresses to scan */
0027 static const unsigned short normal_i2c[] = { 0x2a, 0x4c, 0x4d, 0x4e, 0x4f,
0028                          I2C_CLIENT_END };
0029 
0030 enum chips { tmp421, tmp422, tmp423, tmp441, tmp442 };
0031 
0032 #define MAX_CHANNELS                4
0033 /* The TMP421 registers */
0034 #define TMP421_STATUS_REG           0x08
0035 #define TMP421_CONFIG_REG_1         0x09
0036 #define TMP421_CONFIG_REG_2         0x0A
0037 #define TMP421_CONFIG_REG_REN(x)        (BIT(3 + (x)))
0038 #define TMP421_CONFIG_REG_REN_MASK      GENMASK(6, 3)
0039 #define TMP421_CONVERSION_RATE_REG      0x0B
0040 #define TMP421_N_FACTOR_REG_1           0x21
0041 #define TMP421_MANUFACTURER_ID_REG      0xFE
0042 #define TMP421_DEVICE_ID_REG            0xFF
0043 
0044 static const u8 TMP421_TEMP_MSB[MAX_CHANNELS]   = { 0x00, 0x01, 0x02, 0x03 };
0045 static const u8 TMP421_TEMP_LSB[MAX_CHANNELS]   = { 0x10, 0x11, 0x12, 0x13 };
0046 
0047 /* Flags */
0048 #define TMP421_CONFIG_SHUTDOWN          0x40
0049 #define TMP421_CONFIG_RANGE         0x04
0050 
0051 /* Manufacturer / Device ID's */
0052 #define TMP421_MANUFACTURER_ID          0x55
0053 #define TMP421_DEVICE_ID            0x21
0054 #define TMP422_DEVICE_ID            0x22
0055 #define TMP423_DEVICE_ID            0x23
0056 #define TMP441_DEVICE_ID            0x41
0057 #define TMP442_DEVICE_ID            0x42
0058 
0059 static const struct i2c_device_id tmp421_id[] = {
0060     { "tmp421", 2 },
0061     { "tmp422", 3 },
0062     { "tmp423", 4 },
0063     { "tmp441", 2 },
0064     { "tmp442", 3 },
0065     { }
0066 };
0067 MODULE_DEVICE_TABLE(i2c, tmp421_id);
0068 
0069 static const struct of_device_id __maybe_unused tmp421_of_match[] = {
0070     {
0071         .compatible = "ti,tmp421",
0072         .data = (void *)2
0073     },
0074     {
0075         .compatible = "ti,tmp422",
0076         .data = (void *)3
0077     },
0078     {
0079         .compatible = "ti,tmp423",
0080         .data = (void *)4
0081     },
0082     {
0083         .compatible = "ti,tmp441",
0084         .data = (void *)2
0085     },
0086     {
0087         .compatible = "ti,tmp442",
0088         .data = (void *)3
0089     },
0090     { },
0091 };
0092 MODULE_DEVICE_TABLE(of, tmp421_of_match);
0093 
0094 struct tmp421_channel {
0095     const char *label;
0096     bool enabled;
0097     s16 temp;
0098 };
0099 
0100 struct tmp421_data {
0101     struct i2c_client *client;
0102     struct mutex update_lock;
0103     u32 temp_config[MAX_CHANNELS + 1];
0104     struct hwmon_channel_info temp_info;
0105     const struct hwmon_channel_info *info[2];
0106     struct hwmon_chip_info chip;
0107     bool valid;
0108     unsigned long last_updated;
0109     unsigned long channels;
0110     u8 config;
0111     struct tmp421_channel channel[MAX_CHANNELS];
0112 };
0113 
0114 static int temp_from_raw(u16 reg, bool extended)
0115 {
0116     /* Mask out status bits */
0117     int temp = reg & ~0xf;
0118 
0119     if (extended)
0120         temp = temp - 64 * 256;
0121     else
0122         temp = (s16)temp;
0123 
0124     return DIV_ROUND_CLOSEST(temp * 1000, 256);
0125 }
0126 
0127 static int tmp421_update_device(struct tmp421_data *data)
0128 {
0129     struct i2c_client *client = data->client;
0130     int ret = 0;
0131     int i;
0132 
0133     mutex_lock(&data->update_lock);
0134 
0135     if (time_after(jiffies, data->last_updated + (HZ / 2)) ||
0136         !data->valid) {
0137         ret = i2c_smbus_read_byte_data(client, TMP421_CONFIG_REG_1);
0138         if (ret < 0)
0139             goto exit;
0140         data->config = ret;
0141 
0142         for (i = 0; i < data->channels; i++) {
0143             ret = i2c_smbus_read_byte_data(client, TMP421_TEMP_MSB[i]);
0144             if (ret < 0)
0145                 goto exit;
0146             data->channel[i].temp = ret << 8;
0147 
0148             ret = i2c_smbus_read_byte_data(client, TMP421_TEMP_LSB[i]);
0149             if (ret < 0)
0150                 goto exit;
0151             data->channel[i].temp |= ret;
0152         }
0153         data->last_updated = jiffies;
0154         data->valid = true;
0155     }
0156 
0157 exit:
0158     mutex_unlock(&data->update_lock);
0159 
0160     if (ret < 0) {
0161         data->valid = false;
0162         return ret;
0163     }
0164 
0165     return 0;
0166 }
0167 
0168 static int tmp421_enable_channels(struct tmp421_data *data)
0169 {
0170     int err;
0171     struct i2c_client *client = data->client;
0172     struct device *dev = &client->dev;
0173     int old = i2c_smbus_read_byte_data(client, TMP421_CONFIG_REG_2);
0174     int new, i;
0175 
0176     if (old < 0) {
0177         dev_err(dev, "error reading register, can't disable channels\n");
0178         return old;
0179     }
0180 
0181     new = old & ~TMP421_CONFIG_REG_REN_MASK;
0182     for (i = 0; i < data->channels; i++)
0183         if (data->channel[i].enabled)
0184             new |= TMP421_CONFIG_REG_REN(i);
0185 
0186     if (new == old)
0187         return 0;
0188 
0189     err = i2c_smbus_write_byte_data(client, TMP421_CONFIG_REG_2, new);
0190     if (err < 0)
0191         dev_err(dev, "error writing register, can't disable channels\n");
0192 
0193     return err;
0194 }
0195 
0196 static int tmp421_read(struct device *dev, enum hwmon_sensor_types type,
0197                u32 attr, int channel, long *val)
0198 {
0199     struct tmp421_data *tmp421 = dev_get_drvdata(dev);
0200     int ret = 0;
0201 
0202     ret = tmp421_update_device(tmp421);
0203     if (ret)
0204         return ret;
0205 
0206     switch (attr) {
0207     case hwmon_temp_input:
0208         if (!tmp421->channel[channel].enabled)
0209             return -ENODATA;
0210         *val = temp_from_raw(tmp421->channel[channel].temp,
0211                      tmp421->config & TMP421_CONFIG_RANGE);
0212         return 0;
0213     case hwmon_temp_fault:
0214         if (!tmp421->channel[channel].enabled)
0215             return -ENODATA;
0216         /*
0217          * Any of OPEN or /PVLD bits indicate a hardware mulfunction
0218          * and the conversion result may be incorrect
0219          */
0220         *val = !!(tmp421->channel[channel].temp & 0x03);
0221         return 0;
0222     case hwmon_temp_enable:
0223         *val = tmp421->channel[channel].enabled;
0224         return 0;
0225     default:
0226         return -EOPNOTSUPP;
0227     }
0228 
0229 }
0230 
0231 static int tmp421_read_string(struct device *dev, enum hwmon_sensor_types type,
0232                  u32 attr, int channel, const char **str)
0233 {
0234     struct tmp421_data *data = dev_get_drvdata(dev);
0235 
0236     *str = data->channel[channel].label;
0237 
0238     return 0;
0239 }
0240 
0241 static int tmp421_write(struct device *dev, enum hwmon_sensor_types type,
0242             u32 attr, int channel, long val)
0243 {
0244     struct tmp421_data *data = dev_get_drvdata(dev);
0245     int ret;
0246 
0247     switch (attr) {
0248     case hwmon_temp_enable:
0249         data->channel[channel].enabled = val;
0250         ret = tmp421_enable_channels(data);
0251         break;
0252     default:
0253         ret = -EOPNOTSUPP;
0254     }
0255 
0256     return ret;
0257 }
0258 
0259 static umode_t tmp421_is_visible(const void *data, enum hwmon_sensor_types type,
0260                  u32 attr, int channel)
0261 {
0262     switch (attr) {
0263     case hwmon_temp_fault:
0264     case hwmon_temp_input:
0265         return 0444;
0266     case hwmon_temp_label:
0267         return 0444;
0268     case hwmon_temp_enable:
0269         return 0644;
0270     default:
0271         return 0;
0272     }
0273 }
0274 
0275 static int tmp421_init_client(struct tmp421_data *data)
0276 {
0277     int config, config_orig;
0278     struct i2c_client *client = data->client;
0279 
0280     /* Set the conversion rate to 2 Hz */
0281     i2c_smbus_write_byte_data(client, TMP421_CONVERSION_RATE_REG, 0x05);
0282 
0283     /* Start conversions (disable shutdown if necessary) */
0284     config = i2c_smbus_read_byte_data(client, TMP421_CONFIG_REG_1);
0285     if (config < 0) {
0286         dev_err(&client->dev,
0287             "Could not read configuration register (%d)\n", config);
0288         return config;
0289     }
0290 
0291     config_orig = config;
0292     config &= ~TMP421_CONFIG_SHUTDOWN;
0293 
0294     if (config != config_orig) {
0295         dev_info(&client->dev, "Enable monitoring chip\n");
0296         i2c_smbus_write_byte_data(client, TMP421_CONFIG_REG_1, config);
0297     }
0298 
0299     return tmp421_enable_channels(data);
0300 }
0301 
0302 static int tmp421_detect(struct i2c_client *client,
0303              struct i2c_board_info *info)
0304 {
0305     enum chips kind;
0306     struct i2c_adapter *adapter = client->adapter;
0307     static const char * const names[] = {
0308         "TMP421", "TMP422", "TMP423",
0309         "TMP441", "TMP442"
0310     };
0311     int addr = client->addr;
0312     u8 reg;
0313 
0314     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0315         return -ENODEV;
0316 
0317     reg = i2c_smbus_read_byte_data(client, TMP421_MANUFACTURER_ID_REG);
0318     if (reg != TMP421_MANUFACTURER_ID)
0319         return -ENODEV;
0320 
0321     reg = i2c_smbus_read_byte_data(client, TMP421_CONVERSION_RATE_REG);
0322     if (reg & 0xf8)
0323         return -ENODEV;
0324 
0325     reg = i2c_smbus_read_byte_data(client, TMP421_STATUS_REG);
0326     if (reg & 0x7f)
0327         return -ENODEV;
0328 
0329     reg = i2c_smbus_read_byte_data(client, TMP421_DEVICE_ID_REG);
0330     switch (reg) {
0331     case TMP421_DEVICE_ID:
0332         kind = tmp421;
0333         break;
0334     case TMP422_DEVICE_ID:
0335         if (addr == 0x2a)
0336             return -ENODEV;
0337         kind = tmp422;
0338         break;
0339     case TMP423_DEVICE_ID:
0340         if (addr != 0x4c && addr != 0x4d)
0341             return -ENODEV;
0342         kind = tmp423;
0343         break;
0344     case TMP441_DEVICE_ID:
0345         kind = tmp441;
0346         break;
0347     case TMP442_DEVICE_ID:
0348         if (addr != 0x4c && addr != 0x4d)
0349             return -ENODEV;
0350         kind = tmp442;
0351         break;
0352     default:
0353         return -ENODEV;
0354     }
0355 
0356     strlcpy(info->type, tmp421_id[kind].name, I2C_NAME_SIZE);
0357     dev_info(&adapter->dev, "Detected TI %s chip at 0x%02x\n",
0358          names[kind], client->addr);
0359 
0360     return 0;
0361 }
0362 
0363 static int tmp421_probe_child_from_dt(struct i2c_client *client,
0364                       struct device_node *child,
0365                       struct tmp421_data *data)
0366 
0367 {
0368     struct device *dev = &client->dev;
0369     u32 i;
0370     s32 val;
0371     int err;
0372 
0373     err = of_property_read_u32(child, "reg", &i);
0374     if (err) {
0375         dev_err(dev, "missing reg property of %pOFn\n", child);
0376         return err;
0377     }
0378 
0379     if (i >= data->channels) {
0380         dev_err(dev, "invalid reg %d of %pOFn\n", i, child);
0381         return -EINVAL;
0382     }
0383 
0384     of_property_read_string(child, "label", &data->channel[i].label);
0385     if (data->channel[i].label)
0386         data->temp_config[i] |= HWMON_T_LABEL;
0387 
0388     data->channel[i].enabled = of_device_is_available(child);
0389 
0390     err = of_property_read_s32(child, "ti,n-factor", &val);
0391     if (!err) {
0392         if (i == 0) {
0393             dev_err(dev, "n-factor can't be set for internal channel\n");
0394             return -EINVAL;
0395         }
0396 
0397         if (val > 127 || val < -128) {
0398             dev_err(dev, "n-factor for channel %d invalid (%d)\n",
0399                 i, val);
0400             return -EINVAL;
0401         }
0402         i2c_smbus_write_byte_data(client, TMP421_N_FACTOR_REG_1 + i - 1,
0403                       val);
0404     }
0405 
0406     return 0;
0407 }
0408 
0409 static int tmp421_probe_from_dt(struct i2c_client *client, struct tmp421_data *data)
0410 {
0411     struct device *dev = &client->dev;
0412     const struct device_node *np = dev->of_node;
0413     struct device_node *child;
0414     int err;
0415 
0416     for_each_child_of_node(np, child) {
0417         if (strcmp(child->name, "channel"))
0418             continue;
0419 
0420         err = tmp421_probe_child_from_dt(client, child, data);
0421         if (err) {
0422             of_node_put(child);
0423             return err;
0424         }
0425     }
0426 
0427     return 0;
0428 }
0429 
0430 static const struct hwmon_ops tmp421_ops = {
0431     .is_visible = tmp421_is_visible,
0432     .read = tmp421_read,
0433     .read_string = tmp421_read_string,
0434     .write = tmp421_write,
0435 };
0436 
0437 static int tmp421_probe(struct i2c_client *client)
0438 {
0439     struct device *dev = &client->dev;
0440     struct device *hwmon_dev;
0441     struct tmp421_data *data;
0442     int i, err;
0443 
0444     data = devm_kzalloc(dev, sizeof(struct tmp421_data), GFP_KERNEL);
0445     if (!data)
0446         return -ENOMEM;
0447 
0448     mutex_init(&data->update_lock);
0449     if (client->dev.of_node)
0450         data->channels = (unsigned long)
0451             of_device_get_match_data(&client->dev);
0452     else
0453         data->channels = i2c_match_id(tmp421_id, client)->driver_data;
0454     data->client = client;
0455 
0456     for (i = 0; i < data->channels; i++) {
0457         data->temp_config[i] = HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_ENABLE;
0458         data->channel[i].enabled = true;
0459     }
0460 
0461     err = tmp421_probe_from_dt(client, data);
0462     if (err)
0463         return err;
0464 
0465     err = tmp421_init_client(data);
0466     if (err)
0467         return err;
0468 
0469     data->chip.ops = &tmp421_ops;
0470     data->chip.info = data->info;
0471 
0472     data->info[0] = &data->temp_info;
0473 
0474     data->temp_info.type = hwmon_temp;
0475     data->temp_info.config = data->temp_config;
0476 
0477     hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
0478                              data,
0479                              &data->chip,
0480                              NULL);
0481     return PTR_ERR_OR_ZERO(hwmon_dev);
0482 }
0483 
0484 static struct i2c_driver tmp421_driver = {
0485     .class = I2C_CLASS_HWMON,
0486     .driver = {
0487         .name   = "tmp421",
0488         .of_match_table = of_match_ptr(tmp421_of_match),
0489     },
0490     .probe_new = tmp421_probe,
0491     .id_table = tmp421_id,
0492     .detect = tmp421_detect,
0493     .address_list = normal_i2c,
0494 };
0495 
0496 module_i2c_driver(tmp421_driver);
0497 
0498 MODULE_AUTHOR("Andre Prendel <andre.prendel@gmx.de>");
0499 MODULE_DESCRIPTION("Texas Instruments TMP421/422/423/441/442 temperature sensor driver");
0500 MODULE_LICENSE("GPL");