* [PATCH] iio: dps310.c: Remove duplication in functions
@ 2025-07-02 23:34 Lucas Eiji Uchiyama
2025-07-03 16:12 ` kernel test robot
0 siblings, 1 reply; 2+ messages in thread
From: Lucas Eiji Uchiyama @ 2025-07-02 23:34 UTC (permalink / raw)
To: linux-kernel; +Cc: Lucas Eiji Uchiyama
Consolidate the following functions into shared implementations:
- dps310_get_pres_precision
- dps310_get_temp_precision
- dps310_set_pres_precision
- dps310_set_temp_precision
- dps310_set_pres_samp_freq
- dps310_set_temp_samp_freq
- dps310_get_pres_samp_freq
- dps310_get_temp_samp_freq
- dps310_get_pres_k
- dps310_get_temp_k
These were replaced by the following unified functions:
- dps310_get_precision
- dps310_set_precision
- dps310_set_samp_freq
- dps310_get_samp_freq
- dps310_get_k
Each now takes an additional `mode` parameter indicating whether the
operation applies to temperature or pressure.
All call sites were updated accordingly. To improve readability, new
macros PRESSURE and TEMPERATURE were introduced and passed to the
shared functions.
Additionally, a new macro was defined for:
BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
BIT(IIO_CHAN_INFO_SAMP_FREQ) |
BIT(IIO_CHAN_INFO_PROCESSED)
since this combination was used twice in the same struct.
Signed-off-by: Lucas Eiji Uchiyama <lucaseiji54@gmail.com>
---
drivers/iio/pressure/dps310.c | 182 +++++++++++++---------------------
1 file changed, 69 insertions(+), 113 deletions(-)
diff --git a/drivers/iio/pressure/dps310.c b/drivers/iio/pressure/dps310.c
index 8edaa4d10..99188bf4a 100644
--- a/drivers/iio/pressure/dps310.c
+++ b/drivers/iio/pressure/dps310.c
@@ -56,6 +56,8 @@
#define DPS310_RESET 0x0c
#define DPS310_RESET_MAGIC 0x09
#define DPS310_COEF_BASE 0x10
+#define PRESSURE 0
+#define TEMPERATURE 1
/* Make sure sleep time is <= 30ms for usleep_range */
#define DPS310_POLL_SLEEP_US(t) min(30000, (t) / 8)
@@ -65,6 +67,11 @@
#define DPS310_PRS_BASE DPS310_PRS_B0
#define DPS310_TMP_BASE DPS310_TMP_B0
+#define INFO_MASK_SEPARATE \
+ (BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | \
+ BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
+ BIT(IIO_CHAN_INFO_PROCESSED))
+
/*
* These values (defined in the spec) indicate how to scale the raw register
* values for each level of precision available.
@@ -95,15 +102,11 @@ struct dps310_data {
static const struct iio_chan_spec dps310_channels[] = {
{
.type = IIO_TEMP,
- .info_mask_separate = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
- BIT(IIO_CHAN_INFO_SAMP_FREQ) |
- BIT(IIO_CHAN_INFO_PROCESSED),
+ .info_mask_separate = INFO_MASK_SEPARATE
},
{
.type = IIO_PRESSURE,
- .info_mask_separate = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
- BIT(IIO_CHAN_INFO_SAMP_FREQ) |
- BIT(IIO_CHAN_INFO_PROCESSED),
+ .info_mask_separate = INFO_MASK_SEPARATE
},
};
@@ -256,57 +259,24 @@ static int dps310_startup(struct dps310_data *data)
return dps310_temp_workaround(data);
}
-static int dps310_get_pres_precision(struct dps310_data *data, int *val)
-{
- int reg_val, rc;
-
- rc = regmap_read(data->regmap, DPS310_PRS_CFG, ®_val);
- if (rc < 0)
- return rc;
-
- *val = BIT(reg_val & GENMASK(2, 0));
-
- return 0;
-}
-
-static int dps310_get_temp_precision(struct dps310_data *data, int *val)
+static int dps310_get_precision(struct dps310_data *data, int *val, int mode)
{
int reg_val, rc;
-
- rc = regmap_read(data->regmap, DPS310_TMP_CFG, ®_val);
+ if (!mode)
+ rc = regmap_read(data->regmap, DPS310_PRS_CFG, ®_val);
+ else
+ rc = regmap_read(data->regmap, DPS310_TMP_CFG, ®_val);
if (rc < 0)
return rc;
- /*
- * Scale factor is bottom 4 bits of the register, but 1111 is
- * reserved so just grab bottom three
- */
*val = BIT(reg_val & GENMASK(2, 0));
return 0;
-}
-
-/* Called with lock held */
-static int dps310_set_pres_precision(struct dps310_data *data, int val)
-{
- int rc;
- u8 shift_en;
-
- if (val < 0 || val > 128)
- return -EINVAL;
-
- shift_en = val >= 16 ? DPS310_PRS_SHIFT_EN : 0;
- rc = regmap_write_bits(data->regmap, DPS310_CFG_REG,
- DPS310_PRS_SHIFT_EN, shift_en);
- if (rc)
- return rc;
- return regmap_update_bits(data->regmap, DPS310_PRS_CFG,
- DPS310_PRS_PRC_BITS, ilog2(val));
}
/* Called with lock held */
-static int dps310_set_temp_precision(struct dps310_data *data, int val)
+static int dps310_set_precision(struct dps310_data *data, int val, int mode)
{
int rc;
u8 shift_en;
@@ -314,32 +284,29 @@ static int dps310_set_temp_precision(struct dps310_data *data, int val)
if (val < 0 || val > 128)
return -EINVAL;
- shift_en = val >= 16 ? DPS310_TMP_SHIFT_EN : 0;
- rc = regmap_write_bits(data->regmap, DPS310_CFG_REG,
- DPS310_TMP_SHIFT_EN, shift_en);
- if (rc)
- return rc;
-
- return regmap_update_bits(data->regmap, DPS310_TMP_CFG,
- DPS310_TMP_PRC_BITS, ilog2(val));
-}
-
-/* Called with lock held */
-static int dps310_set_pres_samp_freq(struct dps310_data *data, int freq)
-{
- u8 val;
-
- if (freq < 0 || freq > 128)
- return -EINVAL;
+ if (!mode) {
+ shift_en = val >= 16 ? DPS310_PRS_SHIFT_EN : 0;
+ rc = regmap_write_bits(data->regmap, DPS310_CFG_REG,
+ DPS310_PRS_SHIFT_EN, shift_en);
+ if (rc)
+ return rc;
- val = ilog2(freq) << 4;
+ return regmap_update_bits(data->regmap, DPS310_PRS_CFG,
+ DPS310_PRS_PRC_BITS, ilog2(val));
+ } else {
+ shift_en = val >= 16 ? DPS310_TMP_SHIFT_EN : 0;
+ rc = regmap_write_bits(data->regmap, DPS310_CFG_REG,
+ DPS310_TMP_SHIFT_EN, shift_en);
+ if (rc)
+ return rc;
- return regmap_update_bits(data->regmap, DPS310_PRS_CFG,
- DPS310_PRS_RATE_BITS, val);
+ return regmap_update_bits(data->regmap, DPS310_TMP_CFG,
+ DPS310_TMP_PRC_BITS, ilog2(val));
+ }
}
/* Called with lock held */
-static int dps310_set_temp_samp_freq(struct dps310_data *data, int freq)
+static int dps310_set_samp_freq(struct dps310_data *data, int freq, int mode)
{
u8 val;
@@ -348,54 +315,43 @@ static int dps310_set_temp_samp_freq(struct dps310_data *data, int freq)
val = ilog2(freq) << 4;
- return regmap_update_bits(data->regmap, DPS310_TMP_CFG,
- DPS310_TMP_RATE_BITS, val);
+ if (!mode)
+ return regmap_update_bits(data->regmap, DPS310_PRS_CFG,
+ DPS310_PRS_RATE_BITS, val);
+ else
+ return regmap_update_bits(data->regmap, DPS310_TMP_CFG,
+ DPS310_TMP_RATE_BITS, val);
}
-static int dps310_get_pres_samp_freq(struct dps310_data *data, int *val)
+static int dps310_get_samp_freq(struct dps310_data *data, int *val, int mode)
{
int reg_val, rc;
- rc = regmap_read(data->regmap, DPS310_PRS_CFG, ®_val);
- if (rc < 0)
- return rc;
-
- *val = BIT((reg_val & DPS310_PRS_RATE_BITS) >> 4);
-
- return 0;
-}
-
-static int dps310_get_temp_samp_freq(struct dps310_data *data, int *val)
-{
- int reg_val, rc;
-
- rc = regmap_read(data->regmap, DPS310_TMP_CFG, ®_val);
- if (rc < 0)
- return rc;
+ if (!mode) {
+ rc = regmap_read(data->regmap, DPS310_PRS_CFG, ®_val);
+ if (rc < 0)
+ return rc;
- *val = BIT((reg_val & DPS310_TMP_RATE_BITS) >> 4);
+ *val = BIT((reg_val & DPS310_PRS_RATE_BITS) >> 4);
+ } else {
+ rc = regmap_read(data->regmap, DPS310_TMP_CFG, ®_val);
+ if (rc < 0)
+ return rc;
+ *val = BIT((reg_val & DPS310_TMP_RATE_BITS) >> 4);
+ }
return 0;
}
-static int dps310_get_pres_k(struct dps310_data *data, int *val)
+static int dps310_get_k(struct dps310_data *data, int *val, int mode)
{
int reg_val, rc;
- rc = regmap_read(data->regmap, DPS310_PRS_CFG, ®_val);
- if (rc < 0)
- return rc;
-
- *val = scale_factors[reg_val & GENMASK(2, 0)];
-
- return 0;
-}
-
-static int dps310_get_temp_k(struct dps310_data *data, int *val)
-{
- int reg_val, rc;
+ if (!mode)
+ rc = regmap_read(data->regmap, DPS310_PRS_CFG, ®_val);
+ else
+ rc = regmap_read(data->regmap, DPS310_TMP_CFG, ®_val);
- rc = regmap_read(data->regmap, DPS310_TMP_CFG, ®_val);
if (rc < 0)
return rc;
@@ -474,7 +430,7 @@ static int dps310_read_pres_raw(struct dps310_data *data)
if (mutex_lock_interruptible(&data->lock))
return -EINTR;
- rc = dps310_get_pres_samp_freq(data, &rate);
+ rc = dps310_get_samp_freq(data, &rate, PRESSURE);
if (rc)
goto done;
@@ -523,7 +479,7 @@ static int dps310_read_temp_raw(struct dps310_data *data)
if (mutex_lock_interruptible(&data->lock))
return -EINTR;
- rc = dps310_get_temp_samp_freq(data, &rate);
+ rc = dps310_get_samp_freq(data, &rate, TEMPERATURE);
if (rc)
goto done;
@@ -590,11 +546,11 @@ static int dps310_write_raw(struct iio_dev *iio,
case IIO_CHAN_INFO_SAMP_FREQ:
switch (chan->type) {
case IIO_PRESSURE:
- rc = dps310_set_pres_samp_freq(data, val);
+ rc = dps310_set_samp_freq(data, val, PRESSURE);
break;
case IIO_TEMP:
- rc = dps310_set_temp_samp_freq(data, val);
+ rc = dps310_set_samp_freq(data, TEMPERATURE);
break;
default:
@@ -606,11 +562,11 @@ static int dps310_write_raw(struct iio_dev *iio,
case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
switch (chan->type) {
case IIO_PRESSURE:
- rc = dps310_set_pres_precision(data, val);
+ rc = dps310_set_precision(data, val, PRESSURE);
break;
case IIO_TEMP:
- rc = dps310_set_temp_precision(data, val);
+ rc = dps310_set_precision(data, val, TEMPERATURE);
break;
default:
@@ -645,11 +601,11 @@ static int dps310_calculate_pressure(struct dps310_data *data, int *val)
s64 kp;
s64 kt;
- rc = dps310_get_pres_k(data, &kpi);
+ rc = dps310_get_k(data, &kpi, PRESSURE);
if (rc)
return rc;
- rc = dps310_get_temp_k(data, &kti);
+ rc = dps310_get_k(data, &kti, TEMPERATURE);
if (rc)
return rc;
@@ -717,7 +673,7 @@ static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2,
switch (mask) {
case IIO_CHAN_INFO_SAMP_FREQ:
- rc = dps310_get_pres_samp_freq(data, val);
+ rc = dps310_get_samp_freq(data, val, PRESSURE);
if (rc)
return rc;
@@ -736,7 +692,7 @@ static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2,
return IIO_VAL_FRACTIONAL;
case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
- rc = dps310_get_pres_precision(data, val);
+ rc = dps310_get_precision(data, val, PRESSURE);
if (rc)
return rc;
return IIO_VAL_INT;
@@ -752,7 +708,7 @@ static int dps310_calculate_temp(struct dps310_data *data, int *val)
s64 t;
int kt, rc;
- rc = dps310_get_temp_k(data, &kt);
+ rc = dps310_get_k(data, &kt, TEMPERATURE);
if (rc)
return rc;
@@ -775,7 +731,7 @@ static int dps310_read_temp(struct dps310_data *data, int *val, int *val2,
switch (mask) {
case IIO_CHAN_INFO_SAMP_FREQ:
- rc = dps310_get_temp_samp_freq(data, val);
+ rc = dps310_get_samp_freq(data, val, TEMPERATURE);
if (rc)
return rc;
@@ -793,7 +749,7 @@ static int dps310_read_temp(struct dps310_data *data, int *val, int *val2,
return IIO_VAL_INT;
case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
- rc = dps310_get_temp_precision(data, val);
+ rc = dps310_get_precision(data, val, TEMPERATURE);
if (rc)
return rc;
--
2.34.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] iio: dps310.c: Remove duplication in functions
2025-07-02 23:34 [PATCH] iio: dps310.c: Remove duplication in functions Lucas Eiji Uchiyama
@ 2025-07-03 16:12 ` kernel test robot
0 siblings, 0 replies; 2+ messages in thread
From: kernel test robot @ 2025-07-03 16:12 UTC (permalink / raw)
To: Lucas Eiji Uchiyama, linux-kernel
Cc: llvm, oe-kbuild-all, Lucas Eiji Uchiyama
Hi Lucas,
kernel test robot noticed the following build errors:
[auto build test ERROR on jic23-iio/togreg]
[also build test ERROR on linus/master v6.16-rc4 next-20250703]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Lucas-Eiji-Uchiyama/iio-dps310-c-Remove-duplication-in-functions/20250703-073628
base: https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
patch link: https://lore.kernel.org/r/20250702233441.94203-1-lucaseiji54%40gmail.com
patch subject: [PATCH] iio: dps310.c: Remove duplication in functions
config: arm64-randconfig-001-20250703 (https://download.01.org/0day-ci/archive/20250703/202507032329.6gxPgmu4-lkp@intel.com/config)
compiler: clang version 21.0.0git (https://github.com/llvm/llvm-project f1a4bb62452d88a0edd9340b3ca7c9b11ad9193f)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250703/202507032329.6gxPgmu4-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202507032329.6gxPgmu4-lkp@intel.com/
All errors (new ones prefixed by >>):
>> drivers/iio/pressure/dps310.c:553:47: error: too few arguments to function call, expected 3, have 2
553 | rc = dps310_set_samp_freq(data, TEMPERATURE);
| ~~~~~~~~~~~~~~~~~~~~ ^
drivers/iio/pressure/dps310.c:309:12: note: 'dps310_set_samp_freq' declared here
309 | static int dps310_set_samp_freq(struct dps310_data *data, int freq, int mode)
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
vim +553 drivers/iio/pressure/dps310.c
534
535 static int dps310_write_raw(struct iio_dev *iio,
536 struct iio_chan_spec const *chan, int val,
537 int val2, long mask)
538 {
539 int rc;
540 struct dps310_data *data = iio_priv(iio);
541
542 if (mutex_lock_interruptible(&data->lock))
543 return -EINTR;
544
545 switch (mask) {
546 case IIO_CHAN_INFO_SAMP_FREQ:
547 switch (chan->type) {
548 case IIO_PRESSURE:
549 rc = dps310_set_samp_freq(data, val, PRESSURE);
550 break;
551
552 case IIO_TEMP:
> 553 rc = dps310_set_samp_freq(data, TEMPERATURE);
554 break;
555
556 default:
557 rc = -EINVAL;
558 break;
559 }
560 break;
561
562 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
563 switch (chan->type) {
564 case IIO_PRESSURE:
565 rc = dps310_set_precision(data, val, PRESSURE);
566 break;
567
568 case IIO_TEMP:
569 rc = dps310_set_precision(data, val, TEMPERATURE);
570 break;
571
572 default:
573 rc = -EINVAL;
574 break;
575 }
576 break;
577
578 default:
579 rc = -EINVAL;
580 break;
581 }
582
583 mutex_unlock(&data->lock);
584 return rc;
585 }
586
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-07-03 16:12 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-02 23:34 [PATCH] iio: dps310.c: Remove duplication in functions Lucas Eiji Uchiyama
2025-07-03 16:12 ` kernel test robot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).