Skip to content

Commit f07f9d2

Browse files
andy-shevgroeck
authored andcommitted
hwmon: (tmp513) Use SI constants from units.h
MILLI and MICRO may be used in the driver to make code more robust against possible miscalculations. Signed-off-by: Andy Shevchenko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Guenter Roeck <[email protected]>
1 parent df98976 commit f07f9d2

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

drivers/hwmon/tmp513.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <linux/regmap.h>
3333
#include <linux/slab.h>
3434
#include <linux/types.h>
35+
#include <linux/units.h>
3536

3637
// Common register definition
3738
#define TMP51X_SHUNT_CONFIG 0x00
@@ -101,8 +102,8 @@
101102
#define TMP51X_REMOTE_TEMP_LIMIT_2_POS 8
102103
#define TMP513_REMOTE_TEMP_LIMIT_3_POS 7
103104

104-
#define TMP51X_VBUS_RANGE_32V 32000000
105-
#define TMP51X_VBUS_RANGE_16V 16000000
105+
#define TMP51X_VBUS_RANGE_32V (32 * MICRO)
106+
#define TMP51X_VBUS_RANGE_16V (16 * MICRO)
106107

107108
// Max and Min value
108109
#define MAX_BUS_VOLTAGE_32_LIMIT 32764
@@ -204,7 +205,7 @@ static int tmp51x_get_value(struct tmp51x_data *data, u8 reg, u8 pos,
204205
* on the pga gain setting. 1lsb = 10uV
205206
*/
206207
*val = sign_extend32(regval, 17 - tmp51x_get_pga_shift(data));
207-
*val = DIV_ROUND_CLOSEST(*val * 10000, data->shunt_uohms);
208+
*val = DIV_ROUND_CLOSEST(*val * 10 * MILLI, data->shunt_uohms);
208209
break;
209210
case TMP51X_BUS_VOLTAGE_RESULT:
210211
case TMP51X_BUS_VOLTAGE_H_LIMIT:
@@ -220,7 +221,7 @@ static int tmp51x_get_value(struct tmp51x_data *data, u8 reg, u8 pos,
220221
case TMP51X_BUS_CURRENT_RESULT:
221222
// Current = (ShuntVoltage * CalibrationRegister) / 4096
222223
*val = sign_extend32(regval, 16) * data->curr_lsb_ua;
223-
*val = DIV_ROUND_CLOSEST(*val, 1000);
224+
*val = DIV_ROUND_CLOSEST(*val, MILLI);
224225
break;
225226
case TMP51X_LOCAL_TEMP_RESULT:
226227
case TMP51X_REMOTE_TEMP_RESULT_1:
@@ -260,7 +261,7 @@ static int tmp51x_set_value(struct tmp51x_data *data, u8 reg, long val)
260261
* The user enter current value and we convert it to
261262
* voltage. 1lsb = 10uV
262263
*/
263-
val = DIV_ROUND_CLOSEST(val * data->shunt_uohms, 10000);
264+
val = DIV_ROUND_CLOSEST(val * data->shunt_uohms, 10 * MILLI);
264265
max_val = U16_MAX >> tmp51x_get_pga_shift(data);
265266
regval = clamp_val(val, -max_val, max_val);
266267
break;
@@ -550,18 +551,16 @@ static int tmp51x_calibrate(struct tmp51x_data *data)
550551
if (data->shunt_uohms == 0)
551552
return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION, 0);
552553

553-
max_curr_ma = DIV_ROUND_CLOSEST_ULL(vshunt_max * 1000 * 1000,
554-
data->shunt_uohms);
554+
max_curr_ma = DIV_ROUND_CLOSEST_ULL(vshunt_max * MICRO, data->shunt_uohms);
555555

556556
/*
557557
* Calculate the minimal bit resolution for the current and the power.
558558
* Those values will be used during register interpretation.
559559
*/
560-
data->curr_lsb_ua = DIV_ROUND_CLOSEST_ULL(max_curr_ma * 1000, 32767);
560+
data->curr_lsb_ua = DIV_ROUND_CLOSEST_ULL(max_curr_ma * MILLI, 32767);
561561
data->pwr_lsb_uw = 20 * data->curr_lsb_ua;
562562

563-
div = DIV_ROUND_CLOSEST_ULL(data->curr_lsb_ua * data->shunt_uohms,
564-
1000 * 1000);
563+
div = DIV_ROUND_CLOSEST_ULL(data->curr_lsb_ua * data->shunt_uohms, MICRO);
565564

566565
return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION,
567566
DIV_ROUND_CLOSEST(40960, div));
@@ -678,7 +677,7 @@ static int tmp51x_read_properties(struct device *dev, struct tmp51x_data *data)
678677
data->max_channels - 1);
679678

680679
// Check if shunt value is compatible with pga-gain
681-
if (data->shunt_uohms > data->pga_gain * 40 * 1000 * 1000) {
680+
if (data->shunt_uohms > data->pga_gain * 40 * MICRO) {
682681
return dev_err_probe(dev, -EINVAL,
683682
"shunt-resistor: %u too big for pga_gain: %u\n",
684683
data->shunt_uohms, data->pga_gain);

0 commit comments

Comments
 (0)