]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
hwmon: (lm92) Improve auto-detection accuracy
authorGuenter Roeck <linux@roeck-us.net>
Wed, 17 Jul 2024 19:13:12 +0000 (12:13 -0700)
committerGuenter Roeck <linux@roeck-us.net>
Tue, 27 Aug 2024 15:10:22 +0000 (08:10 -0700)
Checking three configuration register bits and the manufacturer ID
register to auto-detect LM92 is a bit vague. Repeat twice on replicated
register addresses to improve detection accuracy.  Check the manufacturer
ID first and bail out immediately without reading the other register if
there is a mismatch to reduce the number of i2c transfers needed in that
case. Also explicitly test for an error from reading the configuration
register to avoid potential situations where the returned error masked
against 0xe0 is 0.

While at it, drop "lm92: Found National Semiconductor LM92 chip" detection
noise.

Reviewed-by: Tzung-Bi Shih <tzungbi@kernel.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
drivers/hwmon/lm92.c

index 46579a3e1715c94d6ecfedbdacf8532822ae5f09..e2d8c8afcbfa0d3481d93b274329a685e8f3498f 100644 (file)
@@ -272,20 +272,28 @@ static int lm92_detect(struct i2c_client *new_client,
                       struct i2c_board_info *info)
 {
        struct i2c_adapter *adapter = new_client->adapter;
-       u8 config;
-       u16 man_id;
+       u8 config_addr = LM92_REG_CONFIG;
+       u8 man_id_addr = LM92_REG_MAN_ID;
+       int i, regval;
 
        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
                                            | I2C_FUNC_SMBUS_WORD_DATA))
                return -ENODEV;
 
-       config = i2c_smbus_read_byte_data(new_client, LM92_REG_CONFIG);
-       man_id = i2c_smbus_read_word_data(new_client, LM92_REG_MAN_ID);
-
-       if ((config & 0xe0) == 0x00 && man_id == 0x0180)
-               pr_info("lm92: Found National Semiconductor LM92 chip\n");
-       else
-               return -ENODEV;
+       /*
+        * Register values repeat with multiples of 8.
+        * Read twice to improve detection accuracy.
+        */
+       for (i = 0; i < 2; i++) {
+               regval = i2c_smbus_read_word_data(new_client, man_id_addr);
+               if (regval != 0x0180)
+                       return -ENODEV;
+               regval = i2c_smbus_read_byte_data(new_client, config_addr);
+               if (regval < 0 || (regval & 0xe0))
+                       return -ENODEV;
+               config_addr += 8;
+               man_id_addr += 8;
+       }
 
        strscpy(info->type, "lm92", I2C_NAME_SIZE);