[PATCH v2 4/4] iio: mpl3115: add support for sampling frequency

Antoni Pokusinski posted 4 patches 6 days, 2 hours ago
There is a newer version of this series
[PATCH v2 4/4] iio: mpl3115: add support for sampling frequency
Posted by Antoni Pokusinski 6 days, 2 hours ago
When the device is in ACTIVE mode the temperature and pressure measurements
are collected with a frequency determined by the ST[3:0] bits of CTRL_REG2
register.

Reviewed-by: Nuno Sá <nuno.sa@analog.com>
Signed-off-by: Antoni Pokusinski <apokusinski01@gmail.com>
---
 drivers/iio/pressure/mpl3115.c | 81 ++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)

diff --git a/drivers/iio/pressure/mpl3115.c b/drivers/iio/pressure/mpl3115.c
index 13c8b338a15e..b854732e61cb 100644
--- a/drivers/iio/pressure/mpl3115.c
+++ b/drivers/iio/pressure/mpl3115.c
@@ -30,6 +30,7 @@
 #define MPL3115_INT_SOURCE 0x12
 #define MPL3115_PT_DATA_CFG 0x13
 #define MPL3115_CTRL_REG1 0x26
+#define MPL3115_CTRL_REG2 0x27
 #define MPL3115_CTRL_REG3 0x28
 #define MPL3115_CTRL_REG4 0x29
 #define MPL3115_CTRL_REG5 0x2a
@@ -48,6 +49,8 @@
 #define MPL3115_CTRL1_ACTIVE BIT(0) /* continuous measurement */
 #define MPL3115_CTRL1_OS_258MS GENMASK(5, 4) /* 64x oversampling */
 
+#define MPL3115_CTRL2_ST GENMASK(3, 0)
+
 #define MPL3115_CTRL3_IPOL1 BIT(5)
 #define MPL3115_CTRL3_IPOL2 BIT(1)
 
@@ -57,6 +60,25 @@
 
 #define MPL3115_INT2 BIT(2) /* flag that indicates INT2 in use */
 
+static const unsigned int mpl3115_samp_freq_table[][2] = {
+	{ 1,      0},
+	{ 0, 500000},
+	{ 0, 250000},
+	{ 0, 125000},
+	{ 0,  62500},
+	{ 0,  31250},
+	{ 0,  15625},
+	{ 0,   7812},
+	{ 0,   3906},
+	{ 0,   1953},
+	{ 0,    976},
+	{ 0,    488},
+	{ 0,    244},
+	{ 0,    122},
+	{ 0,     61},
+	{ 0,     30},
+};
+
 struct mpl3115_data {
 	struct i2c_client *client;
 	struct iio_trigger *drdy_trig;
@@ -174,10 +196,61 @@ static int mpl3115_read_raw(struct iio_dev *indio_dev,
 		default:
 			return -EINVAL;
 		}
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		ret = i2c_smbus_read_byte_data(data->client, MPL3115_CTRL_REG2);
+		if (ret < 0)
+			return ret;
+
+		ret = FIELD_GET(MPL3115_CTRL2_ST, ret);
+
+		*val = mpl3115_samp_freq_table[ret][0];
+		*val2 = mpl3115_samp_freq_table[ret][1];
+		return IIO_VAL_INT_PLUS_MICRO;
 	}
 	return -EINVAL;
 }
 
+static int mpl3115_read_avail(struct iio_dev *indio_dev,
+			      struct iio_chan_spec const *chan,
+			      const int **vals, int *type, int *length,
+			      long mask)
+{
+	if (mask != IIO_CHAN_INFO_SAMP_FREQ)
+		return -EINVAL;
+
+	*type = IIO_VAL_INT_PLUS_MICRO;
+	*length = ARRAY_SIZE(mpl3115_samp_freq_table) * 2;
+	*vals = (int *)mpl3115_samp_freq_table;
+	return IIO_AVAIL_LIST;
+}
+
+static int mpl3115_write_raw(struct iio_dev *indio_dev,
+			     const struct iio_chan_spec *chan,
+			     int val, int val2, long mask)
+{
+	struct mpl3115_data *data = iio_priv(indio_dev);
+	int i, ret;
+
+	if (mask != IIO_CHAN_INFO_SAMP_FREQ)
+		return -EINVAL;
+
+	for (i = 0; i < ARRAY_SIZE(mpl3115_samp_freq_table); i++)
+		if (val == mpl3115_samp_freq_table[i][0] &&
+		    val2 == mpl3115_samp_freq_table[i][1])
+			break;
+
+	if (i == ARRAY_SIZE(mpl3115_samp_freq_table))
+		return -EINVAL;
+
+	if (!iio_device_claim_direct(indio_dev))
+		return -EBUSY;
+
+	ret = i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG2,
+					FIELD_PREP(MPL3115_CTRL2_ST, i));
+	iio_device_release_direct(indio_dev);
+	return ret;
+}
+
 static irqreturn_t mpl3115_trigger_handler(int irq, void *p)
 {
 	struct iio_poll_func *pf = p;
@@ -229,6 +302,9 @@ static const struct iio_chan_spec mpl3115_channels[] = {
 		.type = IIO_PRESSURE,
 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
+		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
+		.info_mask_shared_by_all_available =
+			BIT(IIO_CHAN_INFO_SAMP_FREQ),
 		.scan_index = 0,
 		.scan_type = {
 			.sign = 'u',
@@ -242,6 +318,9 @@ static const struct iio_chan_spec mpl3115_channels[] = {
 		.type = IIO_TEMP,
 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
+		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
+		.info_mask_shared_by_all_available =
+			BIT(IIO_CHAN_INFO_SAMP_FREQ),
 		.scan_index = 1,
 		.scan_type = {
 			.sign = 's',
@@ -312,6 +391,8 @@ static const struct iio_trigger_ops mpl3115_trigger_ops = {
 
 static const struct iio_info mpl3115_info = {
 	.read_raw = &mpl3115_read_raw,
+	.read_avail = &mpl3115_read_avail,
+	.write_raw = &mpl3115_write_raw,
 };
 
 static int mpl3115_trigger_probe(struct mpl3115_data *data,
-- 
2.25.1

Re: [PATCH v2 4/4] iio: mpl3115: add support for sampling frequency
Posted by Antoni Pokusinski 5 days, 8 hours ago
On Thu, Sep 25, 2025 at 10:45:38PM +0200, Antoni Pokusinski wrote:
> When the device is in ACTIVE mode the temperature and pressure measurements
> are collected with a frequency determined by the ST[3:0] bits of CTRL_REG2
> register.
> 
> Reviewed-by: Nuno Sá <nuno.sa@analog.com>
> Signed-off-by: Antoni Pokusinski <apokusinski01@gmail.com>
> ---
>  drivers/iio/pressure/mpl3115.c | 81 ++++++++++++++++++++++++++++++++++
>  1 file changed, 81 insertions(+)
> 
> diff --git a/drivers/iio/pressure/mpl3115.c b/drivers/iio/pressure/mpl3115.c
> index 13c8b338a15e..b854732e61cb 100644
> --- a/drivers/iio/pressure/mpl3115.c
> +++ b/drivers/iio/pressure/mpl3115.c
> @@ -30,6 +30,7 @@

The errors are due to missing include of bitfield.h, will add it in v3

>  #define MPL3115_INT_SOURCE 0x12
>  #define MPL3115_PT_DATA_CFG 0x13
>  #define MPL3115_CTRL_REG1 0x26
> +#define MPL3115_CTRL_REG2 0x27
>  #define MPL3115_CTRL_REG3 0x28
>  #define MPL3115_CTRL_REG4 0x29
>  #define MPL3115_CTRL_REG5 0x2a
> @@ -48,6 +49,8 @@
> 2.25.1
> 
Re: [PATCH v2 4/4] iio: mpl3115: add support for sampling frequency
Posted by kernel test robot 5 days, 10 hours ago
Hi Antoni,

kernel test robot noticed the following build errors:

[auto build test ERROR on jic23-iio/togreg]
[also build test ERROR on robh/for-next linus/master v6.17-rc7 next-20250925]
[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/Antoni-Pokusinski/dt-bindings-iio-pressure-add-binding-for-mpl3115/20250926-044905
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
patch link:    https://lore.kernel.org/r/20250925204538.63723-5-apokusinski01%40gmail.com
patch subject: [PATCH v2 4/4] iio: mpl3115: add support for sampling frequency
config: i386-buildonly-randconfig-001-20250926 (https://download.01.org/0day-ci/archive/20250926/202509262005.y59poUS9-lkp@intel.com/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250926/202509262005.y59poUS9-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/202509262005.y59poUS9-lkp@intel.com/

All errors (new ones prefixed by >>):

>> drivers/iio/pressure/mpl3115.c:204:9: error: call to undeclared function 'FIELD_GET'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     204 |                 ret = FIELD_GET(MPL3115_CTRL2_ST, ret);
         |                       ^
>> drivers/iio/pressure/mpl3115.c:249:6: error: call to undeclared function 'FIELD_PREP'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     249 |                                         FIELD_PREP(MPL3115_CTRL2_ST, i));
         |                                         ^
   2 errors generated.


vim +/FIELD_GET +204 drivers/iio/pressure/mpl3115.c

   169	
   170	static int mpl3115_read_raw(struct iio_dev *indio_dev,
   171				    struct iio_chan_spec const *chan,
   172				    int *val, int *val2, long mask)
   173	{
   174		struct mpl3115_data *data = iio_priv(indio_dev);
   175		int ret;
   176	
   177		switch (mask) {
   178		case IIO_CHAN_INFO_RAW:
   179			if (!iio_device_claim_direct(indio_dev))
   180				return -EBUSY;
   181	
   182			ret = mpl3115_read_info_raw(data, chan, val);
   183			iio_device_release_direct(indio_dev);
   184			return ret;
   185	
   186		case IIO_CHAN_INFO_SCALE:
   187			switch (chan->type) {
   188			case IIO_PRESSURE:
   189				*val = 0;
   190				*val2 = 250; /* want kilopascal */
   191				return IIO_VAL_INT_PLUS_MICRO;
   192			case IIO_TEMP:
   193				*val = 0;
   194				*val2 = 62500;
   195				return IIO_VAL_INT_PLUS_MICRO;
   196			default:
   197				return -EINVAL;
   198			}
   199		case IIO_CHAN_INFO_SAMP_FREQ:
   200			ret = i2c_smbus_read_byte_data(data->client, MPL3115_CTRL_REG2);
   201			if (ret < 0)
   202				return ret;
   203	
 > 204			ret = FIELD_GET(MPL3115_CTRL2_ST, ret);
   205	
   206			*val = mpl3115_samp_freq_table[ret][0];
   207			*val2 = mpl3115_samp_freq_table[ret][1];
   208			return IIO_VAL_INT_PLUS_MICRO;
   209		}
   210		return -EINVAL;
   211	}
   212	
   213	static int mpl3115_read_avail(struct iio_dev *indio_dev,
   214				      struct iio_chan_spec const *chan,
   215				      const int **vals, int *type, int *length,
   216				      long mask)
   217	{
   218		if (mask != IIO_CHAN_INFO_SAMP_FREQ)
   219			return -EINVAL;
   220	
   221		*type = IIO_VAL_INT_PLUS_MICRO;
   222		*length = ARRAY_SIZE(mpl3115_samp_freq_table) * 2;
   223		*vals = (int *)mpl3115_samp_freq_table;
   224		return IIO_AVAIL_LIST;
   225	}
   226	
   227	static int mpl3115_write_raw(struct iio_dev *indio_dev,
   228				     const struct iio_chan_spec *chan,
   229				     int val, int val2, long mask)
   230	{
   231		struct mpl3115_data *data = iio_priv(indio_dev);
   232		int i, ret;
   233	
   234		if (mask != IIO_CHAN_INFO_SAMP_FREQ)
   235			return -EINVAL;
   236	
   237		for (i = 0; i < ARRAY_SIZE(mpl3115_samp_freq_table); i++)
   238			if (val == mpl3115_samp_freq_table[i][0] &&
   239			    val2 == mpl3115_samp_freq_table[i][1])
   240				break;
   241	
   242		if (i == ARRAY_SIZE(mpl3115_samp_freq_table))
   243			return -EINVAL;
   244	
   245		if (!iio_device_claim_direct(indio_dev))
   246			return -EBUSY;
   247	
   248		ret = i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG2,
 > 249						FIELD_PREP(MPL3115_CTRL2_ST, i));
   250		iio_device_release_direct(indio_dev);
   251		return ret;
   252	}
   253	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki