]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
hwmon: (tmp513) Use SI constants from units.h
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Tue, 28 Nov 2023 18:06:04 +0000 (20:06 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 27 Dec 2024 12:52:58 +0000 (13:52 +0100)
[ Upstream commit f07f9d2467f4a298d24e186ddee6f69724903067 ]

MILLI and MICRO may be used in the driver to make code more robust
against possible miscalculations.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Link: https://lore.kernel.org/r/20231128180654.395692-4-andriy.shevchenko@linux.intel.com
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Stable-dep-of: 74d7e038fd07 ("hwmon: (tmp513) Fix interpretation of values of Shunt Voltage and Limit Registers")
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/hwmon/tmp513.c

index 53525d3a32a0a3e6b88f596b572c21ff86139aa2..589982c92190616582475d460446cae6456a0196 100644 (file)
@@ -32,6 +32,7 @@
 #include <linux/regmap.h>
 #include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/units.h>
 
 // Common register definition
 #define TMP51X_SHUNT_CONFIG            0x00
 #define TMP51X_REMOTE_TEMP_LIMIT_2_POS         8
 #define TMP513_REMOTE_TEMP_LIMIT_3_POS         7
 
-#define TMP51X_VBUS_RANGE_32V          32000000
-#define TMP51X_VBUS_RANGE_16V          16000000
+#define TMP51X_VBUS_RANGE_32V          (32 * MICRO)
+#define TMP51X_VBUS_RANGE_16V          (16 * MICRO)
 
 // Max and Min value
 #define MAX_BUS_VOLTAGE_32_LIMIT       32764
@@ -200,7 +201,7 @@ static int tmp51x_get_value(struct tmp51x_data *data, u8 reg, u8 pos,
                 * on the pga gain setting. 1lsb = 10uV
                 */
                *val = sign_extend32(regval, 17 - tmp51x_get_pga_shift(data));
-               *val = DIV_ROUND_CLOSEST(*val * 10000, data->shunt_uohms);
+               *val = DIV_ROUND_CLOSEST(*val * 10 * MILLI, data->shunt_uohms);
                break;
        case TMP51X_BUS_VOLTAGE_RESULT:
        case TMP51X_BUS_VOLTAGE_H_LIMIT:
@@ -216,7 +217,7 @@ static int tmp51x_get_value(struct tmp51x_data *data, u8 reg, u8 pos,
        case TMP51X_BUS_CURRENT_RESULT:
                // Current = (ShuntVoltage * CalibrationRegister) / 4096
                *val = sign_extend32(regval, 16) * data->curr_lsb_ua;
-               *val = DIV_ROUND_CLOSEST(*val, 1000);
+               *val = DIV_ROUND_CLOSEST(*val, MILLI);
                break;
        case TMP51X_LOCAL_TEMP_RESULT:
        case TMP51X_REMOTE_TEMP_RESULT_1:
@@ -256,7 +257,7 @@ static int tmp51x_set_value(struct tmp51x_data *data, u8 reg, long val)
                 * The user enter current value and we convert it to
                 * voltage. 1lsb = 10uV
                 */
-               val = DIV_ROUND_CLOSEST(val * data->shunt_uohms, 10000);
+               val = DIV_ROUND_CLOSEST(val * data->shunt_uohms, 10 * MILLI);
                max_val = U16_MAX >> tmp51x_get_pga_shift(data);
                regval = clamp_val(val, -max_val, max_val);
                break;
@@ -546,18 +547,16 @@ static int tmp51x_calibrate(struct tmp51x_data *data)
        if (data->shunt_uohms == 0)
                return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION, 0);
 
-       max_curr_ma = DIV_ROUND_CLOSEST_ULL(vshunt_max * 1000 * 1000,
-                                           data->shunt_uohms);
+       max_curr_ma = DIV_ROUND_CLOSEST_ULL(vshunt_max * MICRO, data->shunt_uohms);
 
        /*
         * Calculate the minimal bit resolution for the current and the power.
         * Those values will be used during register interpretation.
         */
-       data->curr_lsb_ua = DIV_ROUND_CLOSEST_ULL(max_curr_ma * 1000, 32767);
+       data->curr_lsb_ua = DIV_ROUND_CLOSEST_ULL(max_curr_ma * MILLI, 32767);
        data->pwr_lsb_uw = 20 * data->curr_lsb_ua;
 
-       div = DIV_ROUND_CLOSEST_ULL(data->curr_lsb_ua * data->shunt_uohms,
-                                   1000 * 1000);
+       div = DIV_ROUND_CLOSEST_ULL(data->curr_lsb_ua * data->shunt_uohms, MICRO);
 
        return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION,
                            DIV_ROUND_CLOSEST(40960, div));
@@ -683,7 +682,7 @@ static int tmp51x_read_properties(struct device *dev, struct tmp51x_data *data)
                memcpy(data->nfactor, nfactor, (data->id == tmp513) ? 3 : 2);
 
        // Check if shunt value is compatible with pga-gain
-       if (data->shunt_uohms > data->pga_gain * 40 * 1000 * 1000) {
+       if (data->shunt_uohms > data->pga_gain * 40 * MICRO) {
                return dev_err_probe(dev, -EINVAL,
                                     "shunt-resistor: %u too big for pga_gain: %u\n",
                                     data->shunt_uohms, data->pga_gain);