Re: [PATCH v2] meson saradc: add iio device attrib to switch channel 7 mux

From: George Stark
Date: Sun May 28 2023 - 18:36:08 EST


On 5/28/23 13:46, Andy Shevchenko wrote:

Hello Andy
On Sun, May 28, 2023 at 12:48:54AM +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.
Thank you for an update, my comments below.

...

---
Missing changelog, what has been done in v2, how it's different to v1.
Ok I'll keep it on mind

drivers/iio/adc/meson_saradc.c | 65 ++++++++++++++++++++++++++++++++++
1 file changed, 65 insertions(+)
...

+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;
I think this is incorrect and prone to error in the future in case this array
will be extended. What I would expect is to return something like "unknown".

I agree this part is unclean. Actually the register's last 3 (out of 8) possible values
are stand for the same mux input "ch7_input". So theoretically we can read from register
a value out of array bounds. There should be a comment at least.

About the question of naming mux inputs from the other letter (vdd/2 vs 0.5Vdd etc).
While I fully agree with you that point is better than slash but mixing letter cases... should we?

e.g. this is how iio_info output looks like now:
...
            voltage7:  (input)
            3 channel-specific attributes found:
                attr  0: mean_raw value: 0
                attr  1: raw value: 1
                attr  2: scale value: 0.439453125
        4 device-specific attributes found:
                attr  0: chan7_mux value: gnd
                attr  1: chan7_mux_available value: gnd vdd/4 vdd/2 vdd*3/4 vdd ch7_input
                attr  2: current_timestamp_clock value: realtime

                attr  3: waiting_for_supplier value: 0

or naming with Jonathan's approach
/sys/devices/platform/soc/fe000000.bus/fe002c00.adc/iio:device0/in_voltage_0.5vdd_raw

Best regards
George

+ 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;
+
+ i = sysfs_match_string(chan7_vol, buf);
+ if (i < 0)
+ return -EINVAL;
Do not shadow the error code if it's not justified.

return i;

+ meson_sar_adc_set_chan7_mux(indio_dev, i);
+ return count;
+}
+
Redundant blank line.

+static IIO_DEVICE_ATTR_RW(chan7_mux, -1);
+
+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;
+}
+
Ditto.

+static IIO_DEVICE_ATTR_RO(chan7_mux_available, -1);