Skip to contentSkip to conversation

w1_therm: adding eeprom sysfs entry

PATCH In review Akira Shimahara 1 participant 1 message Apr 29, 2020 W1 dallas's 1-wire bus

lore
Share & tools
lore
#0 lore raw

PATCH v4 6/9 w1_therm: adding eeprom sysfs entry

Adding eeprom sysfs entry (WO) to trigger either device EEPROM save (by writing 'save' in the sysfs) either device EEPROM restore (by writing 'restore' in the sysfs). All the RAM of the device will be save/restore, whatever its size : resolution config register and alarms level will be save/restore.

The driver implement 2 basics functions to access device RAM:
* copy_scratchpad
* recall_scratchpad
They act according to device specifications.

As EEPROM operations are not device dependent (all w1_therm can perform EEPROM read/write operation following the same protocol), it is removed from device families structures.

---
 .../ABI/testing/sysfs-driver-w1_therm         |  14 ++
 drivers/w1/slaves/w1_therm.c                  | 151 +++++++++++-------
 drivers/w1/slaves/w1_therm.h                  |  36 ++++-
 3 files changed, 142 insertions(+), 59 deletions(-)
Documentation/ABI/testing/sysfs-driver-w1_therm +14 -0
index e52dd6a..9dcbbed 100644
@@ -1,3 +1,17 @@
+What:		/sys/bus/w1/devices/.../eeprom
+Date:		Apr 2020
+Contact:	Akira Shimahara <[email protected]>
+Description:
+		(WO) writing that file will either trigger a save of the
+		device data to its embedded EEPROM, either restore data
+		embedded in device EEPROM. Be aware that devices support
+		limited EEPROM writing cycles (typical 50k)
+			* `save`: save device RAM to EEPROM
+			* `restore`: restore EEPROM data in device RAM
+Users:		any user space application which wants to communicate with
+		w1_term device
+
+
 What:		/sys/bus/w1/devices/.../ext_power
 Date:		Apr 2020
 Contact:	Akira Shimahara <[email protected]>
drivers/w1/slaves/w1_therm.c +97 -54

251 lines not on this page · raw message

drivers/w1/slaves/w1_therm.h +31 -5
index b7f0cac..7202627 100644
@@ -25,12 +25,21 @@
 #include <linux/mutex.h>
 #include <linux/w1.h>
 
+/* This command should be in public header w1.h but is not */
+#define W1_RECALL_EEPROM	0xB8
+
 /* Nb of try for an operation */
 #define W1_THERM_MAX_TRY		5
 
 /* ms delay to retry bus mutex */
 #define W1_THERM_RETRY_DELAY		20
 
+/* delay in ms to write in EEPROM */
+#define W1_THERM_EEPROM_WRITE_DELAY	10
+
+#define EEPROM_CMD_WRITE    "save"	/* cmd for write eeprom sysfs */
+#define EEPROM_CMD_READ     "restore"	/* cmd for read eeprom sysfs */
+
 /*----------------------------------Structs---------------------------------*/
 
 /**
@@ -45,7 +54,6 @@ struct w1_therm_family_converter {
 	u16		reserved;
 	struct w1_family	*f;
 	int		(*convert)(u8 rom[9]);
-	int		(*eeprom)(struct device *device);
 	int		(*set_resolution)(struct w1_slave *sl, int val);
 	int		(*get_resolution)(struct w1_slave *sl);
 	int		(*write_data)(struct w1_slave *sl, const u8 *data);
@@ -84,10 +92,6 @@ struct therm_info {
 
 /*-----------------------Device specific functions-------------------------*/
 
-/* write configuration to eeprom */
-static inline int w1_therm_eeprom(struct device *device);
-
-/* The return value is millidegrees Centigrade. */
 static inline int w1_DS18B20_convert_temp(u8 rom[9]);
 static inline int w1_DS18S20_convert_temp(u8 rom[9]);
 
@@ -175,6 +179,19 @@ static int read_scratchpad(struct w1_slave *sl, struct therm_info *info);
  */
 static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes);
 
+/** copy_scratchpad() - Copy the content of scratchpad in device EEPROM
+ *  @param sl slave involved
+ *  @return 0 if success, -kernel error code otherwise
+ */
+static int copy_scratchpad(struct w1_slave *sl);
+
+/** recall_eeprom()
+ * @brief retrieve EEPROM data to device RAM
+ * @param sl slave involved
+ * @return 0 if success, -kernel error code otherwise
+ */
+static int recall_eeprom(struct w1_slave *sl);
+
 /** read_powermode()
  * @brief ask the device to get its power mode {external, parasite}
  * @param sl slave to be interrogated
@@ -183,6 +200,7 @@ static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes);
  *			<0 kernel error code
  */
 static int read_powermode(struct w1_slave *sl);
+
 /*----------------------------Interface sysfs-------------------------------*/
 
 /** @brief A callback function to output the temperature Old way
@@ -225,12 +243,20 @@ static ssize_t resolution_show(struct device *device,
  */
 static ssize_t resolution_store(struct device *device,
 	struct device_attribute *attr, const char *buf, size_t size);
+
+/** @brief A callback function to let the user read/write device EEPROM
+ *  @param check EEPROM_CMD_WRITE & EEPROM_CMD_READ macros
+ */
+static ssize_t eeprom_store(struct device *device,
+	struct device_attribute *attr, const char *buf, size_t size);
+
 /*-----------------------------Attributes declarations----------------------*/
 
 static DEVICE_ATTR_RW(w1_slave);
 static DEVICE_ATTR_RO(w1_seq);
 static DEVICE_ATTR_RO(ext_power);
 static DEVICE_ATTR_RW(resolution);
+static DEVICE_ATTR_WO(eeprom);
 
 /*--------------------------Interface Functions-----------------------------*/
 
Signature · 1 line
2.26.2