Skip to contentSkip to conversation

staging: iio: cdc: ad7152: Implement IIO_CHAN_INFO_SAMP_FREQ attribute

PATCH Superseded sayli karnik 1 participant 2 messages Oct 1, 2016 1 message not in the archive

Subsystems (4)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: iio: cdc: ad7152: Implement IIO_CHAN_INFO_SAMP_FREQ attribute

Move functionality from IIO_DEV_ATTR_SAMP_FREQ attribute into IIO_CHAN_INFO_SAMP_FREQ. Modify ad7152_read_raw() and ad7152_write_raw() to allow reading and writing the element.

---
 drivers/staging/iio/cdc/ad7152.c | 115 ++++++++++++++++++++++-----------------
 1 file changed, 64 insertions(+), 51 deletions(-)
drivers/staging/iio/cdc/ad7152.c +64 -51
index 485d0a5..7b0f26e 100644
@@ -165,63 +165,12 @@ static const unsigned char ad7152_filter_rate_table[][2] = {
 	{200, 5 + 1}, {50, 20 + 1}, {20, 50 + 1}, {17, 60 + 1},
 };
 
-static ssize_t ad7152_show_filter_rate_setup(struct device *dev,
-		struct device_attribute *attr,
-		char *buf)
-{
-	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct ad7152_chip_info *chip = iio_priv(indio_dev);
-
-	return sprintf(buf, "%d\n",
-		       ad7152_filter_rate_table[chip->filter_rate_setup][0]);
-}
-
-static ssize_t ad7152_store_filter_rate_setup(struct device *dev,
-		struct device_attribute *attr,
-		const char *buf,
-		size_t len)
-{
-	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct ad7152_chip_info *chip = iio_priv(indio_dev);
-	u8 data;
-	int ret, i;
-
-	ret = kstrtou8(buf, 10, &data);
-	if (ret < 0)
-		return ret;
-
-	for (i = 0; i < ARRAY_SIZE(ad7152_filter_rate_table); i++)
-		if (data >= ad7152_filter_rate_table[i][0])
-			break;
-
-	if (i >= ARRAY_SIZE(ad7152_filter_rate_table))
-		i = ARRAY_SIZE(ad7152_filter_rate_table) - 1;
-
-	mutex_lock(&indio_dev->mlock);
-	ret = i2c_smbus_write_byte_data(chip->client,
-			AD7152_REG_CFG2, AD7152_CFG2_OSR(i));
-	if (ret < 0) {
-		mutex_unlock(&indio_dev->mlock);
-		return ret;
-	}
-
-	chip->filter_rate_setup = i;
-	mutex_unlock(&indio_dev->mlock);
-
-	return len;
-}
-
-static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR,
-		ad7152_show_filter_rate_setup,
-		ad7152_store_filter_rate_setup);
-
 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("200 50 20 17");
 
 static IIO_CONST_ATTR(in_capacitance_scale_available,
 		      "0.000061050 0.000030525 0.000015263 0.000007631");
 
 static struct attribute *ad7152_attributes[] = {
-	&iio_dev_attr_sampling_frequency.dev_attr.attr,
 	&iio_dev_attr_in_capacitance0_calibbias_calibration.dev_attr.attr,
 	&iio_dev_attr_in_capacitance1_calibbias_calibration.dev_attr.attr,
 	&iio_dev_attr_in_capacitance0_calibscale_calibration.dev_attr.attr,
@@ -247,6 +196,56 @@ static const int ad7152_scale_table[] = {
 	30525, 7631, 15263, 61050
 };
 
+/**
+ * read_raw handler for IIO_CHAN_INFO_SAMP_FREQ
+ *
+ * lock must be held
+ **/
+static int ad7152_read_raw_samp_freq(struct device *dev, int *val)
+{
+	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+	struct ad7152_chip_info *chip = iio_priv(indio_dev);
+	*val = ad7152_filter_rate_table[chip->filter_rate_setup][0];
+
+	return 0;
+}
+
+/**
+ * write_raw handler for IIO_CHAN_INFO_SAMP_FREQ
+ *
+ * lock must be held
+ **/
+static int ad7152_write_raw_samp_freq(struct device *dev, int val)
+{
+	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+	struct ad7152_chip_info *chip = iio_priv(indio_dev);
+	u8 data;
+	int ret, i;
+
+	if (!val)
+		return -EINVAL;
+
+	for (i = 0; i < ARRAY_SIZE(ad7152_filter_rate_table); i++)
+		if (data >= ad7152_filter_rate_table[i][0])
+			break;
+
+	if (i >= ARRAY_SIZE(ad7152_filter_rate_table))
+		i = ARRAY_SIZE(ad7152_filter_rate_table) - 1;
+
+	mutex_lock(&indio_dev->mlock);
+	ret = i2c_smbus_write_byte_data(chip->client,
+					AD7152_REG_CFG2, AD7152_CFG2_OSR(i));
+	if (ret < 0) {
+		mutex_unlock(&indio_dev->mlock);
+		goto out;
+	}
+
+	chip->filter_rate_setup = i;
+	mutex_unlock(&indio_dev->mlock);
+
+out:
+	return ret;
+}
 static int ad7152_write_raw(struct iio_dev *indio_dev,
 			    struct iio_chan_spec const *chan,
 			    int val,
@@ -309,6 +308,11 @@ static int ad7152_write_raw(struct iio_dev *indio_dev,
 
 		ret = 0;
 		break;
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		ret = ad7152_write_raw_samp_freq(&indio_dev->dev, val);
+		if (ret < 0)
+			goto out;
+		break;
 	default:
 		ret = -EINVAL;
 	}
@@ -403,6 +407,11 @@ static int ad7152_read_raw(struct iio_dev *indio_dev,
 
 		ret = IIO_VAL_INT_PLUS_NANO;
 		break;
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		ret = ad7152_read_raw_samp_freq(&indio_dev->dev, val);
+		if (ret < 0)
+			goto out;
+		break;
 	default:
 		ret = -EINVAL;
 	}
@@ -440,6 +449,7 @@ static const struct iio_chan_spec ad7152_channels[] = {
 		BIT(IIO_CHAN_INFO_CALIBSCALE) |
 		BIT(IIO_CHAN_INFO_CALIBBIAS) |
 		BIT(IIO_CHAN_INFO_SCALE),
+		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
 	}, {
 		.type = IIO_CAPACITANCE,
 		.differential = 1,
@@ -450,6 +460,7 @@ static const struct iio_chan_spec ad7152_channels[] = {
 		BIT(IIO_CHAN_INFO_CALIBSCALE) |
 		BIT(IIO_CHAN_INFO_CALIBBIAS) |
 		BIT(IIO_CHAN_INFO_SCALE),
+		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
 	}, {
 		.type = IIO_CAPACITANCE,
 		.indexed = 1,
@@ -458,6 +469,7 @@ static const struct iio_chan_spec ad7152_channels[] = {
 		BIT(IIO_CHAN_INFO_CALIBSCALE) |
 		BIT(IIO_CHAN_INFO_CALIBBIAS) |
 		BIT(IIO_CHAN_INFO_SCALE),
+		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
 	}, {
 		.type = IIO_CAPACITANCE,
 		.differential = 1,
@@ -468,6 +480,7 @@ static const struct iio_chan_spec ad7152_channels[] = {
 		BIT(IIO_CHAN_INFO_CALIBSCALE) |
 		BIT(IIO_CHAN_INFO_CALIBBIAS) |
 		BIT(IIO_CHAN_INFO_SCALE),
+		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
 	}
 };
 /*
Signature · 1 line
2.7.4

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 190 lines · Jonathan Cameron
Quoted 3 lines · sayli karnik

Move functionality from IIO_DEV_ATTR_SAMP_FREQ attribute into IIO_CHAN_INFO_SAMP_FREQ. Modify ad7152_read_raw() and ad7152_write_raw() to allow reading and writing the element.

Hi

Ideally would have had a note to give the 'why' element of what the change is (Lars just raised this point in a similar patch).

Quoted 2 lines

Take a look at ad7152_write_raw_get_fmt which could be extended to explicitly pick the value up as an integer. If not you should check val2 is 0 in the write function.

A few bits and bobs inline + a suggestion for a follow up patch if you want to continue working on this driver (which is has been somewhat neglected for a long time! Not dropping any hints Lars ;))

Thanks,

Jonathan

Quoted 75 lines
---
 drivers/staging/iio/cdc/ad7152.c | 115 ++++++++++++++++++++++-----------------
 1 file changed, 64 insertions(+), 51 deletions(-)
drivers/staging/iio/cdc/ad7152.c +0 -51
index 485d0a5..7b0f26e 100644
@@ -165,63 +165,12 @@ static const unsigned char ad7152_filter_rate_table[][2] = {
      {200, 5 + 1}, {50, 20 + 1}, {20, 50 + 1}, {17, 60 + 1},
 };
 
-static ssize_t ad7152_show_filter_rate_setup(struct device *dev,
-             struct device_attribute *attr,
-             char *buf)
-{
-     struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-     struct ad7152_chip_info *chip = iio_priv(indio_dev);
-
-     return sprintf(buf, "%d\n",
-                    ad7152_filter_rate_table[chip->filter_rate_setup][0]);
-}
-
-static ssize_t ad7152_store_filter_rate_setup(struct device *dev,
-             struct device_attribute *attr,
-             const char *buf,
-             size_t len)
-{
-     struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-     struct ad7152_chip_info *chip = iio_priv(indio_dev);
-     u8 data;
-     int ret, i;
-
-     ret = kstrtou8(buf, 10, &data);
-     if (ret < 0)
-             return ret;
-
-     for (i = 0; i < ARRAY_SIZE(ad7152_filter_rate_table); i++)
-             if (data >= ad7152_filter_rate_table[i][0])
-                     break;
-
-     if (i >= ARRAY_SIZE(ad7152_filter_rate_table))
-             i = ARRAY_SIZE(ad7152_filter_rate_table) - 1;
-
-     mutex_lock(&indio_dev->mlock);
-     ret = i2c_smbus_write_byte_data(chip->client,
-                     AD7152_REG_CFG2, AD7152_CFG2_OSR(i));
-     if (ret < 0) {
-             mutex_unlock(&indio_dev->mlock);
-             return ret;
-     }
-
-     chip->filter_rate_setup = i;
-     mutex_unlock(&indio_dev->mlock);
-
-     return len;
-}
-
-static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR,
-             ad7152_show_filter_rate_setup,
-             ad7152_store_filter_rate_setup);
-
 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("200 50 20 17");
 
 static IIO_CONST_ATTR(in_capacitance_scale_available,
                    "0.000061050 0.000030525 0.000015263 0.000007631");
 
 static struct attribute *ad7152_attributes[] = {
-     &iio_dev_attr_sampling_frequency.dev_attr.attr,
      &iio_dev_attr_in_capacitance0_calibbias_calibration.dev_attr.attr,
      &iio_dev_attr_in_capacitance1_calibbias_calibration.dev_attr.attr,
      &iio_dev_attr_in_capacitance0_calibscale_calibration.dev_attr.attr,
@@ -247,6 +196,56 @@ static const int ad7152_scale_table[] = {
      30525, 7631, 15263, 61050
 };

drivers/staging/iio/cdc/ad7152.c static const int ad7152_scale_table[] = { · line 207

Quoted 10 lines
drivers/staging/iio/cdc/ad7152.c +9 -0
 
+/**
+ * read_raw handler for IIO_CHAN_INFO_SAMP_FREQ
+ *
+ * lock must be held
+ **/
+static int ad7152_read_raw_samp_freq(struct device *dev, int *val)
+{
+     struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+     struct ad7152_chip_info *chip = iio_priv(indio_dev);

Could have daisychained the above without lost of meaning. e.g struct ad7152_chip_info *chip = iio_priv(dev_to_iio_dev(dev)); Clearly original code didn't do this but nice to make little tidy ups on the way past!

Also should be a blank line here.

Quoted 9 lines
drivers/staging/iio/cdc/ad7152.c +9 -0
+     *val = ad7152_filter_rate_table[chip->filter_rate_setup][0];
+
+     return 0;
+}
+
+/**
+ * write_raw handler for IIO_CHAN_INFO_SAMP_FREQ
+ *
+ * lock must be held

drivers/staging/iio/cdc/ad7152.c static const int ad7152_scale_table[] = { · line 226

Quoted 10 lines
drivers/staging/iio/cdc/ad7152.c +10 -0
+ **/
+static int ad7152_write_raw_samp_freq(struct device *dev, int val)
+{
+     struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+     struct ad7152_chip_info *chip = iio_priv(indio_dev);
+     u8 data;
+     int ret, i;
+
+     if (!val)
+             return -EINVAL;

Why? Admittedly a value of 0 seems unlikely to make much sense, but the code doesn't as such require us to prevent it with it's own check.

if (val2)
return -EINVAL; is more useful though as we want to enforce an integer value.

Quoted 7 lines
drivers/staging/iio/cdc/ad7152.c +7 -0
+
+     for (i = 0; i < ARRAY_SIZE(ad7152_filter_rate_table); i++)
+             if (data >= ad7152_filter_rate_table[i][0])
+                     break;
+
+     if (i >= ARRAY_SIZE(ad7152_filter_rate_table))
+             i = ARRAY_SIZE(ad7152_filter_rate_table) - 1;

drivers/staging/iio/cdc/ad7152.c static const int ad7152_scale_table[] = { · line 243

Quoted 10 lines
drivers/staging/iio/cdc/ad7152.c +10 -0
+
+     mutex_lock(&indio_dev->mlock);
+     ret = i2c_smbus_write_byte_data(chip->client,
+                                     AD7152_REG_CFG2, AD7152_CFG2_OSR(i));
+     if (ret < 0) {
+             mutex_unlock(&indio_dev->mlock);
+             goto out;
+     }
+
+     chip->filter_rate_setup = i;

The original code is rather inelegant in how it handles mlock.

mlock is there for one purpose only, to protect mode state changes (moving between sysfs read modes for data and pushing out on triggers via the 'buffered' interface).

This should really be done with a local lock in the iio_priv structure.

However, different cleanup so should not be in this patch. (i.e. Leave it alone for this patch, but if you want to carry on cleaning up this driver it would be a good next step)

Quoted 22 lines
drivers/staging/iio/cdc/ad7152.c +10 -0
+     mutex_unlock(&indio_dev->mlock);
+
+out:
+     return ret;
+}
 static int ad7152_write_raw(struct iio_dev *indio_dev,
                          struct iio_chan_spec const *chan,
                          int val,
@@ -309,6 +308,11 @@ static int ad7152_write_raw(struct iio_dev *indio_dev,
 
              ret = 0;
              break;
+     case IIO_CHAN_INFO_SAMP_FREQ:
+             ret = ad7152_write_raw_samp_freq(&indio_dev->dev, val);
+             if (ret < 0)
+                     goto out;
+             break;
      default:
              ret = -EINVAL;
      }
@@ -403,6 +407,11 @@ static int ad7152_read_raw(struct iio_dev *indio_dev,
 
Quoted 8 lines

drivers/staging/iio/cdc/ad7152.c static int ad7152_read_raw(struct iio_dev *indio_dev, · line 413

Quoted 6 lines
drivers/staging/iio/cdc/ad7152.c +4 -0
              ret = IIO_VAL_INT_PLUS_NANO;
              break;
+     case IIO_CHAN_INFO_SAMP_FREQ:
+             ret = ad7152_read_raw_samp_freq(&indio_dev->dev, val);
+             if (ret < 0)
+                     goto out;

Look at what the return value is used for.. Needs to be set here.

Thanks for the feedback.
I am sending in a patch v2 making all the changes you mentioned. 'ret' here should be set to IIO_VAL_INT right?

thanks
sayli

Quoted 38 lines
Quoted 37 lines
drivers/staging/iio/cdc/ad7152.c +5 -0
+             break;
      default:
              ret = -EINVAL;
      }
@@ -440,6 +449,7 @@ static const struct iio_chan_spec ad7152_channels[] = {
              BIT(IIO_CHAN_INFO_CALIBSCALE) |
              BIT(IIO_CHAN_INFO_CALIBBIAS) |
              BIT(IIO_CHAN_INFO_SCALE),
+             .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
      }, {
              .type = IIO_CAPACITANCE,
              .differential = 1,
@@ -450,6 +460,7 @@ static const struct iio_chan_spec ad7152_channels[] = {
              BIT(IIO_CHAN_INFO_CALIBSCALE) |
              BIT(IIO_CHAN_INFO_CALIBBIAS) |
              BIT(IIO_CHAN_INFO_SCALE),
+             .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
      }, {
              .type = IIO_CAPACITANCE,
              .indexed = 1,
@@ -458,6 +469,7 @@ static const struct iio_chan_spec ad7152_channels[] = {
              BIT(IIO_CHAN_INFO_CALIBSCALE) |
              BIT(IIO_CHAN_INFO_CALIBBIAS) |
              BIT(IIO_CHAN_INFO_SCALE),
+             .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
      }, {
              .type = IIO_CAPACITANCE,
              .differential = 1,
@@ -468,6 +480,7 @@ static const struct iio_chan_spec ad7152_channels[] = {
              BIT(IIO_CHAN_INFO_CALIBSCALE) |
              BIT(IIO_CHAN_INFO_CALIBBIAS) |
              BIT(IIO_CHAN_INFO_SCALE),
+             .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
      }
 };
 /*