w1: ds2438: adding support for reading page1
authorLuiz Sampaio <sampaio.ime@gmail.com>
Wed, 19 May 2021 22:30:45 +0000 (19:30 -0300)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 21 May 2021 12:51:33 +0000 (14:51 +0200)
Added a sysfs entry to support reading the page1 registers. This registers
contain Elapsed Time Meter (ETM) data, which shows for how long the chip is
on, as well as an Offset Register data, which can be used to calibrate the
current measurement of the chip.

Signed-off-by: Luiz Sampaio <sampaio.ime@gmail.com>
Link: https://lore.kernel.org/r/20210519223046.13798-6-sampaio.ime@gmail.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Documentation/ABI/stable/sysfs-driver-w1_ds2438 [new file with mode: 0644]
Documentation/w1/slaves/w1_ds2438.rst
drivers/w1/slaves/w1_ds2438.c

diff --git a/Documentation/ABI/stable/sysfs-driver-w1_ds2438 b/Documentation/ABI/stable/sysfs-driver-w1_ds2438
new file mode 100644 (file)
index 0000000..fa47437
--- /dev/null
@@ -0,0 +1,6 @@
+What:          /sys/bus/w1/devices/.../page1
+Date:          April 2021
+Contact:       Luiz Sampaio <sampaio.ime@gmail.com>
+Description:   read the contents of the page1 of the DS2438
+               see Documentation/w1/slaves/w1_ds2438.rst for detailed information
+Users:         any user space application which wants to communicate with DS2438
index a29309a3f8e579aa3f8c5289174db88e99c75568..ac8d0d4b0d0e5a80c812c29356f3806c17e679ab 100644 (file)
@@ -44,6 +44,14 @@ Internally when this file is read, the additional CRC byte is also obtained
 from the slave device. If it is correct, the 8 bytes page data are passed
 to userspace, otherwise an I/O error is returned.
 
+"page1"
+-------
+This file provides full 8 bytes of the chip Page 1 (01h).
+This page contains the ICA, elapsed time meter and current offset data of the DS2438.
+Internally when this file is read, the additional CRC byte is also obtained
+from the slave device. If it is correct, the 8 bytes page data are passed
+to userspace, otherwise an I/O error is returned.
+
 "temperature"
 -------------
 Opening and reading this file initiates the CONVERT_T (temperature conversion)
index 1e95f3a256c707b2953e3ae49de0b60e06e07f39..42080ae779f03b9a6b72bb39a1865cfe22433984 100644 (file)
 #define DS2438_CURRENT_MSB             0x06
 #define DS2438_THRESHOLD               0x07
 
+/* Page #1 definitions */
+#define DS2438_ETM_0                   0x00
+#define DS2438_ETM_1                   0x01
+#define DS2438_ETM_2                   0x02
+#define DS2438_ETM_3                   0x03
+#define DS2438_ICA                     0x04
+#define DS2438_OFFSET_LSB              0x05
+#define DS2438_OFFSET_MSB              0x06
+
 static int w1_ds2438_get_page(struct w1_slave *sl, int pageno, u8 *buf)
 {
        unsigned int retries = W1_DS2438_RETRIES;
@@ -325,6 +334,36 @@ static ssize_t page0_read(struct file *filp, struct kobject *kobj,
        return ret;
 }
 
+static ssize_t page1_read(struct file *filp, struct kobject *kobj,
+                         struct bin_attribute *bin_attr, char *buf,
+                         loff_t off, size_t count)
+{
+       struct w1_slave *sl = kobj_to_w1_slave(kobj);
+       int ret;
+       u8 w1_buf[DS2438_PAGE_SIZE + 1 /*for CRC*/];
+
+       if (off != 0)
+               return 0;
+       if (!buf)
+               return -EINVAL;
+
+       mutex_lock(&sl->master->bus_mutex);
+
+       /* Read no more than page1 size */
+       if (count > DS2438_PAGE_SIZE)
+               count = DS2438_PAGE_SIZE;
+
+       if (w1_ds2438_get_page(sl, 1, w1_buf) == 0) {
+               memcpy(buf, &w1_buf, count);
+               ret = count;
+       } else
+               ret = -EIO;
+
+       mutex_unlock(&sl->master->bus_mutex);
+
+       return ret;
+}
+
 static ssize_t temperature_read(struct file *filp, struct kobject *kobj,
                                struct bin_attribute *bin_attr, char *buf,
                                loff_t off, size_t count)
@@ -390,6 +429,7 @@ static ssize_t vdd_read(struct file *filp, struct kobject *kobj,
 
 static BIN_ATTR_RW(iad, 0);
 static BIN_ATTR_RO(page0, DS2438_PAGE_SIZE);
+static BIN_ATTR_RO(page1, DS2438_PAGE_SIZE);
 static BIN_ATTR_RO(temperature, 0/* real length varies */);
 static BIN_ATTR_RO(vad, 0/* real length varies */);
 static BIN_ATTR_RO(vdd, 0/* real length varies */);
@@ -397,6 +437,7 @@ static BIN_ATTR_RO(vdd, 0/* real length varies */);
 static struct bin_attribute *w1_ds2438_bin_attrs[] = {
        &bin_attr_iad,
        &bin_attr_page0,
+       &bin_attr_page1,
        &bin_attr_temperature,
        &bin_attr_vad,
        &bin_attr_vdd,