diff mbox series

[v1] meson saradc: add iio device attrib to switch channel 7 mux

Message ID 20230524000111.14370-1-gnstark@sberdevices.ru (mailing list archive)
State New, archived
Headers show
Series [v1] meson saradc: add iio device attrib to switch channel 7 mux | expand

Commit Message

George Stark May 24, 2023, 12:01 a.m. UTC
Patch adds two sysfs nodes: chan7_mux to set mux state
and chan7_mux_available to show available mux states.
Mux can be used to debug and calibrate adc by
switching and measuring well-known inputs like gnd, vdd etc.

Signed-off-by: George Stark <GNStark@sberdevices.ru>
---
 drivers/iio/adc/meson_saradc.c | 66 ++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)

Comments

Andy Shevchenko May 27, 2023, 8:21 a.m. UTC | #1
On Wed, May 24, 2023 at 03:01:11AM +0300, George Stark wrote:
> Patch adds two sysfs nodes: chan7_mux to set mux state
> and chan7_mux_available to show available mux states.
> Mux can be used to debug and calibrate adc by
> switching and measuring well-known inputs like gnd, vdd etc.

GND
Vdd

...

> +static ssize_t chan7_mux_store(struct device *dev,
> +			       struct device_attribute *attr,
> +			       const char *buf, size_t count)
> +{
> +	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
> +	int i;
> +
> +	for (i = 0; i < ARRAY_SIZE(chan7_vol); i++)
> +		if (!strcmp(chan7_vol[i], buf)) {
> +			meson_sar_adc_set_chan7_mux(indio_dev, i);
> +			return count;
> +		}
> +
> +	return -EINVAL;

NIH sysfs_match_string().

> +}

...

> +static IIO_DEVICE_ATTR_RW(chan7_mux, -1);
> +static IIO_DEVICE_ATTR_RO(chan7_mux_available, -1);

Place each of them near to the respective callback(s),

...

> +static struct attribute *meson_sar_adc_attrs[] = {
> +	&iio_dev_attr_chan7_mux_available.dev_attr.attr,
> +	&iio_dev_attr_chan7_mux.dev_attr.attr,
> +	NULL,

No comma for the terminator entry.

> +};
George Stark May 27, 2023, 9:37 p.m. UTC | #2
Hi Andy

Thanks for review. I fixed it in patch v2

Best regards
George


On 5/27/23 11:21, Andy Shevchenko wrote:
> On Wed, May 24, 2023 at 03:01:11AM +0300, George Stark wrote:
>> Patch adds two sysfs nodes: chan7_mux to set mux state
>> and chan7_mux_available to show available mux states.
>> Mux can be used to debug and calibrate adc by
>> switching and measuring well-known inputs like gnd, vdd etc.
> GND
> Vdd
>
> ...
>
>> +static ssize_t chan7_mux_store(struct device *dev,
>> +			       struct device_attribute *attr,
>> +			       const char *buf, size_t count)
>> +{
>> +	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
>> +	int i;
>> +
>> +	for (i = 0; i < ARRAY_SIZE(chan7_vol); i++)
>> +		if (!strcmp(chan7_vol[i], buf)) {
>> +			meson_sar_adc_set_chan7_mux(indio_dev, i);
>> +			return count;
>> +		}
>> +
>> +	return -EINVAL;
> NIH sysfs_match_string().
>
>> +}
> ...
>
>> +static IIO_DEVICE_ATTR_RW(chan7_mux, -1);
>> +static IIO_DEVICE_ATTR_RO(chan7_mux_available, -1);
> Place each of them near to the respective callback(s),
>
> ...
>
>> +static struct attribute *meson_sar_adc_attrs[] = {
>> +	&iio_dev_attr_chan7_mux_available.dev_attr.attr,
>> +	&iio_dev_attr_chan7_mux.dev_attr.attr,
>> +	NULL,
> No comma for the terminator entry.
>
>> +};
diff mbox series

Patch

diff --git a/drivers/iio/adc/meson_saradc.c b/drivers/iio/adc/meson_saradc.c
index e05e51900c35..77ef2b55f6f4 100644
--- a/drivers/iio/adc/meson_saradc.c
+++ b/drivers/iio/adc/meson_saradc.c
@@ -11,6 +11,7 @@ 
 #include <linux/delay.h>
 #include <linux/io.h>
 #include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/nvmem-consumer.h>
@@ -320,6 +321,7 @@  struct meson_sar_adc_priv {
 	bool					temperature_sensor_calibrated;
 	u8					temperature_sensor_coefficient;
 	u16					temperature_sensor_adc_val;
+	u8					chan7_mux_sel;
 };
 
 static const struct regmap_config meson_sar_adc_regmap_config_gxbb = {
@@ -483,6 +485,7 @@  static void meson_sar_adc_set_chan7_mux(struct iio_dev *indio_dev,
 	regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
 			   MESON_SAR_ADC_REG3_CTRL_CHAN7_MUX_SEL_MASK, regval);
 
+	priv->chan7_mux_sel = sel;
 	usleep_range(10, 20);
 }
 
@@ -1130,8 +1133,71 @@  static int meson_sar_adc_calib(struct iio_dev *indio_dev)
 	return ret;
 }
 
+static const char * const chan7_vol[] = {
+	"gnd",
+	"vdd/4",
+	"vdd/2",
+	"vdd*3/4",
+	"vdd",
+	"ch7_input",
+};
+
+static ssize_t chan7_mux_show(struct device *dev, struct device_attribute *attr,
+			      char *buf)
+{
+	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+	struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
+	unsigned int index = priv->chan7_mux_sel;
+
+	if (index >= ARRAY_SIZE(chan7_vol))
+		index = ARRAY_SIZE(chan7_vol) - 1;
+
+	return sysfs_emit(buf, "%s\n", chan7_vol[index]);
+}
+
+static ssize_t chan7_mux_store(struct device *dev,
+			       struct device_attribute *attr,
+			       const char *buf, size_t count)
+{
+	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(chan7_vol); i++)
+		if (!strcmp(chan7_vol[i], buf)) {
+			meson_sar_adc_set_chan7_mux(indio_dev, i);
+			return count;
+		}
+
+	return -EINVAL;
+}
+
+static ssize_t chan7_mux_available_show(struct device *dev, struct device_attribute *attr,
+			      char *buf)
+{
+	int i, len = 0;
+
+	for (i = 0; i < ARRAY_SIZE(chan7_vol); i++)
+		len += sysfs_emit_at(buf, len, "%s ", chan7_vol[i]);
+
+	return len;
+}
+
+static IIO_DEVICE_ATTR_RW(chan7_mux, -1);
+static IIO_DEVICE_ATTR_RO(chan7_mux_available, -1);
+
+static struct attribute *meson_sar_adc_attrs[] = {
+	&iio_dev_attr_chan7_mux_available.dev_attr.attr,
+	&iio_dev_attr_chan7_mux.dev_attr.attr,
+	NULL,
+};
+
+static const struct attribute_group meson_sar_adc_attr_group = {
+	.attrs = meson_sar_adc_attrs,
+};
+
 static const struct iio_info meson_sar_adc_iio_info = {
 	.read_raw = meson_sar_adc_iio_info_read_raw,
+	.attrs = &meson_sar_adc_attr_group,
 };
 
 static const struct meson_sar_adc_param meson_sar_adc_meson8_param = {