[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250721195419.526920-1-akshayaj.lkd@gmail.com>
Date: Tue, 22 Jul 2025 01:24:18 +0530
From: Akshay Jindal <akshayaj.lkd@...il.com>
To: anshulusr@...il.com,
jic23@...nel.org,
dlechner@...libre.com,
nuno.sa@...log.com,
andy@...nel.org
Cc: shuah@...nel.org,
linux-iio@...r.kernel.org,
linux-kernel@...r.kernel.org,
Akshay Jindal <akshayaj.lkd@...il.com>
Subject: [PATCH 1/2] iio: light: ltr390: Add sysfs attribute to report data freshness
Some IIO sensors provide a status bit indicating whether the current data
register holds freshly measured data or stale data previously read.
Expose this status via a new read-only sysfs attribute, 'data_fresh',
which returns:
- 1 if the data is freshly sampled
- 0 if the data is stale
This attribute allows userspace to observe data freshness directly, which
can be useful for debugging or application-level filtering.
Document the attribute under Documentation/ABI/testing/sysfs-bus-iio.
Signed-off-by: Akshay Jindal <akshayaj.lkd@...il.com>
---
Testing details:
================
-> Tested on Raspberrypi 4B. Follow for more details.
akshayajpi@...pberrypi:~ $ uname -r
6.12.35-v8+
akshayajpi@...pberrypi:~ $ uname -a
Linux raspberrypi 6.12.35-v8+ #5 SMP PREEMPT Tue Jul 15 17:38:06 IST 2025 aarch64 GNU/Linux
-> Sensor Detection, overlaying of device tree and Driver loading
akshayajpi@...pberrypi:~ $ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- 53 -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
akshayajpi@...pberrypi:~ $ sudo dtoverlay i2c-sensor ltr390
akshayajpi@...pberrypi:~ $ lsmod|grep ltr390
ltr390 16384 0
industrialio 110592 1 ltr390
regmap_i2c 12288 1 ltr390
-> Sysfs Attribute Creation validation
akshayajpi@...pberrypi:~ $ ls -ltrh /sys/bus/iio/devices/iio\:device0/
total 0
-rw-r--r-- 1 root root 4.0K Jul 21 21:41 uevent
-r--r--r-- 1 root root 4.0K Jul 21 21:41 name
lrwxrwxrwx 1 root root 0 Jul 21 21:42 of_node -> ../../../../../../../firmware/devicetree/base/soc/i2c@...04000/ltr390@53
-rw-r--r-- 1 root root 4.0K Jul 21 21:42 integration_time
-r--r--r-- 1 root root 4.0K Jul 21 21:42 waiting_for_supplier
lrwxrwxrwx 1 root root 0 Jul 21 21:42 subsystem -> ../../../../../../../bus/iio
-r--r--r-- 1 root root 4.0K Jul 21 21:42 scale_available
-r--r--r-- 1 root root 4.0K Jul 21 21:42 sampling_frequency_available
-rw-r--r-- 1 root root 4.0K Jul 21 21:42 sampling_frequency
drwxr-xr-x 2 root root 0 Jul 21 21:42 power
-rw-r--r-- 1 root root 4.0K Jul 21 21:42 in_uvindex_scale
-rw-r--r-- 1 root root 4.0K Jul 21 21:42 in_uvindex_raw
-r--r--r-- 1 root root 4.0K Jul 21 21:42 integration_time_available
-rw-r--r-- 1 root root 4.0K Jul 21 21:42 in_illuminance_scale
-rw-r--r-- 1 root root 4.0K Jul 21 21:42 in_illuminance_raw
drwxr-xr-x 2 root root 0 Jul 21 21:42 events
-r--r--r-- 1 root root 4.0K Jul 21 21:42 dev
-r--r--r-- 1 root root 4.0K Jul 21 21:42 data_fresh<-----
-> Disabled Bit 1 in MAIN_CTRL (reg 0x0) register to stop fresh measurements
akshayajpi@...pberrypi:~ $ cat /sys/bus/iio/devices/iio\:device0/in_illuminance_raw
609
akshayajpi@...pberrypi:~ $ cat /sys/bus/iio/devices/iio\:device0/in_illuminance_raw
603
akshayajpi@...pberrypi:~ $ cat /sys/bus/iio/devices/iio\:device0/data_fresh
1
akshayajpi@...pberrypi:~ $ cat /sys/bus/iio/devices/iio\:device0/data_fresh
1
akshayajpi@...pberrypi:~ $ i2cget -f -y 1 0x53 0x0
0x02
akshayajpi@...pberrypi:~ $ i2cset -f -y 1 0x53 0x0 0x0
akshayajpi@...pberrypi:~ $ i2cget -f -y 1 0x53 0x0
0x00
-> data_status bit cleared after reading
akshayajpi@...pberrypi:~ $ cat /sys/bus/iio/devices/iio\:device0/data_fresh
1
akshayajpi@...pberrypi:~ $ cat /sys/bus/iio/devices/iio\:device0/data_fresh
0
akshayajpi@...pberrypi:~ $ cat /sys/bus/iio/devices/iio\:device0/data_fresh
0
akshayajpi@...pberrypi:~ $ cat /sys/bus/iio/devices/iio\:device0/in_illuminance_raw
605
akshayajpi@...pberrypi:~ $ cat /sys/bus/iio/devices/iio\:device0/in_illuminance_raw
605
-> Re-enabled sensor and fresh measurements reported
akshayajpi@...pberrypi:~ $ i2cset -f -y 1 0x53 0x0 0x2
akshayajpi@...pberrypi:~ $ cat /sys/bus/iio/devices/iio\:device0/data_fresh
1
akshayajpi@...pberrypi:~ $ cat /sys/bus/iio/devices/iio\:device0/data_fresh
1
akshayajpi@...pberrypi:~ $ cat /sys/bus/iio/devices/iio\:device0/in_illuminance_raw
607
akshayajpi@...pberrypi:~ $ cat /sys/bus/iio/devices/iio\:device0/in_illuminance_raw
622
Documentation/ABI/testing/sysfs-bus-iio | 14 +++++++++++
drivers/iio/light/ltr390.c | 33 +++++++++++++++++++++++++
2 files changed, 47 insertions(+)
diff --git a/Documentation/ABI/testing/sysfs-bus-iio b/Documentation/ABI/testing/sysfs-bus-iio
index 190bfcc1e836..5d176d46c15d 100644
--- a/Documentation/ABI/testing/sysfs-bus-iio
+++ b/Documentation/ABI/testing/sysfs-bus-iio
@@ -2383,3 +2383,17 @@ Description:
Value representing the user's attention to the system expressed
in units as percentage. This usually means if the user is
looking at the screen or not.
+
+What: /sys/.../iio:deviceX/data_fresh
+KernelVersion: 6.16
+Contact: linux-iio@...r.kernel.org
+Description:
+ Read-only attribute indicating whether the sensor data currently
+ available in the device is freshly measured or stale.
+
+ Returns:
+ 1 - Data is freshly measured
+ 0 - Data is stale (previously read or not yet updated)
+
+ Provides userspace visibility into data_freshness status which
+ can be used for debugging and informational use.
diff --git a/drivers/iio/light/ltr390.c b/drivers/iio/light/ltr390.c
index ee59bbb8aa09..5af0ffd3df1d 100644
--- a/drivers/iio/light/ltr390.c
+++ b/drivers/iio/light/ltr390.c
@@ -30,6 +30,7 @@
#include <linux/iio/iio.h>
#include <linux/iio/events.h>
+#include <linux/iio/sysfs.h>
#include <linux/unaligned.h>
@@ -61,6 +62,7 @@
#define LTR390_FRACTIONAL_PRECISION 100
+#define LTR390_DATA_STATUS_MASK BIT(3)
/*
* At 20-bit resolution (integration time: 400ms) and 18x gain, 2300 counts of
* the sensor are equal to 1 UV Index [Datasheet Page#8].
@@ -178,6 +180,36 @@ static int ltr390_get_samp_freq_or_period(struct ltr390_data *data,
return ltr390_samp_freq_table[value][option];
}
+/*
+ * Indicates whether the most recent sensor data read from the device
+ * was freshly measured (1) or stale/old (0). This is based on ltr390's
+ * internal data status bit.
+ */
+static ssize_t data_fresh_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ int ret, status;
+ struct ltr390_data *data = iio_priv(dev_to_iio_dev(dev));
+
+ ret = ltr390_register_read(data, LTR390_MAIN_STATUS);
+ if (ret < 0)
+ return ret;
+
+ status = ret;
+ return sysfs_emit(buf, "%d\n", !!(status & LTR390_DATA_STATUS_MASK));
+}
+
+static IIO_DEVICE_ATTR_RO(data_fresh, 0);
+
+static struct attribute *ltr390_attributes[] = {
+ &iio_dev_attr_data_fresh.dev_attr.attr,
+ NULL
+};
+
+static const struct attribute_group ltr390_attribute_group = {
+ .attrs = ltr390_attributes,
+};
+
static int ltr390_read_raw(struct iio_dev *iio_device,
struct iio_chan_spec const *chan, int *val,
int *val2, long mask)
@@ -594,6 +626,7 @@ static const struct iio_info ltr390_info = {
.read_event_config = ltr390_read_event_config,
.write_event_value = ltr390_write_event_value,
.write_event_config = ltr390_write_event_config,
+ .attrs = <r390_attribute_group,
};
static irqreturn_t ltr390_interrupt_handler(int irq, void *private)
--
2.43.0
Powered by blists - more mailing lists