Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * lm75.c - Part of lm_sensors, Linux kernel modules for hardware
0004  *   monitoring
0005  * Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/init.h>
0010 #include <linux/slab.h>
0011 #include <linux/jiffies.h>
0012 #include <linux/i2c.h>
0013 #include <linux/hwmon.h>
0014 #include <linux/hwmon-sysfs.h>
0015 #include <linux/err.h>
0016 #include <linux/of_device.h>
0017 #include <linux/of.h>
0018 #include <linux/regmap.h>
0019 #include <linux/util_macros.h>
0020 #include <linux/regulator/consumer.h>
0021 #include "lm75.h"
0022 
0023 /*
0024  * This driver handles the LM75 and compatible digital temperature sensors.
0025  */
0026 
0027 enum lm75_type {        /* keep sorted in alphabetical order */
0028     adt75,
0029     at30ts74,
0030     ds1775,
0031     ds75,
0032     ds7505,
0033     g751,
0034     lm75,
0035     lm75a,
0036     lm75b,
0037     max6625,
0038     max6626,
0039     max31725,
0040     mcp980x,
0041     pct2075,
0042     stds75,
0043     stlm75,
0044     tcn75,
0045     tmp100,
0046     tmp101,
0047     tmp105,
0048     tmp112,
0049     tmp175,
0050     tmp275,
0051     tmp75,
0052     tmp75b,
0053     tmp75c,
0054     tmp1075,
0055 };
0056 
0057 /**
0058  * struct lm75_params - lm75 configuration parameters.
0059  * @set_mask:       Bits to set in configuration register when configuring
0060  *          the chip.
0061  * @clr_mask:       Bits to clear in configuration register when configuring
0062  *          the chip.
0063  * @default_resolution: Default number of bits to represent the temperature
0064  *          value.
0065  * @resolution_limits:  Limit register resolution. Optional. Should be set if
0066  *          the resolution of limit registers does not match the
0067  *          resolution of the temperature register.
0068  * @resolutions:    List of resolutions associated with sample times.
0069  *          Optional. Should be set if num_sample_times is larger
0070  *          than 1, and if the resolution changes with sample times.
0071  *          If set, number of entries must match num_sample_times.
0072  * @default_sample_time:Sample time to be set by default.
0073  * @num_sample_times:   Number of possible sample times to be set. Optional.
0074  *          Should be set if the number of sample times is larger
0075  *          than one.
0076  * @sample_times:   All the possible sample times to be set. Mandatory if
0077  *          num_sample_times is larger than 1. If set, number of
0078  *          entries must match num_sample_times.
0079  */
0080 
0081 struct lm75_params {
0082     u8          set_mask;
0083     u8          clr_mask;
0084     u8          default_resolution;
0085     u8          resolution_limits;
0086     const u8        *resolutions;
0087     unsigned int        default_sample_time;
0088     u8          num_sample_times;
0089     const unsigned int  *sample_times;
0090 };
0091 
0092 /* Addresses scanned */
0093 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
0094                     0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
0095 
0096 /* The LM75 registers */
0097 #define LM75_REG_TEMP       0x00
0098 #define LM75_REG_CONF       0x01
0099 #define LM75_REG_HYST       0x02
0100 #define LM75_REG_MAX        0x03
0101 #define PCT2075_REG_IDLE    0x04
0102 
0103 /* Each client has this additional data */
0104 struct lm75_data {
0105     struct i2c_client       *client;
0106     struct regmap           *regmap;
0107     struct regulator        *vs;
0108     u8              orig_conf;
0109     u8              current_conf;
0110     u8              resolution; /* In bits, 9 to 16 */
0111     unsigned int            sample_time;    /* In ms */
0112     enum lm75_type          kind;
0113     const struct lm75_params    *params;
0114 };
0115 
0116 /*-----------------------------------------------------------------------*/
0117 
0118 static const u8 lm75_sample_set_masks[] = { 0 << 5, 1 << 5, 2 << 5, 3 << 5 };
0119 
0120 #define LM75_SAMPLE_CLEAR_MASK  (3 << 5)
0121 
0122 /* The structure below stores the configuration values of the supported devices.
0123  * In case of being supported multiple configurations, the default one must
0124  * always be the first element of the array
0125  */
0126 static const struct lm75_params device_params[] = {
0127     [adt75] = {
0128         .clr_mask = 1 << 5, /* not one-shot mode */
0129         .default_resolution = 12,
0130         .default_sample_time = MSEC_PER_SEC / 10,
0131     },
0132     [at30ts74] = {
0133         .set_mask = 3 << 5, /* 12-bit mode*/
0134         .default_resolution = 12,
0135         .default_sample_time = 200,
0136         .num_sample_times = 4,
0137         .sample_times = (unsigned int []){ 25, 50, 100, 200 },
0138         .resolutions = (u8 []) {9, 10, 11, 12 },
0139     },
0140     [ds1775] = {
0141         .clr_mask = 3 << 5,
0142         .set_mask = 2 << 5, /* 11-bit mode */
0143         .default_resolution = 11,
0144         .default_sample_time = 500,
0145         .num_sample_times = 4,
0146         .sample_times = (unsigned int []){ 125, 250, 500, 1000 },
0147         .resolutions = (u8 []) {9, 10, 11, 12 },
0148     },
0149     [ds75] = {
0150         .clr_mask = 3 << 5,
0151         .set_mask = 2 << 5, /* 11-bit mode */
0152         .default_resolution = 11,
0153         .default_sample_time = 600,
0154         .num_sample_times = 4,
0155         .sample_times = (unsigned int []){ 150, 300, 600, 1200 },
0156         .resolutions = (u8 []) {9, 10, 11, 12 },
0157     },
0158     [stds75] = {
0159         .clr_mask = 3 << 5,
0160         .set_mask = 2 << 5, /* 11-bit mode */
0161         .default_resolution = 11,
0162         .default_sample_time = 600,
0163         .num_sample_times = 4,
0164         .sample_times = (unsigned int []){ 150, 300, 600, 1200 },
0165         .resolutions = (u8 []) {9, 10, 11, 12 },
0166     },
0167     [stlm75] = {
0168         .default_resolution = 9,
0169         .default_sample_time = MSEC_PER_SEC / 6,
0170     },
0171     [ds7505] = {
0172         .set_mask = 3 << 5, /* 12-bit mode*/
0173         .default_resolution = 12,
0174         .default_sample_time = 200,
0175         .num_sample_times = 4,
0176         .sample_times = (unsigned int []){ 25, 50, 100, 200 },
0177         .resolutions = (u8 []) {9, 10, 11, 12 },
0178     },
0179     [g751] = {
0180         .default_resolution = 9,
0181         .default_sample_time = MSEC_PER_SEC / 10,
0182     },
0183     [lm75] = {
0184         .default_resolution = 9,
0185         .default_sample_time = MSEC_PER_SEC / 10,
0186     },
0187     [lm75a] = {
0188         .default_resolution = 9,
0189         .default_sample_time = MSEC_PER_SEC / 10,
0190     },
0191     [lm75b] = {
0192         .default_resolution = 11,
0193         .default_sample_time = MSEC_PER_SEC / 10,
0194     },
0195     [max6625] = {
0196         .default_resolution = 9,
0197         .default_sample_time = MSEC_PER_SEC / 7,
0198     },
0199     [max6626] = {
0200         .default_resolution = 12,
0201         .default_sample_time = MSEC_PER_SEC / 7,
0202         .resolution_limits = 9,
0203     },
0204     [max31725] = {
0205         .default_resolution = 16,
0206         .default_sample_time = MSEC_PER_SEC / 20,
0207     },
0208     [tcn75] = {
0209         .default_resolution = 9,
0210         .default_sample_time = MSEC_PER_SEC / 18,
0211     },
0212     [pct2075] = {
0213         .default_resolution = 11,
0214         .default_sample_time = MSEC_PER_SEC / 10,
0215         .num_sample_times = 31,
0216         .sample_times = (unsigned int []){ 100, 200, 300, 400, 500, 600,
0217         700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700,
0218         1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700,
0219         2800, 2900, 3000, 3100 },
0220     },
0221     [mcp980x] = {
0222         .set_mask = 3 << 5, /* 12-bit mode */
0223         .clr_mask = 1 << 7, /* not one-shot mode */
0224         .default_resolution = 12,
0225         .resolution_limits = 9,
0226         .default_sample_time = 240,
0227         .num_sample_times = 4,
0228         .sample_times = (unsigned int []){ 30, 60, 120, 240 },
0229         .resolutions = (u8 []) {9, 10, 11, 12 },
0230     },
0231     [tmp100] = {
0232         .set_mask = 3 << 5, /* 12-bit mode */
0233         .clr_mask = 1 << 7, /* not one-shot mode */
0234         .default_resolution = 12,
0235         .default_sample_time = 320,
0236         .num_sample_times = 4,
0237         .sample_times = (unsigned int []){ 40, 80, 160, 320 },
0238         .resolutions = (u8 []) {9, 10, 11, 12 },
0239     },
0240     [tmp101] = {
0241         .set_mask = 3 << 5, /* 12-bit mode */
0242         .clr_mask = 1 << 7, /* not one-shot mode */
0243         .default_resolution = 12,
0244         .default_sample_time = 320,
0245         .num_sample_times = 4,
0246         .sample_times = (unsigned int []){ 40, 80, 160, 320 },
0247         .resolutions = (u8 []) {9, 10, 11, 12 },
0248     },
0249     [tmp105] = {
0250         .set_mask = 3 << 5, /* 12-bit mode */
0251         .clr_mask = 1 << 7, /* not one-shot mode*/
0252         .default_resolution = 12,
0253         .default_sample_time = 220,
0254         .num_sample_times = 4,
0255         .sample_times = (unsigned int []){ 28, 55, 110, 220 },
0256         .resolutions = (u8 []) {9, 10, 11, 12 },
0257     },
0258     [tmp112] = {
0259         .set_mask = 3 << 5, /* 8 samples / second */
0260         .clr_mask = 1 << 7, /* no one-shot mode*/
0261         .default_resolution = 12,
0262         .default_sample_time = 125,
0263         .num_sample_times = 4,
0264         .sample_times = (unsigned int []){ 125, 250, 1000, 4000 },
0265     },
0266     [tmp175] = {
0267         .set_mask = 3 << 5, /* 12-bit mode */
0268         .clr_mask = 1 << 7, /* not one-shot mode*/
0269         .default_resolution = 12,
0270         .default_sample_time = 220,
0271         .num_sample_times = 4,
0272         .sample_times = (unsigned int []){ 28, 55, 110, 220 },
0273         .resolutions = (u8 []) {9, 10, 11, 12 },
0274     },
0275     [tmp275] = {
0276         .set_mask = 3 << 5, /* 12-bit mode */
0277         .clr_mask = 1 << 7, /* not one-shot mode*/
0278         .default_resolution = 12,
0279         .default_sample_time = 220,
0280         .num_sample_times = 4,
0281         .sample_times = (unsigned int []){ 28, 55, 110, 220 },
0282         .resolutions = (u8 []) {9, 10, 11, 12 },
0283     },
0284     [tmp75] = {
0285         .set_mask = 3 << 5, /* 12-bit mode */
0286         .clr_mask = 1 << 7, /* not one-shot mode*/
0287         .default_resolution = 12,
0288         .default_sample_time = 220,
0289         .num_sample_times = 4,
0290         .sample_times = (unsigned int []){ 28, 55, 110, 220 },
0291         .resolutions = (u8 []) {9, 10, 11, 12 },
0292     },
0293     [tmp75b] = { /* not one-shot mode, Conversion rate 37Hz */
0294         .clr_mask = 1 << 7 | 3 << 5,
0295         .default_resolution = 12,
0296         .default_sample_time = MSEC_PER_SEC / 37,
0297         .sample_times = (unsigned int []){ MSEC_PER_SEC / 37,
0298             MSEC_PER_SEC / 18,
0299             MSEC_PER_SEC / 9, MSEC_PER_SEC / 4 },
0300         .num_sample_times = 4,
0301     },
0302     [tmp75c] = {
0303         .clr_mask = 1 << 5, /*not one-shot mode*/
0304         .default_resolution = 12,
0305         .default_sample_time = MSEC_PER_SEC / 12,
0306     },
0307     [tmp1075] = { /* not one-shot mode, 27.5 ms sample rate */
0308         .clr_mask = 1 << 5 | 1 << 6 | 1 << 7,
0309         .default_resolution = 12,
0310         .default_sample_time = 28,
0311         .num_sample_times = 4,
0312         .sample_times = (unsigned int []){ 28, 55, 110, 220 },
0313     }
0314 };
0315 
0316 static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
0317 {
0318     return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
0319 }
0320 
0321 static int lm75_write_config(struct lm75_data *data, u8 set_mask,
0322                  u8 clr_mask)
0323 {
0324     u8 value;
0325 
0326     clr_mask |= LM75_SHUTDOWN;
0327     value = data->current_conf & ~clr_mask;
0328     value |= set_mask;
0329 
0330     if (data->current_conf != value) {
0331         s32 err;
0332 
0333         err = i2c_smbus_write_byte_data(data->client, LM75_REG_CONF,
0334                         value);
0335         if (err)
0336             return err;
0337         data->current_conf = value;
0338     }
0339     return 0;
0340 }
0341 
0342 static int lm75_read(struct device *dev, enum hwmon_sensor_types type,
0343              u32 attr, int channel, long *val)
0344 {
0345     struct lm75_data *data = dev_get_drvdata(dev);
0346     unsigned int regval;
0347     int err, reg;
0348 
0349     switch (type) {
0350     case hwmon_chip:
0351         switch (attr) {
0352         case hwmon_chip_update_interval:
0353             *val = data->sample_time;
0354             break;
0355         default:
0356             return -EINVAL;
0357         }
0358         break;
0359     case hwmon_temp:
0360         switch (attr) {
0361         case hwmon_temp_input:
0362             reg = LM75_REG_TEMP;
0363             break;
0364         case hwmon_temp_max:
0365             reg = LM75_REG_MAX;
0366             break;
0367         case hwmon_temp_max_hyst:
0368             reg = LM75_REG_HYST;
0369             break;
0370         default:
0371             return -EINVAL;
0372         }
0373         err = regmap_read(data->regmap, reg, &regval);
0374         if (err < 0)
0375             return err;
0376 
0377         *val = lm75_reg_to_mc(regval, data->resolution);
0378         break;
0379     default:
0380         return -EINVAL;
0381     }
0382     return 0;
0383 }
0384 
0385 static int lm75_write_temp(struct device *dev, u32 attr, long temp)
0386 {
0387     struct lm75_data *data = dev_get_drvdata(dev);
0388     u8 resolution;
0389     int reg;
0390 
0391     switch (attr) {
0392     case hwmon_temp_max:
0393         reg = LM75_REG_MAX;
0394         break;
0395     case hwmon_temp_max_hyst:
0396         reg = LM75_REG_HYST;
0397         break;
0398     default:
0399         return -EINVAL;
0400     }
0401 
0402     /*
0403      * Resolution of limit registers is assumed to be the same as the
0404      * temperature input register resolution unless given explicitly.
0405      */
0406     if (data->params->resolution_limits)
0407         resolution = data->params->resolution_limits;
0408     else
0409         resolution = data->resolution;
0410 
0411     temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
0412     temp = DIV_ROUND_CLOSEST(temp  << (resolution - 8),
0413                  1000) << (16 - resolution);
0414 
0415     return regmap_write(data->regmap, reg, (u16)temp);
0416 }
0417 
0418 static int lm75_update_interval(struct device *dev, long val)
0419 {
0420     struct lm75_data *data = dev_get_drvdata(dev);
0421     unsigned int reg;
0422     u8 index;
0423     s32 err;
0424 
0425     index = find_closest(val, data->params->sample_times,
0426                  (int)data->params->num_sample_times);
0427 
0428     switch (data->kind) {
0429     default:
0430         err = lm75_write_config(data, lm75_sample_set_masks[index],
0431                     LM75_SAMPLE_CLEAR_MASK);
0432         if (err)
0433             return err;
0434 
0435         data->sample_time = data->params->sample_times[index];
0436         if (data->params->resolutions)
0437             data->resolution = data->params->resolutions[index];
0438         break;
0439     case tmp112:
0440         err = regmap_read(data->regmap, LM75_REG_CONF, &reg);
0441         if (err < 0)
0442             return err;
0443         reg &= ~0x00c0;
0444         reg |= (3 - index) << 6;
0445         err = regmap_write(data->regmap, LM75_REG_CONF, reg);
0446         if (err < 0)
0447             return err;
0448         data->sample_time = data->params->sample_times[index];
0449         break;
0450     case pct2075:
0451         err = i2c_smbus_write_byte_data(data->client, PCT2075_REG_IDLE,
0452                         index + 1);
0453         if (err)
0454             return err;
0455         data->sample_time = data->params->sample_times[index];
0456         break;
0457     }
0458     return 0;
0459 }
0460 
0461 static int lm75_write_chip(struct device *dev, u32 attr, long val)
0462 {
0463     switch (attr) {
0464     case hwmon_chip_update_interval:
0465         return lm75_update_interval(dev, val);
0466     default:
0467         return -EINVAL;
0468     }
0469     return 0;
0470 }
0471 
0472 static int lm75_write(struct device *dev, enum hwmon_sensor_types type,
0473               u32 attr, int channel, long val)
0474 {
0475     switch (type) {
0476     case hwmon_chip:
0477         return lm75_write_chip(dev, attr, val);
0478     case hwmon_temp:
0479         return lm75_write_temp(dev, attr, val);
0480     default:
0481         return -EINVAL;
0482     }
0483     return 0;
0484 }
0485 
0486 static umode_t lm75_is_visible(const void *data, enum hwmon_sensor_types type,
0487                    u32 attr, int channel)
0488 {
0489     const struct lm75_data *config_data = data;
0490 
0491     switch (type) {
0492     case hwmon_chip:
0493         switch (attr) {
0494         case hwmon_chip_update_interval:
0495             if (config_data->params->num_sample_times > 1)
0496                 return 0644;
0497             return 0444;
0498         }
0499         break;
0500     case hwmon_temp:
0501         switch (attr) {
0502         case hwmon_temp_input:
0503             return 0444;
0504         case hwmon_temp_max:
0505         case hwmon_temp_max_hyst:
0506             return 0644;
0507         }
0508         break;
0509     default:
0510         break;
0511     }
0512     return 0;
0513 }
0514 
0515 static const struct hwmon_channel_info *lm75_info[] = {
0516     HWMON_CHANNEL_INFO(chip,
0517                HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
0518     HWMON_CHANNEL_INFO(temp,
0519                HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST),
0520     NULL
0521 };
0522 
0523 static const struct hwmon_ops lm75_hwmon_ops = {
0524     .is_visible = lm75_is_visible,
0525     .read = lm75_read,
0526     .write = lm75_write,
0527 };
0528 
0529 static const struct hwmon_chip_info lm75_chip_info = {
0530     .ops = &lm75_hwmon_ops,
0531     .info = lm75_info,
0532 };
0533 
0534 static bool lm75_is_writeable_reg(struct device *dev, unsigned int reg)
0535 {
0536     return reg != LM75_REG_TEMP;
0537 }
0538 
0539 static bool lm75_is_volatile_reg(struct device *dev, unsigned int reg)
0540 {
0541     return reg == LM75_REG_TEMP || reg == LM75_REG_CONF;
0542 }
0543 
0544 static const struct regmap_config lm75_regmap_config = {
0545     .reg_bits = 8,
0546     .val_bits = 16,
0547     .max_register = PCT2075_REG_IDLE,
0548     .writeable_reg = lm75_is_writeable_reg,
0549     .volatile_reg = lm75_is_volatile_reg,
0550     .val_format_endian = REGMAP_ENDIAN_BIG,
0551     .cache_type = REGCACHE_RBTREE,
0552     .use_single_read = true,
0553     .use_single_write = true,
0554 };
0555 
0556 static void lm75_disable_regulator(void *data)
0557 {
0558     struct lm75_data *lm75 = data;
0559 
0560     regulator_disable(lm75->vs);
0561 }
0562 
0563 static void lm75_remove(void *data)
0564 {
0565     struct lm75_data *lm75 = data;
0566     struct i2c_client *client = lm75->client;
0567 
0568     i2c_smbus_write_byte_data(client, LM75_REG_CONF, lm75->orig_conf);
0569 }
0570 
0571 static const struct i2c_device_id lm75_ids[];
0572 
0573 static int lm75_probe(struct i2c_client *client)
0574 {
0575     struct device *dev = &client->dev;
0576     struct device *hwmon_dev;
0577     struct lm75_data *data;
0578     int status, err;
0579     enum lm75_type kind;
0580 
0581     if (client->dev.of_node)
0582         kind = (enum lm75_type)of_device_get_match_data(&client->dev);
0583     else
0584         kind = i2c_match_id(lm75_ids, client)->driver_data;
0585 
0586     if (!i2c_check_functionality(client->adapter,
0587             I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
0588         return -EIO;
0589 
0590     data = devm_kzalloc(dev, sizeof(struct lm75_data), GFP_KERNEL);
0591     if (!data)
0592         return -ENOMEM;
0593 
0594     data->client = client;
0595     data->kind = kind;
0596 
0597     data->vs = devm_regulator_get(dev, "vs");
0598     if (IS_ERR(data->vs))
0599         return PTR_ERR(data->vs);
0600 
0601     data->regmap = devm_regmap_init_i2c(client, &lm75_regmap_config);
0602     if (IS_ERR(data->regmap))
0603         return PTR_ERR(data->regmap);
0604 
0605     /* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
0606      * Then tweak to be more precise when appropriate.
0607      */
0608 
0609     data->params = &device_params[data->kind];
0610 
0611     /* Save default sample time and resolution*/
0612     data->sample_time = data->params->default_sample_time;
0613     data->resolution = data->params->default_resolution;
0614 
0615     /* Enable the power */
0616     err = regulator_enable(data->vs);
0617     if (err) {
0618         dev_err(dev, "failed to enable regulator: %d\n", err);
0619         return err;
0620     }
0621 
0622     err = devm_add_action_or_reset(dev, lm75_disable_regulator, data);
0623     if (err)
0624         return err;
0625 
0626     /* Cache original configuration */
0627     status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
0628     if (status < 0) {
0629         dev_dbg(dev, "Can't read config? %d\n", status);
0630         return status;
0631     }
0632     data->orig_conf = status;
0633     data->current_conf = status;
0634 
0635     err = lm75_write_config(data, data->params->set_mask,
0636                 data->params->clr_mask);
0637     if (err)
0638         return err;
0639 
0640     err = devm_add_action_or_reset(dev, lm75_remove, data);
0641     if (err)
0642         return err;
0643 
0644     hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
0645                              data, &lm75_chip_info,
0646                              NULL);
0647     if (IS_ERR(hwmon_dev))
0648         return PTR_ERR(hwmon_dev);
0649 
0650     dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), client->name);
0651 
0652     return 0;
0653 }
0654 
0655 static const struct i2c_device_id lm75_ids[] = {
0656     { "adt75", adt75, },
0657     { "at30ts74", at30ts74, },
0658     { "ds1775", ds1775, },
0659     { "ds75", ds75, },
0660     { "ds7505", ds7505, },
0661     { "g751", g751, },
0662     { "lm75", lm75, },
0663     { "lm75a", lm75a, },
0664     { "lm75b", lm75b, },
0665     { "max6625", max6625, },
0666     { "max6626", max6626, },
0667     { "max31725", max31725, },
0668     { "max31726", max31725, },
0669     { "mcp980x", mcp980x, },
0670     { "pct2075", pct2075, },
0671     { "stds75", stds75, },
0672     { "stlm75", stlm75, },
0673     { "tcn75", tcn75, },
0674     { "tmp100", tmp100, },
0675     { "tmp101", tmp101, },
0676     { "tmp105", tmp105, },
0677     { "tmp112", tmp112, },
0678     { "tmp175", tmp175, },
0679     { "tmp275", tmp275, },
0680     { "tmp75", tmp75, },
0681     { "tmp75b", tmp75b, },
0682     { "tmp75c", tmp75c, },
0683     { "tmp1075", tmp1075, },
0684     { /* LIST END */ }
0685 };
0686 MODULE_DEVICE_TABLE(i2c, lm75_ids);
0687 
0688 static const struct of_device_id __maybe_unused lm75_of_match[] = {
0689     {
0690         .compatible = "adi,adt75",
0691         .data = (void *)adt75
0692     },
0693     {
0694         .compatible = "atmel,at30ts74",
0695         .data = (void *)at30ts74
0696     },
0697     {
0698         .compatible = "dallas,ds1775",
0699         .data = (void *)ds1775
0700     },
0701     {
0702         .compatible = "dallas,ds75",
0703         .data = (void *)ds75
0704     },
0705     {
0706         .compatible = "dallas,ds7505",
0707         .data = (void *)ds7505
0708     },
0709     {
0710         .compatible = "gmt,g751",
0711         .data = (void *)g751
0712     },
0713     {
0714         .compatible = "national,lm75",
0715         .data = (void *)lm75
0716     },
0717     {
0718         .compatible = "national,lm75a",
0719         .data = (void *)lm75a
0720     },
0721     {
0722         .compatible = "national,lm75b",
0723         .data = (void *)lm75b
0724     },
0725     {
0726         .compatible = "maxim,max6625",
0727         .data = (void *)max6625
0728     },
0729     {
0730         .compatible = "maxim,max6626",
0731         .data = (void *)max6626
0732     },
0733     {
0734         .compatible = "maxim,max31725",
0735         .data = (void *)max31725
0736     },
0737     {
0738         .compatible = "maxim,max31726",
0739         .data = (void *)max31725
0740     },
0741     {
0742         .compatible = "maxim,mcp980x",
0743         .data = (void *)mcp980x
0744     },
0745     {
0746         .compatible = "nxp,pct2075",
0747         .data = (void *)pct2075
0748     },
0749     {
0750         .compatible = "st,stds75",
0751         .data = (void *)stds75
0752     },
0753     {
0754         .compatible = "st,stlm75",
0755         .data = (void *)stlm75
0756     },
0757     {
0758         .compatible = "microchip,tcn75",
0759         .data = (void *)tcn75
0760     },
0761     {
0762         .compatible = "ti,tmp100",
0763         .data = (void *)tmp100
0764     },
0765     {
0766         .compatible = "ti,tmp101",
0767         .data = (void *)tmp101
0768     },
0769     {
0770         .compatible = "ti,tmp105",
0771         .data = (void *)tmp105
0772     },
0773     {
0774         .compatible = "ti,tmp112",
0775         .data = (void *)tmp112
0776     },
0777     {
0778         .compatible = "ti,tmp175",
0779         .data = (void *)tmp175
0780     },
0781     {
0782         .compatible = "ti,tmp275",
0783         .data = (void *)tmp275
0784     },
0785     {
0786         .compatible = "ti,tmp75",
0787         .data = (void *)tmp75
0788     },
0789     {
0790         .compatible = "ti,tmp75b",
0791         .data = (void *)tmp75b
0792     },
0793     {
0794         .compatible = "ti,tmp75c",
0795         .data = (void *)tmp75c
0796     },
0797     {
0798         .compatible = "ti,tmp1075",
0799         .data = (void *)tmp1075
0800     },
0801     { },
0802 };
0803 MODULE_DEVICE_TABLE(of, lm75_of_match);
0804 
0805 #define LM75A_ID 0xA1
0806 
0807 /* Return 0 if detection is successful, -ENODEV otherwise */
0808 static int lm75_detect(struct i2c_client *new_client,
0809                struct i2c_board_info *info)
0810 {
0811     struct i2c_adapter *adapter = new_client->adapter;
0812     int i;
0813     int conf, hyst, os;
0814     bool is_lm75a = 0;
0815 
0816     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
0817                      I2C_FUNC_SMBUS_WORD_DATA))
0818         return -ENODEV;
0819 
0820     /*
0821      * Now, we do the remaining detection. There is no identification-
0822      * dedicated register so we have to rely on several tricks:
0823      * unused bits, registers cycling over 8-address boundaries,
0824      * addresses 0x04-0x07 returning the last read value.
0825      * The cycling+unused addresses combination is not tested,
0826      * since it would significantly slow the detection down and would
0827      * hardly add any value.
0828      *
0829      * The National Semiconductor LM75A is different than earlier
0830      * LM75s.  It has an ID byte of 0xaX (where X is the chip
0831      * revision, with 1 being the only revision in existence) in
0832      * register 7, and unused registers return 0xff rather than the
0833      * last read value.
0834      *
0835      * Note that this function only detects the original National
0836      * Semiconductor LM75 and the LM75A. Clones from other vendors
0837      * aren't detected, on purpose, because they are typically never
0838      * found on PC hardware. They are found on embedded designs where
0839      * they can be instantiated explicitly so detection is not needed.
0840      * The absence of identification registers on all these clones
0841      * would make their exhaustive detection very difficult and weak,
0842      * and odds are that the driver would bind to unsupported devices.
0843      */
0844 
0845     /* Unused bits */
0846     conf = i2c_smbus_read_byte_data(new_client, 1);
0847     if (conf & 0xe0)
0848         return -ENODEV;
0849 
0850     /* First check for LM75A */
0851     if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) {
0852         /*
0853          * LM75A returns 0xff on unused registers so
0854          * just to be sure we check for that too.
0855          */
0856         if (i2c_smbus_read_byte_data(new_client, 4) != 0xff
0857          || i2c_smbus_read_byte_data(new_client, 5) != 0xff
0858          || i2c_smbus_read_byte_data(new_client, 6) != 0xff)
0859             return -ENODEV;
0860         is_lm75a = 1;
0861         hyst = i2c_smbus_read_byte_data(new_client, 2);
0862         os = i2c_smbus_read_byte_data(new_client, 3);
0863     } else { /* Traditional style LM75 detection */
0864         /* Unused addresses */
0865         hyst = i2c_smbus_read_byte_data(new_client, 2);
0866         if (i2c_smbus_read_byte_data(new_client, 4) != hyst
0867          || i2c_smbus_read_byte_data(new_client, 5) != hyst
0868          || i2c_smbus_read_byte_data(new_client, 6) != hyst
0869          || i2c_smbus_read_byte_data(new_client, 7) != hyst)
0870             return -ENODEV;
0871         os = i2c_smbus_read_byte_data(new_client, 3);
0872         if (i2c_smbus_read_byte_data(new_client, 4) != os
0873          || i2c_smbus_read_byte_data(new_client, 5) != os
0874          || i2c_smbus_read_byte_data(new_client, 6) != os
0875          || i2c_smbus_read_byte_data(new_client, 7) != os)
0876             return -ENODEV;
0877     }
0878     /*
0879      * It is very unlikely that this is a LM75 if both
0880      * hysteresis and temperature limit registers are 0.
0881      */
0882     if (hyst == 0 && os == 0)
0883         return -ENODEV;
0884 
0885     /* Addresses cycling */
0886     for (i = 8; i <= 248; i += 40) {
0887         if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
0888          || i2c_smbus_read_byte_data(new_client, i + 2) != hyst
0889          || i2c_smbus_read_byte_data(new_client, i + 3) != os)
0890             return -ENODEV;
0891         if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7)
0892                 != LM75A_ID)
0893             return -ENODEV;
0894     }
0895 
0896     strlcpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE);
0897 
0898     return 0;
0899 }
0900 
0901 #ifdef CONFIG_PM
0902 static int lm75_suspend(struct device *dev)
0903 {
0904     int status;
0905     struct i2c_client *client = to_i2c_client(dev);
0906 
0907     status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
0908     if (status < 0) {
0909         dev_dbg(&client->dev, "Can't read config? %d\n", status);
0910         return status;
0911     }
0912     status = status | LM75_SHUTDOWN;
0913     i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
0914     return 0;
0915 }
0916 
0917 static int lm75_resume(struct device *dev)
0918 {
0919     int status;
0920     struct i2c_client *client = to_i2c_client(dev);
0921 
0922     status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
0923     if (status < 0) {
0924         dev_dbg(&client->dev, "Can't read config? %d\n", status);
0925         return status;
0926     }
0927     status = status & ~LM75_SHUTDOWN;
0928     i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
0929     return 0;
0930 }
0931 
0932 static const struct dev_pm_ops lm75_dev_pm_ops = {
0933     .suspend    = lm75_suspend,
0934     .resume     = lm75_resume,
0935 };
0936 #define LM75_DEV_PM_OPS (&lm75_dev_pm_ops)
0937 #else
0938 #define LM75_DEV_PM_OPS NULL
0939 #endif /* CONFIG_PM */
0940 
0941 static struct i2c_driver lm75_driver = {
0942     .class      = I2C_CLASS_HWMON,
0943     .driver = {
0944         .name   = "lm75",
0945         .of_match_table = of_match_ptr(lm75_of_match),
0946         .pm = LM75_DEV_PM_OPS,
0947     },
0948     .probe_new  = lm75_probe,
0949     .id_table   = lm75_ids,
0950     .detect     = lm75_detect,
0951     .address_list   = normal_i2c,
0952 };
0953 
0954 module_i2c_driver(lm75_driver);
0955 
0956 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
0957 MODULE_DESCRIPTION("LM75 driver");
0958 MODULE_LICENSE("GPL");