Commit b8189beb authored by Thomas Haemmerle's avatar Thomas Haemmerle Committed by Jonathan Cameron

iio: pressure: dps310: introduce consistent error handling

Align error handling with `dps310_calculate_temp`, where it's not
possible to differentiate between errors and valid calculations by
checking if the returned value is negative.
Signed-off-by: default avatarThomas Haemmerle <thomas.haemmerle@leica-geosystems.com>
Link: https://lore.kernel.org/r/20240415105030.1161770-3-thomas.haemmerle@leica-geosystems.comSigned-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent 9dd6b32e
...@@ -256,24 +256,24 @@ static int dps310_startup(struct dps310_data *data) ...@@ -256,24 +256,24 @@ static int dps310_startup(struct dps310_data *data)
return dps310_temp_workaround(data); return dps310_temp_workaround(data);
} }
static int dps310_get_pres_precision(struct dps310_data *data) static int dps310_get_pres_precision(struct dps310_data *data, int *val)
{ {
int rc; int reg_val, rc;
int val;
rc = regmap_read(data->regmap, DPS310_PRS_CFG, &val); rc = regmap_read(data->regmap, DPS310_PRS_CFG, &reg_val);
if (rc < 0) if (rc < 0)
return rc; return rc;
return BIT(val & GENMASK(2, 0)); *val = BIT(reg_val & GENMASK(2, 0));
return 0;
} }
static int dps310_get_temp_precision(struct dps310_data *data) static int dps310_get_temp_precision(struct dps310_data *data, int *val)
{ {
int rc; int reg_val, rc;
int val;
rc = regmap_read(data->regmap, DPS310_TMP_CFG, &val); rc = regmap_read(data->regmap, DPS310_TMP_CFG, &reg_val);
if (rc < 0) if (rc < 0)
return rc; return rc;
...@@ -281,7 +281,9 @@ static int dps310_get_temp_precision(struct dps310_data *data) ...@@ -281,7 +281,9 @@ static int dps310_get_temp_precision(struct dps310_data *data)
* Scale factor is bottom 4 bits of the register, but 1111 is * Scale factor is bottom 4 bits of the register, but 1111 is
* reserved so just grab bottom three * reserved so just grab bottom three
*/ */
return BIT(val & GENMASK(2, 0)); *val = BIT(reg_val & GENMASK(2, 0));
return 0;
} }
/* Called with lock held */ /* Called with lock held */
...@@ -350,48 +352,56 @@ static int dps310_set_temp_samp_freq(struct dps310_data *data, int freq) ...@@ -350,48 +352,56 @@ static int dps310_set_temp_samp_freq(struct dps310_data *data, int freq)
DPS310_TMP_RATE_BITS, val); DPS310_TMP_RATE_BITS, val);
} }
static int dps310_get_pres_samp_freq(struct dps310_data *data) static int dps310_get_pres_samp_freq(struct dps310_data *data, int *val)
{ {
int rc; int reg_val, rc;
int val;
rc = regmap_read(data->regmap, DPS310_PRS_CFG, &val); rc = regmap_read(data->regmap, DPS310_PRS_CFG, &reg_val);
if (rc < 0) if (rc < 0)
return rc; return rc;
return BIT((val & DPS310_PRS_RATE_BITS) >> 4); *val = BIT((reg_val & DPS310_PRS_RATE_BITS) >> 4);
return 0;
} }
static int dps310_get_temp_samp_freq(struct dps310_data *data) static int dps310_get_temp_samp_freq(struct dps310_data *data, int *val)
{ {
int rc; int reg_val, rc;
int val;
rc = regmap_read(data->regmap, DPS310_TMP_CFG, &val); rc = regmap_read(data->regmap, DPS310_TMP_CFG, &reg_val);
if (rc < 0) if (rc < 0)
return rc; return rc;
return BIT((val & DPS310_TMP_RATE_BITS) >> 4); *val = BIT((reg_val & DPS310_TMP_RATE_BITS) >> 4);
return 0;
} }
static int dps310_get_pres_k(struct dps310_data *data) static int dps310_get_pres_k(struct dps310_data *data, int *val)
{ {
int rc = dps310_get_pres_precision(data); int reg_val, rc;
if (rc < 0) rc = dps310_get_pres_precision(data, &reg_val);
if (rc)
return rc; return rc;
return scale_factors[ilog2(rc)]; *val = scale_factors[ilog2(reg_val)];
return 0;
} }
static int dps310_get_temp_k(struct dps310_data *data) static int dps310_get_temp_k(struct dps310_data *data, int *val)
{ {
int rc = dps310_get_temp_precision(data); int reg_val, rc;
if (rc < 0) rc = dps310_get_temp_precision(data, &reg_val);
if (rc)
return rc; return rc;
return scale_factors[ilog2(rc)]; *val = scale_factors[ilog2(reg_val)];
return 0;
} }
static int dps310_reset_wait(struct dps310_data *data) static int dps310_reset_wait(struct dps310_data *data)
...@@ -464,7 +474,10 @@ static int dps310_read_pres_raw(struct dps310_data *data) ...@@ -464,7 +474,10 @@ static int dps310_read_pres_raw(struct dps310_data *data)
if (mutex_lock_interruptible(&data->lock)) if (mutex_lock_interruptible(&data->lock))
return -EINTR; return -EINTR;
rate = dps310_get_pres_samp_freq(data); rc = dps310_get_pres_samp_freq(data, &rate);
if (rc)
goto done;
timeout = DPS310_POLL_TIMEOUT_US(rate); timeout = DPS310_POLL_TIMEOUT_US(rate);
/* Poll for sensor readiness; base the timeout upon the sample rate. */ /* Poll for sensor readiness; base the timeout upon the sample rate. */
...@@ -510,7 +523,10 @@ static int dps310_read_temp_raw(struct dps310_data *data) ...@@ -510,7 +523,10 @@ static int dps310_read_temp_raw(struct dps310_data *data)
if (mutex_lock_interruptible(&data->lock)) if (mutex_lock_interruptible(&data->lock))
return -EINTR; return -EINTR;
rate = dps310_get_temp_samp_freq(data); rc = dps310_get_temp_samp_freq(data, &rate);
if (rc)
goto done;
timeout = DPS310_POLL_TIMEOUT_US(rate); timeout = DPS310_POLL_TIMEOUT_US(rate);
/* Poll for sensor readiness; base the timeout upon the sample rate. */ /* Poll for sensor readiness; base the timeout upon the sample rate. */
...@@ -612,13 +628,13 @@ static int dps310_write_raw(struct iio_dev *iio, ...@@ -612,13 +628,13 @@ static int dps310_write_raw(struct iio_dev *iio,
return rc; return rc;
} }
static int dps310_calculate_pressure(struct dps310_data *data) static int dps310_calculate_pressure(struct dps310_data *data, int *val)
{ {
int i; int i;
int rc; int rc;
int t_ready; int t_ready;
int kpi = dps310_get_pres_k(data); int kpi;
int kti = dps310_get_temp_k(data); int kti;
s64 rem = 0ULL; s64 rem = 0ULL;
s64 pressure = 0ULL; s64 pressure = 0ULL;
s64 p; s64 p;
...@@ -629,11 +645,13 @@ static int dps310_calculate_pressure(struct dps310_data *data) ...@@ -629,11 +645,13 @@ static int dps310_calculate_pressure(struct dps310_data *data)
s64 kp; s64 kp;
s64 kt; s64 kt;
if (kpi < 0) rc = dps310_get_pres_k(data, &kpi);
return kpi; if (rc)
return rc;
if (kti < 0) rc = dps310_get_temp_k(data, &kti);
return kti; if (rc)
return rc;
kp = (s64)kpi; kp = (s64)kpi;
kt = (s64)kti; kt = (s64)kti;
...@@ -687,7 +705,9 @@ static int dps310_calculate_pressure(struct dps310_data *data) ...@@ -687,7 +705,9 @@ static int dps310_calculate_pressure(struct dps310_data *data)
if (pressure < 0LL) if (pressure < 0LL)
return -ERANGE; return -ERANGE;
return (int)min_t(s64, pressure, INT_MAX); *val = (int)min_t(s64, pressure, INT_MAX);
return 0;
} }
static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2, static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2,
...@@ -697,11 +717,10 @@ static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2, ...@@ -697,11 +717,10 @@ static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2,
switch (mask) { switch (mask) {
case IIO_CHAN_INFO_SAMP_FREQ: case IIO_CHAN_INFO_SAMP_FREQ:
rc = dps310_get_pres_samp_freq(data); rc = dps310_get_pres_samp_freq(data, val);
if (rc < 0) if (rc)
return rc; return rc;
*val = rc;
return IIO_VAL_INT; return IIO_VAL_INT;
case IIO_CHAN_INFO_PROCESSED: case IIO_CHAN_INFO_PROCESSED:
...@@ -709,20 +728,17 @@ static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2, ...@@ -709,20 +728,17 @@ static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2,
if (rc) if (rc)
return rc; return rc;
rc = dps310_calculate_pressure(data); rc = dps310_calculate_pressure(data, val);
if (rc < 0) if (rc)
return rc; return rc;
*val = rc;
*val2 = 1000; /* Convert Pa to KPa per IIO ABI */ *val2 = 1000; /* Convert Pa to KPa per IIO ABI */
return IIO_VAL_FRACTIONAL; return IIO_VAL_FRACTIONAL;
case IIO_CHAN_INFO_OVERSAMPLING_RATIO: case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
rc = dps310_get_pres_precision(data); rc = dps310_get_pres_precision(data, val);
if (rc < 0) if (rc)
return rc; return rc;
*val = rc;
return IIO_VAL_INT; return IIO_VAL_INT;
default: default:
...@@ -734,10 +750,11 @@ static int dps310_calculate_temp(struct dps310_data *data, int *val) ...@@ -734,10 +750,11 @@ static int dps310_calculate_temp(struct dps310_data *data, int *val)
{ {
s64 c0; s64 c0;
s64 t; s64 t;
int kt = dps310_get_temp_k(data); int kt, rc;
if (kt < 0) rc = dps310_get_temp_k(data, &kt);
return kt; if (rc)
return rc;
/* Obtain inverse-scaled offset */ /* Obtain inverse-scaled offset */
c0 = div_s64((s64)kt * (s64)data->c0, 2); c0 = div_s64((s64)kt * (s64)data->c0, 2);
...@@ -758,11 +775,10 @@ static int dps310_read_temp(struct dps310_data *data, int *val, int *val2, ...@@ -758,11 +775,10 @@ static int dps310_read_temp(struct dps310_data *data, int *val, int *val2,
switch (mask) { switch (mask) {
case IIO_CHAN_INFO_SAMP_FREQ: case IIO_CHAN_INFO_SAMP_FREQ:
rc = dps310_get_temp_samp_freq(data); rc = dps310_get_temp_samp_freq(data, val);
if (rc < 0) if (rc)
return rc; return rc;
*val = rc;
return IIO_VAL_INT; return IIO_VAL_INT;
case IIO_CHAN_INFO_PROCESSED: case IIO_CHAN_INFO_PROCESSED:
...@@ -777,11 +793,10 @@ static int dps310_read_temp(struct dps310_data *data, int *val, int *val2, ...@@ -777,11 +793,10 @@ static int dps310_read_temp(struct dps310_data *data, int *val, int *val2,
return IIO_VAL_INT; return IIO_VAL_INT;
case IIO_CHAN_INFO_OVERSAMPLING_RATIO: case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
rc = dps310_get_temp_precision(data); rc = dps310_get_temp_precision(data, val);
if (rc < 0) if (rc)
return rc; return rc;
*val = rc;
return IIO_VAL_INT; return IIO_VAL_INT;
default: default:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment