[PATCH v7 4/5] nvmem: eeprom: at25: export FRAM serial num

From: Jiri Prchal
Date: Mon Jun 07 2021 - 08:27:02 EST


This exports serial number of FRAM in sysfs file named "sernum".
Formatted in hex, each byte separated by space.
Example:
$ cat /sys/class/spi_master/spi0/spi0.0/sernum
0000a43644f2ae6c

Signed-off-by: Jiri Prchal <jiri.prchal@xxxxxxxxxxx>
---
v2: no change here
v3: resend and added more recipients
v4: resend
v5: reworked up on Greg comments: no spaces in string, sysfs done correctly
v6: no change here
v7: moved FM25_SN_LEN, static array, used sysfs_emit, DEVICE_ATTR_RO
---
drivers/misc/eeprom/at25.c | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/eeprom/at25.c b/drivers/misc/eeprom/at25.c
index 3b7ffef1f0d7..a28dfd7e1798 100644
--- a/drivers/misc/eeprom/at25.c
+++ b/drivers/misc/eeprom/at25.c
@@ -31,6 +31,7 @@
* AT25M02, AT25128B
*/

+#define FM25_SN_LEN 8 /* serial number length */
struct at25_data {
struct spi_device *spi;
struct mutex lock;
@@ -39,6 +40,7 @@ struct at25_data {
struct nvmem_config nvmem_config;
struct nvmem_device *nvmem;
int has_sernum;
+ u8 sernum[FM25_SN_LEN];
};

#define AT25_WREN 0x06 /* latch the write enable */
@@ -172,6 +174,21 @@ static int fm25_aux_read(struct at25_data *at25, char *buf, uint8_t command,
return status;
}

+static ssize_t sernum_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ struct at25_data *at25;
+
+ at25 = dev_get_drvdata(dev);
+ return sysfs_emit(buf, "%016llx\n", *(unsigned long long *)at25->sernum);
+}
+static DEVICE_ATTR_RO(sernum);
+
+static struct attribute *sernum_attrs[] = {
+ &dev_attr_sernum.attr,
+ NULL,
+};
+ATTRIBUTE_GROUPS(sernum);
+
static int at25_ee_write(void *priv, unsigned int off, void *val, size_t count)
{
struct at25_data *at25 = priv;
@@ -416,8 +433,10 @@ static int at25_probe(struct spi_device *spi)
else
at25->chip.flags |= EE_ADDR2;

- if (id[8])
+ if (id[8]) {
at25->has_sernum = 1;
+ fm25_aux_read(at25, at25->sernum, FM25_RDSN, FM25_SN_LEN);
+ }
else
at25->has_sernum = 0;

@@ -471,6 +490,7 @@ static struct spi_driver at25_driver = {
.driver = {
.name = "at25",
.of_match_table = at25_of_match,
+ .dev_groups = sernum_groups,
},
.probe = at25_probe,
};
--
2.25.1