Skip to contentSkip to conversation

w1_therm: optimizing temperature reading timings

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 7/9 w1_therm: optimizing temperature reading timings

Optimizing temperature reading by reducing waiting conversion time according to device resolution settings, as per device specification. This is device dependent as not all the devices supports resolution setting, so it has been added in device family structures.

The process to read the temperature on the device has been adapted in a new function convert_t() to deal with this timing. Strong pull up is also applied during the required time, according to device power status needs and strong_pullup module parameter.

An new sysfs entry has been added to ouptut only temperature. The old entry w1_slave has been kept for compatibility, without changing its output format.

Updating doc in Documentation/ABI/testing/sysfs-driver-w1_therm accordingly.

---
 .../ABI/testing/sysfs-driver-w1_therm         |  12 +
 drivers/w1/slaves/w1_therm.c                  | 266 +++++++++++-------
 drivers/w1/slaves/w1_therm.h                  |  34 ++-
 3 files changed, 215 insertions(+), 97 deletions(-)
Documentation/ABI/testing/sysfs-driver-w1_therm +12 -0
index 9dcbbed..279e13d 100644
@@ -41,6 +41,18 @@ Users:		any user space application which wants to communicate with
 		w1_term device
 
 
+What:		/sys/bus/w1/devices/.../temperature
+Date:		Apr 2020
+Contact:	Akira Shimahara <[email protected]>
+Description:
+		(RO) return the temperature in 1/1000 degC.
+		Note that the conversion duration depend on the resolution (if
+		device support this feature). It takes 94ms in 9bits
+		resolution, 750ms for 12bits.
+Users:		any user space application which wants to communicate with
+		w1_term device
+
+
 What:		/sys/bus/w1/devices/.../w1_slave
 Date:		Apr 2020
 Contact:	Akira Shimahara <[email protected]>
drivers/w1/slaves/w1_therm.c +172 -94

424 lines not on this page · raw message

drivers/w1/slaves/w1_therm.h +31 -3
index 7202627..6929991 100644
@@ -54,6 +54,7 @@ struct w1_therm_family_converter {
 	u16		reserved;
 	struct w1_family	*f;
 	int		(*convert)(u8 rom[9]);
+	int		(*get_conversion_time)(struct w1_slave *sl);
 	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);
@@ -95,6 +96,9 @@ struct therm_info {
 static inline int w1_DS18B20_convert_temp(u8 rom[9]);
 static inline int w1_DS18S20_convert_temp(u8 rom[9]);
 
+static inline int w1_DS18B20_convert_time(struct w1_slave *sl);
+static inline int w1_DS18S20_convert_time(struct w1_slave *sl);
+
 static inline int w1_DS18B20_write_data(struct w1_slave *sl, const u8 *data);
 static inline int w1_DS18S20_write_data(struct w1_slave *sl, const u8 *data);
 
@@ -141,6 +145,20 @@ static struct w1_therm_family_converter *device_family(struct w1_slave *sl);
  */
 static inline bool bus_mutex_lock(struct mutex *lock);
 
+/** conversion_time() get the Tconv fo the device
+ *  @param sl device to get the conversion time
+ *  @return conversion time in ms, negative values kernel error code
+ */
+static inline int conversion_time(struct w1_slave *sl);
+
+/** temperature_from_RAM() return the temperature in 1/100°
+ *  @brief Device dependent, it will select the correct computation method
+ *  @param sl device that sent the RAM data
+ *  @param rom ram read value
+ *  @return temperature in 1/1000°
+ */
+static inline int temperature_from_RAM(struct w1_slave *sl, u8 rom[9]);
+
 /*---------------------------Hardware Functions-----------------------------*/
 
 /**
@@ -156,13 +174,12 @@ static inline bool bus_mutex_lock(struct mutex *lock);
  */
 static int reset_select_slave(struct w1_slave *sl);
 
-/** read_therm()
+/** convert_t()
  * @param sl pointer to the slave to read
  * @param info pointer to a structure to store the read results
  * @return 0 if success, -kernel error code otherwise
  */
-static ssize_t read_therm(struct device *device,
-			struct w1_slave *sl, struct therm_info *info);
+static int convert_t(struct w1_slave *sl, struct therm_info *info);
 
 /** read_scratchpad()
  * @param sl pointer to the slave to read
@@ -220,6 +237,16 @@ static ssize_t w1_slave_store(struct device *device,
 
 static ssize_t w1_seq_show(struct device *device,
 	struct device_attribute *attr, char *buf);
+
+/** @brief A callback function to output the temperature
+ *  Main differences with w1_slave :
+ *	No hardware check (just read the stored device infos)
+ *	No formatting
+ *  @return temperature (1/1000°)
+ */
+static ssize_t temperature_show(struct device *device,
+	struct device_attribute *attr, char *buf);
+
 /** @brief A callback function to output the power mode of the device
  *	Once done, it is stored in the sl->family_data to avoid doing the test
  *	during data read
@@ -254,6 +281,7 @@ static ssize_t eeprom_store(struct device *device,
 
 static DEVICE_ATTR_RW(w1_slave);
 static DEVICE_ATTR_RO(w1_seq);
+static DEVICE_ATTR_RO(temperature);
 static DEVICE_ATTR_RO(ext_power);
 static DEVICE_ATTR_RW(resolution);
 static DEVICE_ATTR_WO(eeprom);
Signature · 1 line
2.26.2