1 From 23769ad113407ceb21a4285a8194c7d788149269 Mon Sep 17 00:00:00 2001
2 From: Akira Shimahara <akira215corp@gmail.com>
3 Date: Mon, 11 May 2020 22:37:42 +0200
4 Subject: [PATCH] w1_therm: optimizing temperature read timings
6 commit 67b392f7b8edfa6f427fecd98722acab34c1c99f upstream.
8 Optimizing temperature reading by reducing waiting conversion time
9 according to device resolution settings, as per device specification.
10 This is device dependent as not all the devices supports resolution
11 setting, so it has been added in device family structures.
13 The process to read the temperature on the device has been adapted in a
14 new function 'convert_t()', which replace the former 'read_therm()', is
15 introduce to deal with this timing. Strong pull up is also applied during
16 the required time, according to device power status needs and
17 'strong_pullup' module parameter.
19 'temperature_from_RAM()' function is introduced to get the correct
20 temperature computation (device dependent) from device RAM data.
22 An new sysfs entry has been added to ouptut only temperature. The old
23 entry w1_slave has been kept for compatibility, without changing its
26 Updating Documentation/ABI/testing/sysfs-driver-w1_therm accordingly.
28 Signed-off-by: Akira Shimahara <akira215corp@gmail.com>
29 Link: https://lore.kernel.org/r/20200511203742.411039-1-akira215corp@gmail.com
30 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
32 .../ABI/testing/sysfs-driver-w1_therm | 12 +
33 drivers/w1/slaves/w1_therm.c | 286 +++++++++++-------
34 2 files changed, 197 insertions(+), 101 deletions(-)
36 --- a/Documentation/ABI/testing/sysfs-driver-w1_therm
37 +++ b/Documentation/ABI/testing/sysfs-driver-w1_therm
38 @@ -41,6 +41,18 @@ Users: any user space application which
42 +What: /sys/bus/w1/devices/.../temperature
44 +Contact: Akira Shimahara <akira215corp@gmail.com>
46 + (RO) return the temperature in 1/1000 degC.
47 + Note that the conversion duration depend on the resolution (if
48 + device support this feature). It takes 94ms in 9bits
49 + resolution, 750ms for 12bits.
50 +Users: any user space application which wants to communicate with
54 What: /sys/bus/w1/devices/.../w1_slave
56 Contact: Akira Shimahara <akira215corp@gmail.com>
57 --- a/drivers/w1/slaves/w1_therm.c
58 +++ b/drivers/w1/slaves/w1_therm.c
59 @@ -93,6 +93,7 @@ module_param_named(strong_pullup, w1_str
60 * @reserved: not used here
61 * @f: pointer to the device binding structure
62 * @convert: pointer to the device conversion function
63 + * @get_conversion_time: pointer to the device conversion time function
64 * @set_resolution: pointer to the device set_resolution function
65 * @get_resolution: pointer to the device get_resolution function
67 @@ -101,6 +102,7 @@ struct w1_therm_family_converter {
70 int (*convert)(u8 rom[9]);
71 + int (*get_conversion_time)(struct w1_slave *sl);
72 int (*set_resolution)(struct w1_slave *sl, int val);
73 int (*get_resolution)(struct w1_slave *sl);
75 @@ -154,6 +156,15 @@ struct therm_info {
76 static int reset_select_slave(struct w1_slave *sl);
79 + * convert_t() - Query the device for temperature conversion and read
80 + * @sl: pointer to the slave to read
81 + * @info: pointer to a structure to store the read results
83 + * Return: 0 if success, -kernel error code otherwise
85 +static int convert_t(struct w1_slave *sl, struct therm_info *info);
88 * read_scratchpad() - read the data in device RAM
89 * @sl: pointer to the slave to read
90 * @info: pointer to a structure to store the read results
91 @@ -213,6 +224,9 @@ static ssize_t w1_slave_store(struct dev
92 static ssize_t w1_seq_show(struct device *device,
93 struct device_attribute *attr, char *buf);
95 +static ssize_t temperature_show(struct device *device,
96 + struct device_attribute *attr, char *buf);
98 static ssize_t ext_power_show(struct device *device,
99 struct device_attribute *attr, char *buf);
101 @@ -229,6 +243,7 @@ static ssize_t eeprom_store(struct devic
103 static DEVICE_ATTR_RW(w1_slave);
104 static DEVICE_ATTR_RO(w1_seq);
105 +static DEVICE_ATTR_RO(temperature);
106 static DEVICE_ATTR_RO(ext_power);
107 static DEVICE_ATTR_RW(resolution);
108 static DEVICE_ATTR_WO(eeprom);
109 @@ -259,6 +274,7 @@ static void w1_therm_remove_slave(struct
111 static struct attribute *w1_therm_attrs[] = {
112 &dev_attr_w1_slave.attr,
113 + &dev_attr_temperature.attr,
114 &dev_attr_ext_power.attr,
115 &dev_attr_resolution.attr,
116 &dev_attr_eeprom.attr,
117 @@ -267,6 +283,7 @@ static struct attribute *w1_therm_attrs[
119 static struct attribute *w1_ds18s20_attrs[] = {
120 &dev_attr_w1_slave.attr,
121 + &dev_attr_temperature.attr,
122 &dev_attr_ext_power.attr,
123 &dev_attr_eeprom.attr,
125 @@ -275,6 +292,7 @@ static struct attribute *w1_ds18s20_attr
126 static struct attribute *w1_ds28ea00_attrs[] = {
127 &dev_attr_w1_slave.attr,
128 &dev_attr_w1_seq.attr,
129 + &dev_attr_temperature.attr,
130 &dev_attr_ext_power.attr,
131 &dev_attr_resolution.attr,
132 &dev_attr_eeprom.attr,
133 @@ -389,6 +407,37 @@ static struct w1_family w1_therm_family_
135 /* Device dependent func */
137 +static inline int w1_DS18B20_convert_time(struct w1_slave *sl)
141 + if (!sl->family_data)
142 + return -ENODEV; /* device unknown */
144 + /* return time in ms for conversion operation */
145 + switch (SLAVE_RESOLUTION(sl)) {
162 +static inline int w1_DS18S20_convert_time(struct w1_slave *sl)
165 + return 750; /* always 750ms for DS18S20 */
168 static inline int w1_DS18B20_write_data(struct w1_slave *sl,
171 @@ -480,8 +529,10 @@ static inline int w1_DS18S20_convert_tem
177 + pr_debug("%s: Invalid argument for conversion\n", __func__);
182 t = ((s32)rom[0] >> 1)*1000;
183 @@ -500,34 +551,39 @@ static inline int w1_DS18S20_convert_tem
185 static struct w1_therm_family_converter w1_therm_families[] = {
187 - .f = &w1_therm_family_DS18S20,
188 - .convert = w1_DS18S20_convert_temp,
189 - .set_resolution = NULL, /* no config register */
190 - .get_resolution = NULL, /* no config register */
191 + .f = &w1_therm_family_DS18S20,
192 + .convert = w1_DS18S20_convert_temp,
193 + .get_conversion_time = w1_DS18S20_convert_time,
194 + .set_resolution = NULL, /* no config register */
195 + .get_resolution = NULL, /* no config register */
198 - .f = &w1_therm_family_DS1822,
199 - .convert = w1_DS18B20_convert_temp,
200 - .set_resolution = w1_DS18B20_set_resolution,
201 - .get_resolution = w1_DS18B20_get_resolution,
202 + .f = &w1_therm_family_DS1822,
203 + .convert = w1_DS18B20_convert_temp,
204 + .get_conversion_time = w1_DS18B20_convert_time,
205 + .set_resolution = w1_DS18B20_set_resolution,
206 + .get_resolution = w1_DS18B20_get_resolution,
209 - .f = &w1_therm_family_DS18B20,
210 - .convert = w1_DS18B20_convert_temp,
211 - .set_resolution = w1_DS18B20_set_resolution,
212 - .get_resolution = w1_DS18B20_get_resolution,
213 + .f = &w1_therm_family_DS18B20,
214 + .convert = w1_DS18B20_convert_temp,
215 + .get_conversion_time = w1_DS18B20_convert_time,
216 + .set_resolution = w1_DS18B20_set_resolution,
217 + .get_resolution = w1_DS18B20_get_resolution,
220 - .f = &w1_therm_family_DS28EA00,
221 - .convert = w1_DS18B20_convert_temp,
222 - .set_resolution = w1_DS18B20_set_resolution,
223 - .get_resolution = w1_DS18B20_get_resolution,
224 + .f = &w1_therm_family_DS28EA00,
225 + .convert = w1_DS18B20_convert_temp,
226 + .get_conversion_time = w1_DS18B20_convert_time,
227 + .set_resolution = w1_DS18B20_set_resolution,
228 + .get_resolution = w1_DS18B20_get_resolution,
231 - .f = &w1_therm_family_DS1825,
232 - .convert = w1_DS18B20_convert_temp,
233 - .set_resolution = w1_DS18B20_set_resolution,
234 - .get_resolution = w1_DS18B20_get_resolution,
235 + .f = &w1_therm_family_DS1825,
236 + .convert = w1_DS18B20_convert_temp,
237 + .get_conversion_time = w1_DS18B20_convert_time,
238 + .set_resolution = w1_DS18B20_set_resolution,
239 + .get_resolution = w1_DS18B20_get_resolution,
243 @@ -582,24 +638,44 @@ static inline bool bus_mutex_lock(struct
247 - * w1_convert_temp() - temperature conversion binding function
248 - * @rom: data read from device RAM (8 data bytes + 1 CRC byte)
249 - * @fid: device family id
250 + * conversion_time() - get the Tconv for the slave
251 + * @sl: device to get the conversion time
253 - * The function call the temperature computation function according to
255 + * On device supporting resolution settings, conversion time depend
256 + * on the resolution setting. This helper function get the slave timing,
257 + * depending on its current setting.
259 - * Return: value in millidegrees Celsius.
260 + * Return: conversion time in ms, negative values are kernel error code
262 -static inline int w1_convert_temp(u8 rom[9], u8 fid)
263 +static inline int conversion_time(struct w1_slave *sl)
266 + if (SLAVE_SPECIFIC_FUNC(sl))
267 + return SLAVE_SPECIFIC_FUNC(sl)->get_conversion_time(sl);
269 - for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
270 - if (w1_therm_families[i].f->fid == fid)
271 - return w1_therm_families[i].convert(rom);
273 + "%s: Device not supported by the driver\n", __func__);
276 + return -ENODEV; /* No device family */
280 + * temperature_from_RAM() - Convert the read info to temperature
281 + * @sl: device that sent the RAM data
282 + * @rom: read value on the slave device RAM
284 + * Device dependent, the function bind the correct computation method.
286 + * Return: temperature in 1/1000degC, 0 on error.
288 +static inline int temperature_from_RAM(struct w1_slave *sl, u8 rom[9])
290 + if (SLAVE_SPECIFIC_FUNC(sl))
291 + return SLAVE_SPECIFIC_FUNC(sl)->convert(rom);
294 + "%s: Device not supported by the driver\n", __func__);
296 + return 0; /* No device family */
299 /* Interface Functions */
300 @@ -679,96 +755,74 @@ static int reset_select_slave(struct w1_
304 -static ssize_t read_therm(struct device *device,
305 - struct w1_slave *sl, struct therm_info *info)
306 +static int convert_t(struct w1_slave *sl, struct therm_info *info)
308 - struct w1_master *dev = sl->master;
310 - int ret, max_trying = 10;
311 - u8 *family_data = sl->family_data;
312 + struct w1_master *dev_master = sl->master;
313 + int max_trying = W1_THERM_MAX_TRY;
316 + bool strong_pullup;
318 - if (!family_data) {
320 + if (!sl->family_data)
324 - /* prevent the slave from going away in sleep */
325 - atomic_inc(THERM_REFCNT(family_data));
326 + strong_pullup = (w1_strong_pullup == 2 ||
327 + (!SLAVE_POWERMODE(sl) &&
328 + w1_strong_pullup));
330 - ret = mutex_lock_interruptible(&dev->bus_mutex);
333 + /* get conversion duration device and id dependent */
334 + t_conv = conversion_time(sl);
336 memset(info->rom, 0, sizeof(info->rom));
338 - while (max_trying--) {
339 + /* prevent the slave from going away in sleep */
340 + atomic_inc(THERM_REFCNT(sl->family_data));
342 + if (!bus_mutex_lock(&dev_master->bus_mutex)) {
343 + ret = -EAGAIN; /* Didn't acquire the mutex */
347 + while (max_trying-- && ret) { /* ret should be 0 */
352 + /* safe version to select slave */
353 if (!reset_select_slave(sl)) {
355 - unsigned int tm = 750;
356 unsigned long sleep_rem;
358 - w1_write_8(dev, W1_READ_PSUPPLY);
359 - external_power = w1_read_8(dev);
361 - if (reset_select_slave(sl))
364 /* 750ms strong pullup (or delay) after the convert */
365 - if (w1_strong_pullup == 2 ||
366 - (!external_power && w1_strong_pullup))
367 - w1_next_pullup(dev, tm);
369 - w1_write_8(dev, W1_CONVERT_TEMP);
371 + w1_next_pullup(dev_master, t_conv);
373 - if (external_power) {
374 - mutex_unlock(&dev->bus_mutex);
375 + w1_write_8(dev_master, W1_CONVERT_TEMP);
377 - sleep_rem = msleep_interruptible(tm);
378 + if (strong_pullup) { /*some device need pullup */
379 + sleep_rem = msleep_interruptible(t_conv);
380 if (sleep_rem != 0) {
385 + mutex_unlock(&dev_master->bus_mutex);
386 + } else { /*no device need pullup */
387 + mutex_unlock(&dev_master->bus_mutex);
389 - ret = mutex_lock_interruptible(&dev->bus_mutex);
392 - } else if (!w1_strong_pullup) {
393 - sleep_rem = msleep_interruptible(tm);
394 + sleep_rem = msleep_interruptible(t_conv);
395 if (sleep_rem != 0) {
402 - if (!reset_select_slave(sl)) {
404 - w1_write_8(dev, W1_READ_SCRATCHPAD);
405 - count = w1_read_block(dev, info->rom, 9);
407 - dev_warn(device, "w1_read_block() "
408 - "returned %u instead of 9.\n",
412 - info->crc = w1_calc_crc8(info->rom, 8);
414 - if (info->rom[8] == info->crc)
417 + ret = read_scratchpad(sl, info);
426 - mutex_unlock(&dev->bus_mutex);
427 + mutex_unlock(&dev_master->bus_mutex);
429 - atomic_dec(THERM_REFCNT(family_data));
430 + atomic_dec(THERM_REFCNT(sl->family_data));
434 @@ -1000,27 +1054,33 @@ static ssize_t w1_slave_show(struct devi
435 u8 *family_data = sl->family_data;
437 ssize_t c = PAGE_SIZE;
438 - u8 fid = sl->family->fid;
440 - ret = read_therm(device, sl, &info);
443 + ret = convert_t(sl, &info);
447 + "%s: Temperature data may be corrupted. err=%d\n",
452 for (i = 0; i < 9; ++i)
453 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", info.rom[i]);
454 c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
455 info.crc, (info.verdict) ? "YES" : "NO");
458 memcpy(family_data, info.rom, sizeof(info.rom));
460 - dev_warn(device, "Read failed CRC check\n");
461 + dev_warn(device, "%s:Read failed CRC check\n", __func__);
463 for (i = 0; i < 9; ++i)
464 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ",
465 ((u8 *)family_data)[i]);
467 c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
468 - w1_convert_temp(info.rom, fid));
469 + temperature_from_RAM(sl, info.rom));
474 @@ -1063,6 +1123,31 @@ static ssize_t w1_slave_store(struct dev
475 return size; /* always return size to avoid infinite calling */
478 +static ssize_t temperature_show(struct device *device,
479 + struct device_attribute *attr, char *buf)
481 + struct w1_slave *sl = dev_to_w1_slave(device);
482 + struct therm_info info;
485 + if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
487 + "%s: Device not supported by the driver\n", __func__);
488 + return 0; /* No device family */
491 + ret = convert_t(sl, &info);
495 + "%s: Temperature data may be corrupted. err=%d\n",
500 + return sprintf(buf, "%d\n", temperature_from_RAM(sl, info.rom));
503 static ssize_t ext_power_show(struct device *device,
504 struct device_attribute *attr, char *buf)
506 @@ -1172,12 +1257,11 @@ static int w1_read_temp(struct device *d
508 struct w1_slave *sl = dev_get_drvdata(device);
509 struct therm_info info;
510 - u8 fid = sl->family->fid;
514 case hwmon_temp_input:
515 - ret = read_therm(device, sl, &info);
516 + ret = convert_t(sl, &info);
520 @@ -1186,7 +1270,7 @@ static int w1_read_temp(struct device *d
524 - *val = w1_convert_temp(info.rom, fid);
525 + *val = temperature_from_RAM(sl, info.rom);