diff mbox series

[3/6] iio:pressure:ms5637: limit available sample frequencies

Message ID 20201209234857.1521453-4-alexandre.belloni@bootlin.com (mailing list archive)
State New, archived
Headers show
Series iio:pressure:ms5637: add ms5803 support | expand

Commit Message

Alexandre Belloni Dec. 9, 2020, 11:48 p.m. UTC
Avoid exposing all the sampling frequencies for chip that only support a
subset.

Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
---
 drivers/iio/pressure/ms5637.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

Comments

Andy Shevchenko Dec. 12, 2020, 6:26 p.m. UTC | #1
On Thu, Dec 10, 2020 at 2:03 AM Alexandre Belloni
<alexandre.belloni@bootlin.com> wrote:
>
> Avoid exposing all the sampling frequencies for chip that only support a
> subset.

> +static ssize_t ms5637_show_samp_freq(struct device *dev, struct device_attribute *attr, char *buf)
> +{
> +       struct iio_dev *indio_dev = dev_to_iio_dev(dev);
> +       struct ms_tp_dev *dev_data = iio_priv(indio_dev);
> +       int i, len = 0;
> +
> +       for (i = 0; i <= dev_data->hw->max_res_index; i++)
> +               len += scnprintf(buf + len, PAGE_SIZE - len, "%u ", ms5637_samp_freq[i]);

Doesn't IIO core have a helper?
Also, it's better to use sysfs_emit().

> +       buf[len - 1] = '\n';
> +
> +       return len;
> +}
Jonathan Cameron Dec. 13, 2020, 5:17 p.m. UTC | #2
On Sat, 12 Dec 2020 20:26:16 +0200
Andy Shevchenko <andy.shevchenko@gmail.com> wrote:

> On Thu, Dec 10, 2020 at 2:03 AM Alexandre Belloni
> <alexandre.belloni@bootlin.com> wrote:
> >
> > Avoid exposing all the sampling frequencies for chip that only support a
> > subset.  
> 
> > +static ssize_t ms5637_show_samp_freq(struct device *dev, struct device_attribute *attr, char *buf)
> > +{
> > +       struct iio_dev *indio_dev = dev_to_iio_dev(dev);
> > +       struct ms_tp_dev *dev_data = iio_priv(indio_dev);
> > +       int i, len = 0;
> > +
> > +       for (i = 0; i <= dev_data->hw->max_res_index; i++)
> > +               len += scnprintf(buf + len, PAGE_SIZE - len, "%u ", ms5637_samp_freq[i]);  
> 
> Doesn't IIO core have a helper?
read_avail() callback and matching masks provide the infrastructure to do this.
It's not a huge saving in code by the time you've wired it up, but has the
advantage that consumer drivers can get hold of the values.  Mind you
I'm not sure what consumers we are likely to get for pressure drivers any
time soon.

> Also, it's better to use sysfs_emit().

New one to me. Thanks.  sysfs_emit_at() here I guess.

Nice.

Jonathan

> 
> > +       buf[len - 1] = '\n';
> > +
> > +       return len;
> > +}  
>
diff mbox series

Patch

diff --git a/drivers/iio/pressure/ms5637.c b/drivers/iio/pressure/ms5637.c
index 351bfdb24fb4..2943b88734b3 100644
--- a/drivers/iio/pressure/ms5637.c
+++ b/drivers/iio/pressure/ms5637.c
@@ -36,8 +36,19 @@  struct ms_tp_data {
 };
 
 static const int ms5637_samp_freq[6] = { 960, 480, 240, 120, 60, 30 };
-/* String copy of the above const for readability purpose */
-static const char ms5637_show_samp_freq[] = "960 480 240 120 60 30";
+
+static ssize_t ms5637_show_samp_freq(struct device *dev, struct device_attribute *attr, char *buf)
+{
+	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+	struct ms_tp_dev *dev_data = iio_priv(indio_dev);
+	int i, len = 0;
+
+	for (i = 0; i <= dev_data->hw->max_res_index; i++)
+		len += scnprintf(buf + len, PAGE_SIZE - len, "%u ", ms5637_samp_freq[i]);
+	buf[len - 1] = '\n';
+
+	return len;
+}
 
 static int ms5637_read_raw(struct iio_dev *indio_dev,
 			   struct iio_chan_spec const *channel, int *val,
@@ -114,10 +125,10 @@  static const struct iio_chan_spec ms5637_channels[] = {
 	}
 };
 
-static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(ms5637_show_samp_freq);
+static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(ms5637_show_samp_freq);
 
 static struct attribute *ms5637_attributes[] = {
-	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
+	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
 	NULL,
 };